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(ANGELSCRIPT_VERSION); engine->SetMessageCallback(asFUNCTION(MessageCallback), 0, asCALL_CDECL);
// 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 }
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. With it the code for building a module looks like this:
CScriptBuilder builder; int r = builder.BuildScriptFromFile(engine, "module", "script.as"); if( r < 0 ) { // The build failed. The message stream will have received // compiler errors that shows what needs to be fixed }