Skip to main content
gamedev.net
🔒 Locked

Using Lua Tables to store Behavior

Started by brwarner Jul 2, 2008 at 3:24 PM 5 replies 1.1k views
Original Post
brwarner
brwarner
I am trying to code a system in which game objects behavior can be dictated by lua scripts. The setup I wanted is where each object has an associated lua file which would look something like this:

OBJECT.OnPickup = function()
{
   ...
}
OBJECT.OnUse = function()
{
   ...
}


I've been looking at lots of lua documentation and I know how to get functions from a lua table, however the problem I don't know how to solve is that all the tables would have the same name and would be global, so how would I reference any of them individually, unless there is some way to store them in C code for later use.
Mike nl
Mike nl
Can't you give each object instance it's own lua_State? That way each has it's own global table and such.

Alternatively, you may check if it's possible to change the global table's metatable, notably the "index" metamethod.
Million-to-one chances occur nine times out of ten!
brwarner
brwarner
I wasn't sure if it was good lua practise to make so many lua_States, but I'll go with that idea then.
ddn3
ddn3
You can get the objects by name if they are global, someone asked a similar question about accesing member functions. I'll dig up the post. Bascially there are lua c functions to find a table by name and once you have that, the rest is ezy.

-ddn
brwarner
brwarner
I know that I was just saying they all have the name OBJECT, so how would lua distinquish?
Sneftel
Sneftel
It would be very odd to name them all OBJECT. More likely would be something like this:

objects = {    {        OnPickup = function()            ...        end        OnUse = function()            ...        end    },    {        OnPickup = function()            ...        end        OnUse = function()            ...        end    },}

In this instance, each object would be a separate entry in the objects array.

Alternatively, you can use a bit of Lua syntactic sugar and make OBJECT into a function which takes a table as its single argument:

OBJECT {    OnPickup = function()        ...    end    OnUse = function()        ...    end}OBJECT {    OnPickup = function()        ...    end    OnUse = function()        ...    end}

There's plenty of ways to do it that don't require reinventing the syntax.

BTW, it is generally _not_ considered good practice to have a whole bunch of lua_States. Certainly Lua has many language features to help you avoid that eventuality.
brwarner
brwarner
Well I got it working with many lua_States, but you said as I originally guessed - so I came up with a better idea.

I will use my original scheme, each object will load a file, each file having a table called OBJECT in it with behavior functions, however - before loading the file I may be able to push something onto the stack to put the table into another table to avoid naming collisions of a single lua state.

EDIT: Final Solution
Every time an object is created, in C++ a lua table is put onto the stack with the name OBJECT. The file is then run via luaL_dofile, then since the table is still on the top of the stack the function lua_setglobal is used to change its name to bh_#, where # is a unique id generated for each object, thus allowing the use of a single lua state.

[Edited by - brwarner on July 3, 2008 4:05:55 PM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.