Jump to content
  • Advertisement
Sign in to follow this  
Lith

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement

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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!