• 9
• 16
• 15
• 12
• 9

# lua_State and multiple entities

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

## Recommended Posts

hi

In the my Lua code i have some global variables which is unique per entity.

my Lua script is like this:
 a = 1 function func1(Entity n) do a = a+1 end 

variable "a" have unique value per object.

can i copy luabind tables or i must create a lua_State for each instance of entity?

##### Share on other sites
The question is a bit ambiguous but I will go with it. (If my assumptions are wrong I blame it to you )

I suppose what you want to do is load entities from files and want to have separate tables for each entities. Creating a lua state for each entity is not a good idea.

"i have some global variables which is unique per entity"
This is self-contradicting, because you say you have global variables which are at the same time instance variables, but I think can see what you mean.
In lua the global scope is just a table and can easily be changed to any table using setfenv(0, <table>). This way you can create a table for your entity and set it as the environment table so that every global variable is created in that table instead of the actual global space. Once the loading finishes you switch back to the original environment table.

Here is a code snipped that should do the job. Note that it could contain errors, becuase I haven't written in lua for months and I can't compile Lua code atm (btw your code won't compile either).
 function loadEntity(filename) local entity = {}; --//We create the table here entity._G = _G; --//Make sure we have a reference to the original global space setfenv(0, entity); --//Change the environment. Note: all functions ang global variables are now gone setmetatable(entity, --//this gets us back the funcs and global vars { index = entity._G} ); --//from with the script file dofile(filename); --// Load the entity setmetatable(entity) --//Remove the metatable from entity setfenv(0, entity._G) --//Restore the global space return entity; end; ----------------------------------------------------------------------- --//It works as follows ent1 = loadEntity("<entity_script_filename.lua>"); ent1:func1(); ent1:func1(); print("The value of ent1.a is " .. ent1.a); --//You can load multiple entities from the same file, using this function ent2 = loadEntity("<entity_script_filename.lua>"); ent3 = loadEntity("<entity_script_filename.lua>"); --//and it will work fine with each entity properly initialized and having its own variables --//but if you plan to load multiple instances from one and the same script you might consider --//replacing "dofile" with "loadfile" and keeping a reference to the function returned 

Your code would look like this now
 a = 1 --// a is stored in the newly created table function func1(Entity n) --//func1 is also stored in that table n.a = n.a+1 --//You still have to index n from within functions (I think), otherwise when the --//scope changes it will try to access the global a from the original global space end 

I hope this is what you were asking forand that it will help. Bare in mind that it's up to you to test it, because I can't.