| AngelScript |

Loading...
Searching...
No Matches
Compiling scripts

After registering the application interface it's time to compile the scripts that will be executed.

# Message callback

Before starting the compilation, remember to have the message callback set in the engine so that you can get more information on compilation errors than just an error code. In fact, it is recommended to set the message callback right after creating the script engine, as the message callback may even be helpful while registering the application interface.

The message callback has been designed so that it doesn't output anything if there are no errors or warnings, so when everything is ok, you shouldn't get anything from it. But if the Build method returns an error, the message callback will have received detailed information about the error.

If desired, the application may send its own messages to the callback via the WriteMessage method on the engine.

// Implement a simple message callback function
void MessageCallback(const asSMessageInfo *msg, void *param)
{
const char *type = "ERR ";
if( msg->type == asMSGTYPE_WARNING )
type = "WARN";
else if( msg->type == asMSGTYPE_INFORMATION )
type = "INFO";
printf("%s (%d, %d) : %s : %s\n", msg->section, msg->row, msg->col, type, msg->message);
}
// Set the message callback when creating the engine
asIScriptEngine *engine = asCreateScriptEngine();
engine->SetMessageCallback(asFUNCTION(MessageCallback), 0, asCALL_CDECL);
asCALL_CDECL
@ asCALL_CDECL
A cdecl function.
Definition: angelscript.h:230
asFUNCTION
#define asFUNCTION(f)
Returns an asSFuncPtr representing the function specified by the name.
Definition: angelscript.h:685
asMSGTYPE_WARNING
@ asMSGTYPE_WARNING
The message is a warning.
Definition: angelscript.h:428
asMSGTYPE_INFORMATION
@ asMSGTYPE_INFORMATION
The message is informational only.
Definition: angelscript.h:430
asIScriptEngine
The engine interface.
Definition: angelscript.h:1102
asIScriptEngine::SetMessageCallback
virtual int SetMessageCallback(const asSFuncPtr &callback, void *obj, asDWORD callConv)=0
Sets a message callback that will receive compiler messages.
asCreateScriptEngine
AS_API asIScriptEngine * asCreateScriptEngine(asDWORD version=ANGELSCRIPT_VERSION)
Creates the script engine.
asSMessageInfo
Represents a compiler message.
Definition: angelscript.h:777
asSMessageInfo::col
int col
The column.
Definition: angelscript.h:783
asSMessageInfo::row
int row
The row number.
Definition: angelscript.h:781
asSMessageInfo::type
asEMsgType type
The type of message.
Definition: angelscript.h:785
asSMessageInfo::section
const char * section
The script section where the message is raised.
Definition: angelscript.h:779
asSMessageInfo::message
const char * message
The message text.
Definition: angelscript.h:787

# Loading and compiling scripts

To build a script module you first obtain a module from the engine, then add the script sections, and finally compile them. A compiled script module may be composed of one or more script sections, so your application may store each section in different files, or even generate them dynamically. It doesn't matter in which order the script sections are added to the module, as the compiler is able to resolve all names regardless of where they are declared in the script.

// Create a new script module
asIScriptModule *mod = engine->GetModule("module", asGM_ALWAYS_CREATE);
// Load and add the script sections to the module
string script;
LoadScriptFile("script.as", script);
mod->AddScriptSection("script.as", script.c_str());
// Build the module
int r = mod->Build();
if( r < 0 )
{
// The build failed. The message stream will have received
// compiler errors that shows what needs to be fixed
}
asGM_ALWAYS_CREATE
@ asGM_ALWAYS_CREATE
Always create a new module, discarding the existing one.
Definition: angelscript.h:534
asIScriptEngine::GetModule
virtual asIScriptModule * GetModule(const char *module, asEGMFlags flag=asGM_ONLY_IF_EXISTS)=0
Return an interface pointer to the module.
asIScriptModule
The interface to the script modules.
Definition: angelscript.h:2232
asIScriptModule::AddScriptSection
virtual int AddScriptSection(const char *name, const char *code, size_t codeLength=0, int lineOffset=0)=0
Add a script section for the next build.
asIScriptModule::Build
virtual int Build()=0
Build the previously added script sections.

AngelScript doesn't provide built-in functions for loading script files as most applications have their own way of loading files. However, it is quite easy to write your own routines for loading script files, for example:

// Load the entire script file into a string buffer
void LoadScriptFile(const char *fileName, string &script)
{
// Open the file in binary mode
FILE *f = fopen("test.as", "rb");
// Determine the size of the file
fseek(f, 0, SEEK_END);
int len = ftell(f);
fseek(f, 0, SEEK_SET);
// Load the entire file in one call
script.resize(len);
fread(&script[0], len, 1, f);
fclose(f);
}

As AngelScript doesn't load the files itself, it also doesn't have built-in support for including other files from within the script. However, if you look in the add-on directory, you'll find a CScriptBuilder class that provides this support and more. It is a helper class for loading files, perform a pre-processing pass, and then building the module. You can a see an example of how to use the script builder in Your first script.

See also
  Pre-compiled byte code