# C++ and Lua Entity System Problem

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

## Recommended Posts

Hi.

I have an idea for an entity system involving lua and c++. Each entity will have its own script, each script will hold 4 functions that the C++ side will call when needed.

The problem i have is that i can execute the script to load the function into the Lua instance but if i execute multiple scripts containing the same function names i will need to differentiate between them somehow. Does Lua have some sort of namespace functionality or something similar, if so, how can i use it?

##### Share on other sites

Hi.

I have an idea for an entity system involving lua and c++. Each entity will have its own script, each script will hold 4 functions that the C++ side will call when needed.

The problem i have is that i can execute the script to load the function into the Lua instance but if i execute multiple scripts containing the same function names i will need to differentiate between them somehow. Does Lua have some sort of namespace functionality or something similar, if so, how can i use it?

One easy solution is to use one lua_State per script , the overhead for this should be quite negligable as long as you don't go overboard with it, the main drawback is that you have to pass the data through c++ if you wish to get it from one lua instance to another (Depending on your architecture this may or may not be an issue)

The lua equivalence to namespaces are tables (lua uses tables for everything it seems) so you could also do:

function somenamespace.myFunction(...)
...
end

allthough this would have to be done in each lua script then.

You also do it automatically when you load the scripts by first loading the script into a buffer (a string or character array) and then replace any occurance of "function xyz" with function "customnamespace.xyz" before you pass it to the lua instance. (Global variables will still be a mess though and you have to make sure any calls to functions you've placed in a table is modified aswell)

##### Share on other sites
We had this problem with our maps, which were all stored in their own Lua file and had their own name. I believe we used some sort of metatable namespace mechanism to prevent the naming conflict problem. I didn't research/write this myself so I'm not sure exactly how this work, but the top of every map file contains the following:

 local ns = {} setmetatable(ns, {__index = _G}) river_access_cave = ns; setfenv(1, ns); 

Where "river_access_cave" is the name of the map. Maybe you could use this as a starting point for solving your problem and research what these calls do.

##### Share on other sites

We had this problem with our maps, which were all stored in their own Lua file and had their own name. I believe we used some sort of metatable namespace mechanism to prevent the naming conflict problem. I didn't research/write this myself so I'm not sure exactly how this work, but the top of every map file contains the following:

 local ns = {} setmetatable(ns, {__index = _G}) river_access_cave = ns; setfenv(1, ns); 

Where "river_access_cave" is the name of the map. Maybe you could use this as a starting point for solving your problem and research what these calls do.

Basically what that appears to be doing is change the enviroment a function runs in, its a fairly neat solution.

http://www.lua.org/pil/14.3.html gives more detailed information.

1. 1
2. 2
3. 3
4. 4
Rutin
16
5. 5

• 12
• 9
• 12
• 37
• 12
• ### Forum Statistics

• Total Topics
631419
• Total Posts
2999981
×