lua-users home
lua-l archive

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

Hi Tim,

Tim Conkling wrote:

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.
   You could write:

update = funtion(self)
  self.state = self.state + 1
  self.speed = self.speed * 2

And then add this function to the entity table. If you call your function like entity1:update(), this way a invisible argument called 'self', a reference to the table itself, will be passed to that function.