Jump to content
  • Advertisement
Sign in to follow this  
ZMaster

Lua: pointer to a table into another script

This topic is 4676 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I have objects in my game defined by lua scripts. Each Character for example has it's own script file with a definition of a table called "Character", which owns certain properties of this character (Name, Type, Experience, etc.). Each object also has some functions declared in the script that are triggered on events like when the character is upgraded. What I want is, that each character can read/write the propertiers of another character from within the script. Let's assume I want to change the experience of a Character when another is upgraded, I want something like this: function onUpgrade ( ) OtherCharacter = GetCharacter("Name", "Type"); OtherCharacter.experience = 0; end From inside my game C++ code I can search for an object by it's name and type, so I could supply a function like "GetCharacter(name, type)", which could return me a handle to the object and thus to it's lua script and state. But I have no idea how I can get a pointer to a table from one lua script into another lua script. Is that possible? Any other ideas? TIA, ZMaster

Share this post


Link to post
Share on other sites
Advertisement
table(function...) in lua is just a name, it hold a "real object", like point in cpp.
so you can import a table's addresss into cpp, and export it to lua late.

like this:

--in charA.lua
character = {}

//in cpp
class character
{
public:
//you need some way export this function to script
//see luabind or tolua
__EXPORT_TO_SCRIPT int get_tbl() { return addr_of_tbl; }

private:

//you need some way get the address of a table and save it
//like this
//lua_pushstring("tbl", L);
//lua_gettable(-1, L);
//int addr = luaL_ref(L, LUA_REGISTRYINDEX);
int addr_of_tbl;
};




--in charB.lua
funcion on_even()
other = get_char("name", "type")
--this is the table of A
tbl_of_A = other.get_tbl()
end

in this case, you must define a global script, make sure script dofile(charA.lua) and script dofile(charB.lua).

try it :)

Share this post


Link to post
Share on other sites
Thank you for your reply.

I assume your solution requires one lua state for both scripts.
My design requires one lua state for every script. Does that work with your idea?

If it works: How do I get the table address in the second script? If'd just do lua_pushnumber(L, addr_of_table), I would just push the address as an value to the variable that takes the return value of get_tbl(), right? And if I'd push the address onto the lua registry I wouldn't be able to access it from within the lua script, because the registry is only available through the lua C api, isn't it?
So how would you do that?

Share this post


Link to post
Share on other sites

yeah, maybe you are right. I can't test it because my computer haven't install lua, and I haven't use it for long time.

anyway, it's just an idea.

how about lua_rawgeti() function:

lua_rawgeti(L, LUA_REGISTRYINDEX, addr_of_tbl)

//now, this table at the top of stack
//we can use it call a lua function, table is the argument
lua_pcall(L, "func")

in lua(another lua file):

function func(tbl)
--this is the table
end


or

//in some function to export lua

int get_tbl(lua_State *L)
{
//get the addr_of_tbl by some way
lua_rawgeti(L, LUA_REGISTRYINDEX, addr_of_tbl)

//now, top of stack is this table
//and just return it
return 1;
}

if you use tolua, it's simple to define a "get_tbl" function

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!