# [LUA] Using a variable from another state

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

## 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");


##### Share on other sites
Typically, you don't want multiple Lua states. Can you explain why you need multiple states?

##### 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 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 on other sites
Quote:
 Original post by rip-offTypically, 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 BalaamYou'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 hh10kLua 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 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.

1. 1
2. 2
3. 3
4. 4
Rutin
18
5. 5

• 13
• 14
• 9
• 9
• 9
• ### Forum Statistics

• Total Topics
632926
• Total Posts
3009240
• ### Who's Online (See full list)

There are no registered users currently online

×