Game Development Reference
In-Depth Information
print(
Overwriting enemy actor; id =
.. actorId);
end
self._enemies[actorId] = scriptObject;
-- set up some sample game data
scriptObject.maxHitPoints = 3;
scriptObject.hitPoints = scriptObject.maxHitPoints;
-- create the teapot brain
local brain = nil;
if (TEAPOT_BRAIN) then
brain = TEAPOT_BRAIN:Create({_teapot = scriptObject});
if (not brain:Init()) then
print(
Failed to initialize brain
);
brain = nil;
end
end
-- set up the state machine
scriptObject.stateMachine =
TeapotStateMachine:Create({_teapot = scriptObject, _brain = brain});
-- set the initial state
scriptObject.stateMachine:SetState(PatrolState);
-- increment the enemy count and create the enemy processes if necessary
if (self._enemyProcesses == nil) then
self:_CreateEnemyProcesses();
end
-- make sure the UI is up to date
self:UpdateUi();
end
The scriptObject parameter is a table that contains an __object pointer back to
the C++ BaseScriptComponent object (see Chapter 12 for details).
The first block checks to see if the enemy has already been added to the map. If it
has, the code simply overwrites it. The next block sets up some variables on the
enemy object. Keep in mind that this only sets the variables on the Lua table wrap-
ping the C++ object, so these variables won
t be available in C++.
The next block of code creates the brain for the teapot. It looks at the global
TEAPOT_BRAIN constant and instantiates an object of that type, passing in the script
object to the constructor. TEAPOT_BRAIN is defined at the top of this file:
'
TEAPOT_BRAIN = DecisionTreeBrain;
Search WWH ::




Custom Search