lua-users home
lua-l archive

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

If I were to do this, how would I handle table variable accesses from within the table scripts?

For example, say I create the following script file:

MySprite = {}
MySprite.state = 0
MySprite.speed = 3
MySprite.update = function()
 MySprite.state = MySprite.state + 1
 MySprite.speed = MySprite.speed * 2

And several entities use this script file, so I create a new copy of this table for each of them. I'll probably be generating names for these new tables based on entity ID or something like that, so now I have the following tables: entity1, entity2, entity3. I call entity1.update(), entity2.update(), and entity3.update() from my engine every frame to update these entities. But nothing will happen, since each entity will be updating MySprite.state and MySprite.speed rather than their own variables, right?

I'm sure I"m missing an obvious way to do this, or perhaps I'm simply attacking the problem from the wrong angle.


> From: Alex Sandro Queiroz e Silva <>
> Date: 2003/08/11 Mon PM 02:44:11 EDT
> To: Lua list <>
> Subject: Re: Game entity scripting strategies?
> Hallo,
> Tim Conkling wrote:
> >[...]
> >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?
> >
>     Why don't you put all the functions and variables of each single 
> entity in a table?
> -Alex

Tim Conkling