• Advertisement
Sign in to follow this  

c++ Lua wrapper help

This topic is 1315 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

Hello,
anyone can suggest me a simple lua wrapper with clean code?
 
I want to call from c++ lua functions but I dont really get it..
 
I only know a little LUA and C++.
 
Example lua function I have:
 
Get the level:
function Lv(a)
     local lv = GetChaAttr( a , ATTR_LV )
     return lv 
end 
 
Get life points:
function Lp(a)
     local lp = GetChaAttr( a , ATTR_LP )
     return lp 
end
 
How can I create now a "GetChaAttr" function in c++ which fetch the player level/life points?
 
ATTR_LV = to get level
ATTR_LP = to get life points
 
Any idea which lua wrapper would be the best for me?

Share this post


Link to post
Share on other sites
Advertisement
There are a number of different libraries that can make binding C++ and lua much easier. This page lists many of them under the "Binding C/C++ with Lua" section.  A popular one is LuaBind.  It's very easy to set up the bindings, but it requires you to also link in the boost library.   I currently use a library called LuaBridge.  It's also really easy to use, but it does not require boost.
 
Using LuaBridge, your C++ code for exporting the GetChaAttr from C++ to lua might look something like this:
getGlobalNamespace(L)
	.addFunction("GetChaAttr", &GetChaAttr);
.endNamespace();

where "L" is your lua state object.  The parameters and return type of the function are automatically determined.  The code for LuaBind would look similar.

 

Share this post


Link to post
Share on other sites

LuaBridge looks good. Thank you. Im going to try it out.

 

I got a question..

If I want to run lua functions parallel do I have to load the files + functions every time a new user connects to the game?

Edited by Daneos

Share this post


Link to post
Share on other sites

Some shameless advertising :P

 

I wrote a tutorial with my limited knowledge. Maybe it will help you http://zwodahs.github.io/tutorial/lua/. If you don't want to read the tutorial, you can just check out my code from https://github.com/ZwodahS/lua-examples. 

Let me know if there are anything missing =). 
 

I looked at a few of the wrappers and didn't like them. Writing your own wrapper isn't that hard and you get full control over them. =)

Share this post


Link to post
Share on other sites

If I want to run lua functions parallel do I have to load the files + functions every time a new user connects to the game?

 

There are a couple different approaches for dealing with this. One way is to do as you say and create a new lua state with lua_open for each thread and reload your lua files for each new state.  Using this method, however, global lua data cannot be shared between lua states.  So if you need to access any global data, it will need to be exposed through a C++ function, which you can protect with a lock if necessary (if it's read only, you won't need any locks).

 

Another method is described here. This uses lua_newthread instead of lua_open to create a child state that can access global data. However this may have some performance issues as it could be locking more than you need.

 

Or you may want to try one of the lua threading libraries listed here like LuaLanes.

Edited by megadan

Share this post


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

  • Advertisement