Public Group

# Luabind C++ issues

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

## Recommended Posts

Hi, I've searched both on Gamedev and google/bing and I can't figure out what's going wrong. I am trying to bind functions/classes to Lua and its just not working at all. It's extremely weird, because some cases work and some dont. The script I am testing consists of, which runs perfectly fine with just the uncommented source:
script = LuaScript()
script:getFilename()

if(state == NULL)
{
print("Error initializing LuaState (NULL lua_State**)");
return;
}

*state = lua_open();
if(*state == NULL)
{
print("Error initializing luaState");
return;
}

luabind::open(*state);
if(*state == NULL)
{
print("Error binding luaState");
return;
}

luabind::module(*state)
[

luabind::class_<LuaScript>("LuaScript")
.def(luabind::constructor<>())
.def("getFilename", &LuaScript::getFilename)

/* If I were to add:  .def("getPlayerTarget", &LuaScript::getPlayerTarget) the script errors out! */

/* Even if I add a completely new function not bound to a class, it STILL errors out and does not run the script! */

/* I assure you the functions are defined!  These functions worked before, and then I added some things (nothing to the existing functions) and came back to it and now this is just completely messed up */
];


Also.. if I add more functions, such as functions not part of classes, I can call them and they will work perfectly. However, if I touch ANY class related functions/properties - it breaks.

##### Share on other sites
It seems that the type of 'state' is lua_state**. Why didn't you declare it as lua_state*? Not only would that get rid of all the *, it's less error-prone.

Quote:
 Original post by CrazyfoolThese functions worked before, and then I added some things (nothing to the existing functions) and came back to it and now this is just completely messed up

##### Share on other sites
Thats the thing.. I don't know. I've obviously done something, but when I strip down everything to its minimum, it STILL doesn't work. But I DID find something that might be the issue:

I tried many combinations of functions, and it seems that only when I try binding a function (either a class function or a non-class function) that returns a class object. When I try returning a pointer to an Npc, it fails when I access any part of any object, even if I don't use that function!

Edit: For instance, binding this class, compiles but causes the script to not run, regardless of me using this function or not.

LuaScript* getActiveScript(){       return &luaManager.activeScript;}//... below is lua bindingluabind::def("getActiveScript", &getActiveScript)

The most baffling part is, I have a different project (though its compiled using the debug libraries as opposed to the release) and it works just fine. The EXACT same bindings (I utilize the same class/data for basic things) work perfectly.

(Note: about the lua_state**, it was for a function that I passed a pointer of a pointer when I was learning LuaBind - I'll remove that extra garbage).

##### Share on other sites
If it works in debug is it possible there are optimizations going on that mess with either the bind function or even the presence of the function?

Although I would assume that the compiler would not inline a function that you retrieve the address too, there may be a host of optimizations that potentially mess up the bind?

I am not intimately familiar with Lua, but I have worked with it enough to know that occasionally some bad inputs can cause the whole thing to go haywire and due to stack manipulation errors can expose themselves in all sorts of ways.

##### Share on other sites
Alright, would it be better than to NOT allow any direct interaction with objects (like, pointers to objects)? I am trying to learn how to best use Lua for games not so much for THIS game, but for the future or for the sake of learning it. So I have no current design plan for Lua, just that I'd like to make parts of the game available to those without access to the code (i.e., everyone but me).

For example:
Way of accessing object pointers
- - 1 - - Call a function that gets an Npc at location x,y (returns address)
- - 2 - - Call function to lower health on the Npc

Other way
- - 1 - - Call function to lower health on the Npc at location x,y (returns true/false on success/fail)

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

• 11
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633722
• Total Posts
3013540
×

## Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!