lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I am planning on using Lua to script the behavior of various entities in my game (characters, sectors, etc). Each entity should be able to update itself on a per-frame basis if its associated script supplies an update function, and should be able to respond to any number of arbitrary events. The set of events that an entity responds to should be defined solely by the presence of corresponding functions in the entity's associated script -- that is, a script that defines the functions evt_die, evt_runAway, and evt_collide will respond to the die, runAway, and collide events.

Each entity will have a script file associated with it that defines the various functions that the entities which employ the script file support. A given script file may define an arbitrary number of functions (an optional update function and an arbitrary number of evt_ functions) -- obviously, two different script files can (and most likely will) define some of the same functions. Moreover, multiple entities can opt to use the same script file (if there are three generic ogre enemies in a level, for example, they will all share the same behavior and therefore will all use the same script file.

I'm having trouble figuring out how to get this all working in Lua without creating a separate Lua state for each entity. I'd like all entities to operate within the same state for speed concerns. Each script file will be like a C++ class: it will declare instance variables that are used by each entity that uses that script file to define its behavior, and it will define functions that operate on that data. Each game entity will be like a C++ object -- it will have its own copies of the instance variables defined by its associated script file, and functions called on that object will manipulate the object's instance variables. But since multiple script files can define the same functions, and since multiple entities can use the same script file, I'm not sure how to keep all the data organized in one Lua state. Any thoughts? Should I be approaching this problem from a different direction?

Thanks,
Tim

-- 
Tim Conkling
tconkling@brown.edu