Sign in to follow this  

[LUA] Using a variable from another state

This topic is 3487 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 tried to explain it with words, but I just can't seem to find the right ones, so let's say I have this c++ code :
luaL_dostring(lua1, "temp = 12");
... and another lua_State* called "lua2". Is there a way to copy "temp" from "lua1" to "lua2" ? Of course, I could just :
lua_getglobal(lua1, "temp");
int temp = lua_tonumber(lua1, -1);
lua_pop(lua1, 1);
lua_pushnumber(lua2, temp);
lua_setglobal(lua2, "temp");
... but what about tables ? Thanks in advance !

Share this post


Link to post
Share on other sites
You'd have to write a table copying function in C++ / C.

It'd have to iterate through the fields and repopulate them in the new state. You'd have to account for cases where a table is field of a table - and cycles.

Share this post


Link to post
Share on other sites
Lua has the lua_xmove function, but it will only work between lua_State*s created from the same global state (ie, between Lua threads). If you really have created two lua_State* by calling lua_newstate twice, then they share no information and you need to do all the copying yourself.

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
Typically, you don't want multiple Lua states. Can you explain why you need multiple states?

I'm using LUA for two separated environments :
- interface scripts
- spell (gameplay) scripts
I don't want gameplay functions (such as UnitDamage()) to be available in the interface scripts.
Spell scripts are also used for storing some custom (= unpredictable) data stored in a table, which is then needed in the interface to fill a formated string with the values it contains.
I know there are ways to solve the problem by doing things differently.
Yet I want to do it that way :)

Quote:
Original post by Balaam
You'd have to write a table copying function in C++ / C.

It'd have to iterate through the fields and repopulate them in the new state. You'd have to account for cases where a table is field of a table - and cycles.

That's the way I do it for now, but I was wondering if there was a cleaner way.

Quote:
Original post by hh10k
Lua has the lua_xmove function, but it will only work between lua_State*s created from the same global state (ie, between Lua threads). If you really have created two lua_State* by calling lua_newstate twice, then they share no information and you need to do all the copying yourself.

I'm calling luaL_newstate twice. From what I've read in the lua doc, I can't use threads for what I'm doing.
So I think I'll have to stick with my copy function :)

Thanks for your help, all of you.

Share this post


Link to post
Share on other sites
You could place the functions in two different environments. That way, you could restrict the flow of communication, having no reference to the spell functions from the interface, but you could have the interface functions/data accessible in the spell environment. I haven't done much with environments, but I think they can be used to create the kind of partition you are describing.

Other links (if you keep reading the above you will get to them)
Non global environments.
Packages.

Effectively (I think [grin]) you are trying to create two packages, and then hide one from the other.

Share this post


Link to post
Share on other sites

This topic is 3487 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.

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