Sign in to follow this  
brwarner

Using Lua Tables to store Behavior

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this