Game Development Reference
In-Depth Information
" y = me.y; \n" \
" color = me.color; \n" \
"} \n" \
" \n" \
;
The lifecycle of the script system requires two things: an AngelScript engine and an AngelScript context, which
you'll use for executing scripts. An optional but important step is to set a callback function that AngelScript uses
for reporting errors, warnings, and other information. The current one just writes the message to the Android log.
But a better system would pass the message to the GUI. Since the user should find out about script errors as soon as
possible, a good idea would be to build the code in the script editor and inform of any errors then. Listing 12-15 shows
the implementation of the script engine's constructor, destructor, and message callback functions.
Listing 12-15. Script Engine Constructor and Destructor
void MessageCallback(const asSMessageInfo *msg, void *param)
{
const char* type = "UNKNOWN";
if(msg->type == asMSGTYPE_ERROR)
type = "ERROR";
else if(msg->type == asMSGTYPE_WARNING)
type = "WARNING";
else if(msg->type == asMSGTYPE_INFORMATION)
type = "INFO";
LOGE("%s (%d, %d) : %s : %s\n",
msg->section, msg->row, msg->col,
type, msg->message);
}
GPTScriptEngine:: GPTScriptEngine()
{
m_pScriptEngine=asCreateScriptEngine(ANGELSCRIPT_VERSION);
m_pScriptContext = m_pScriptEngine->CreateContext();
m_pScriptEngine->SetMessageCallback(
asFUNCTION(MessageCallback), 0, asCALL_CDECL);
}
GPTScriptEngine::~GPTScriptEngine()
{
m_pScriptContext->Release();
m_pScriptContext = NULL;
m_pScriptEngine->Release();
m_pScriptEngine = NULL;
}
The next member function to be implemented is AddGameObject . This method receives all the properties of a
game object and the script it should run. The function builds the script into a module and creates a new entry in the
m_scriptModules vector. In order to give the scripted game object the same values as the native object, a call is made
to the PreInit script function in the module. And now that the new object is in a container, the system keeps track of
it and later calls the OnUpdate script function on it, as Listing 12-16 demonstrates.
 
Search WWH ::




Custom Search