Advertisement

C++ and Lua Entity System Problem

Started by May 14, 2011 11:32 AM
2 comments, last by SimonForsman 13 years, 6 months ago
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?

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)
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!
Advertisement
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.

Hero of Allacrost - A free, open-source 2D RPG in development.
Latest release June, 2015 - GameDev annoucement


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.
[size="1"]I don't suffer from insanity, I'm enjoying every minute of it.
The voices in my head may not be real, but they have some good ideas!

This topic is closed to new replies.

Advertisement