LUA Table Pointer question

Hello All!

I have a situation where I need to “switch” information in a table based on what the user is doing.

I recall something about being able to have an empty table def and being able to point to the other table somehow but I can’t find it in the docs.

I have a Current table that starts out empty.

When they enter room 1, the Current table should be whatever is in room 1.

When they enter room 2, the Current table should switch to whatever is in room 2

I have separate tables with what is in each room which varies. 

How to I say Current = Room1 when they are in room 1? or Current = Room2 when they are in room 2?

Thanks!
stu

hi Stu,

you could accomplish this in two ways:

  1. either store the room table using a property on table A, or
  2. set table A’s metatable using __index to refer to the room table

the former is a classic pattern for OOP, but to make lookup a little nicer, you could set methods on table A which refer to the room table.

the latter simplifies all table lookup because you don’t have to know ahead of time what’s in the room table. that is, failing to find a property on table A, Lua will automatically search __index, which is the room table.
this sounds close to what you were referring to “being able to point to another table”.

cheers,
dmc

hi Stu,

you could accomplish this in two ways:

  1. either store the room table using a property on table A, or
  2. set table A’s metatable using __index to refer to the room table

the former is a classic pattern for OOP, but to make lookup a little nicer, you could set methods on table A which refer to the room table.

the latter simplifies all table lookup because you don’t have to know ahead of time what’s in the room table. that is, failing to find a property on table A, Lua will automatically search __index, which is the room table.
this sounds close to what you were referring to “being able to point to another table”.

cheers,
dmc