Script builder helper

Path: /sdk/add_on/scriptbuilder/

This class is a helper class for loading and building scripts, with a basic pre-processor that supports include directives and metadata declarations.

Public C++ interface

class CScriptBuilder
{
public:
  // Load and build a script file from disk
  int BuildScriptFromFile(asIScriptEngine *engine, 
                          const char      *module, 
                          const char      *filename);

  // Build a script file from a memory buffer
  int BuildScriptFromMemory(asIScriptEngine *engine, 
                            const char      *module, 
                            const char      *script, 
                            const char      *sectionname = "");

  // Get metadata declared for class types and interfaces
  const char *GetMetadataStringForType(int typeId);

  // Get metadata declared for functions
  const char *GetMetadataStringForFunc(int funcId);

  // Get metadata declared for global variables
  const char *GetMetadataStringForVar(int varIdx);
};

Include directives

Example script with include directive:

  #include "commonfuncs.as"

  void main()
  {
    // Call a function from the included file
    CommonFunc();
  }

Metadata in scripts

Metadata can be added before script class, interface, function, and global variable declarations. The metadata is removed from the script by the builder class and stored for post build lookup by the type id, function id, or variable index.

Exactly what the metadata looks like is up to the application. The builder class doesn't impose any rules, except that the metadata should be added between brackets []. After the script has been built the application can obtain the metadata strings and interpret them as it sees fit.

Example script with metadata:

  [factory func = CreateOgre,
   editable: myPosition,
   editable: myStrength [10, 100]]
  class COgre
  {
    vector3 myPosition;
    int     myStrength;
  }

  [factory]
  COgre @CreateOgre()
  {
    return @COgre();
  }

Example usage:

CScriptBuilder builder;
int r = builder.BuildScriptFromMemory(engine, "my module", script);
if( r >= 0 )
{
  // Find global variables that have been marked as editable by user
  int count = engine->GetGlobalVarCount("my module");
  for( int n = 0; n < count; n++ )
  {
    string metadata = builder.GetMetadataStringForVar(n);
    if( metadata == "editable" )
    {
      // Show the global variable in a GUI
      ...
    }
  }
}

Generated on Sun Dec 14 10:31:06 2008 for AngelScript by  doxygen 1.5.6