AngelScript
|
Path: /sdk/add_on/clib/
This add-on defines a pure C interface, that can be used in those applications that do not understand C++ code but do understand C, e.g. Delphi, Java, and D.
To compile the AngelScript C library, you need to compile the library source files in sdk/angelscript/source together with the source files in sdk/add-on/clib, and link them as a shared dynamic library. In the application that will use the AngelScript C library, you'll include the angelscript_c.h
header file, instead of the ordinary angelscript.h
header file. After that you can use the library much the same way that it's used in C++.
To find the name of the C functions to call, you normally take the corresponding interface method and give a prefix according to the following table:
interface | prefix |
asIScriptEngine | asEngine_ |
asIScriptModule | asModule_ |
asIScriptContext | asContext_ |
asIScriptGeneric | asGeneric_ |
asIScriptObject | asObject_ |
asIObjectType | asObjectType_ |
asIScriptFunction | asFunction_ |
All interface methods take the interface pointer as the first parameter when in the C function format, the rest of the parameters are the same as in the C++ interface. There are a few exceptions though, e.g. all parameters that take an asSFuncPtr
take a normal function pointer in the C function format.
Example:
#include <stdio.h> #include <assert.h> #include "angelscript_c.h" void MessageCallback(asSMessageInfo *msg, void *); void PrintSomething(); int main(int argc, char **argv) { int r = 0; // Create and initialize the script engine asIScriptEngine *engine = asCreateScriptEngine(ANGELSCRIPT_VERSION); r = asEngine_SetMessageCallback(engine, (asFUNCTION_t)MessageCallback, 0, asCALL_CDECL); assert( r >= 0 ); r = asEngine_RegisterGlobalFunction(engine, "void print()", (asFUNCTION_t)PrintSomething, asCALL_CDECL); assert( r >= 0 ); // Execute a simple script r = asEngine_ExecuteString(engine, 0, "print()", 0, 0); if( r != asEXECUTION_FINISHED ) { printf("Something wen't wrong with the execution\n"); } else { printf("The script was executed successfully\n"); } // Release the script engine asEngine_Release(engine); return r; } void MessageCallback(asSMessageInfo *msg, void *) { const char *msgType = 0; if( msg->type == 0 ) msgType = "Error "; if( msg->type == 1 ) msgType = "Warning"; if( msg->type == 2 ) msgType = "Info "; printf("%s (%d, %d) : %s : %s\n", msg->section, msg->row, msg->col, msgType, msg->message); } void PrintSomething() { printf("Called from the script\n"); }