Game Development Reference
In-Depth Information
asIScriptModule *pScriptModule =
m_pScriptEngine->GetModule(modName.c_str(),
asGM_ALWAYS_CREATE);
// Load and add the script sections to the module
pScriptModule->AddScriptSection(
"mainSection", scriptCode);
pScriptModule->AddScriptSection(
"classesSection", classesCode);
pScriptModule->AddScriptSection(
"globalObjectsSection", globalObjectsCode);
pScriptModule->AddScriptSection(
"globalFunctionsSection", globalFunctionsCode);
// Build the module
int retCode = pScriptModule->Build();
if( retCode < 0 )
{
// Build failed, delete the module and return NULL
m_pScriptEngine->DiscardModule(modName.c_str());
return NULL;
}
return pScriptModule;
}
The BuildScriptModule function creates an empty module, adds the object's source code, and also adds
the common code you defined previously. It compiles the combined script and returns an AngelScript module if
successful.
So far, the functions you've implemented don't make the game objects come to life; this will happen when you
update the scripting system. Each object has a script function called OnUpdate that dictates its behavior at every step
of the game loop. The scripting engine iterates through the collection of game objects and calls that function on each
module, as shown in Listing 12-17.
Listing 12-17. The Script Engine's Update Function
void GPTScriptEngine::Update(float deltaTime)
{
// Call the "OnUpdate" script function on every module
for (size_t i = 0; i < m_scriptModules.size(); ++i)
{
asIScriptModule* pScriptModule =
m_scriptModules[i].pModule;
asIScriptFunction* pOnUpdateFunction =
pScriptModule->GetFunctionByDecl(
"void OnUpdate(float dt)"
);
 
Search WWH ::




Custom Search