Game Development Reference
In-Depth Information
Adding data members
To create an action, we'll pass three functions that the action will use for the initialization,
updating, and cleanup. Additional information such as the name of the action and a
user-
Data
variable, used for passing information to each callback function, is passed in during
the construction time.
Note
Moving our systems away from global data and into instanced object-oriented patterns re-
quires each instance of an object to store its own data. As our
Action
class is generic, we
use a custom data member, which is
userData
, to store action-specific information.
Whenever a callback function for the action is executed, the same
userData
table passed
in during the construction time will be passed into each function. The update callback will
receive an additional
deltaTimeInMillis
parameter in order to perform any time spe-
cific update logic.
To flush out the
Action
class' constructor function, we'll store each of the callback func-
tions as well as initialize some common data members:
Action.lua
:
function Action.new(name, initializeFunction,
updateFunction,
cleanUpFunction, userData)
local action = {};
-- The Action's data members.
action.cleanUpFunction_ = cleanUpFunction;
action.initializeFunction_ = initializeFunction;
action.updateFunction_ = updateFunction;
action.name_ = name or "";
action.status_ = Action.Status.UNINITIALIZED;
action.type_ = Action.Type;
action.userData_ = userData;