Error calling C++ functions from Lua with LuaBind

Started by
13 comments, last by bobofjoe 14 years, 8 months ago
I'm tasked with implementing Lua into an existing C++ codebase. I've looked at Luna, Luabind, and tolua++ and I thought that Luabind was the most appealing choice of binding Lua and C++. After getting LuaBind all set up to compile and run, I'm having trouble getting it to work in the most basic sense. I have a ScriptingManager open up a LuaState, which is held throughout the life of the game. Later, once an object is created, I bind a very basic test function to Lua.
void testabc()
{
	return;
}

luabind::module(gScriptManager->GetState())
[
    luabind::def("testabc", &testabc)
];
Very basic, right? Well, I later have a script which merely has a function call to testabc, and is just as such: test.lua:
testabc()
This is the code that is supposed to run it, however I am getting an error code (supposedly a Yield from Lua).
void ScriptingManager::Run(const char* script)
{
	int ret = luaL_dofile(m_luaState, script);
	if(ret != 0)
	{
		if(!lua_isstring(m_luaState, lua_gettop(m_luaState)))
		{
			return;
			
		}

		std::string err = lua_tostring(m_luaState, lua_gettop(m_luaState));
		lua_pop(m_luaState, 1);
	}
	
}
The return value I am getting is 1, however the error message is the following:
Quote:void ð­ºtestabc()
That's it. Nothing descriptive, but just the function itself it seems. Can anyone help figure out what the problem is? [Edited by - Iced_Eagle on July 28, 2009 2:58:24 PM]
Advertisement
Well im not sure how lua bind works ... but with Luna all of my exposed functions return ints (like int main returning 1)... dont know if that helps, im not very familiar with luabind.

i just made a blog post about how i use lua... again... not sure how luabind works... but you may find some of the information useful.

------------------------------

redwoodpixel.com

Well, from what I looked at with Luna, you have to have specific function signatures (returning ints and taking the lua stack IIRC?). With LuaBind, you don't have to change the signatures, but instead just have to define the functions (which makes it work better with an existing codebase IMO), which is where I might be screwing up somehow...

I ran my project in Decoda (Lua IDE/Debugger) and it throws an exception when it tries to call that function, so there is definitely something wrong in setting up that function.
Quote:Original post by Iced_Eagle
Well, from what I looked at with Luna, you have to have specific function signatures (returning ints and taking the lua stack IIRC?). With LuaBind, you don't have to change the signatures, but instead just have to define the functions (which makes it work better with an existing codebase IMO), which is where I might be screwing up somehow...

I ran my project in Decoda (Lua IDE/Debugger) and it throws an exception when it tries to call that function, so there is definitely something wrong in setting up that function.


Afraid I cant help you then. I actually like the fact that you need specific functions to get exposed... Makes things very clear (to me and my dev buddies at least.) Of course I never really gave LuaBind a fair assessment.

After looking at a tutorial you need to do 2 open's for luabind...

// Create a new lua state
lua_State *myLuaState = lua_open();

// Connect LuaBind to this lua state
luabind::open(myLuaState);


.... etc

// Add our function to the state's global scope
luabind::module(myLuaState) [
luabind::def("print_hello", print_hello)
];

You have that stuff right?

------------------------------

redwoodpixel.com

I actually missed the luabind::open somehow (personally, I think LuaBind's documentation is fairly poor :\). I put that right after I init Lua and load the basic libs, but it still has no change (I did a full rebuild hoping it would work, but no dice and yes I made sure that everything is getting init'ed before I define the funcs or call the script :) ).

Error message is still the same and when I debug the script, it still throws an assert, but nothing descriptive...

*Edit* BTW, thanks for your help thus far :) I'm sure it will end up being something silly, but hopefully others in the future can read and learn from my mistakes!
No problem... as you can tell from my blog post.. im trying to learn everything i can about lua for some big implementation stuff going on in my project.

Ah... one other thing is to make sure the versions line up... I used luna for 4.x in a 5.1 lua build... and it totally sucked.

------------------------------

redwoodpixel.com

Hehe, I did a quick sanity check by looking online for info (LuaBind only says it's for 5.0 so I freaked for a second), but starting with their 0.8 release, it supports Lua 5.1...

I'll try emailing the LuaBind email list and see if I can get any results or else I may switch to another binding library.
http://www.gamedev.net/community/forums/topic.asp?topic_id=202942

that post is a bit old but has some sound logic in it.

------------------------------

redwoodpixel.com

I changed the order so it loads the script, defines the func, and then runs it to no avail. I've even tried doing a vanilla "dostring" with also no results.

I'm not sure that his final solution really works for me since I'm doing a global function and not using member functions with structs and classes (yet). :
I just emailed the luabind group, so I'm hoping within a few days someone can give me something to try or look at!

I wish I could do more debugging on my own, however since I'm not familiar with Lua, I'm not sure how the stack should look before/after it runs and such, and why the only "error" I get is the function prototype. Hell, I even thought that maybe the C++ name-mangling was an issue, but extern "C" didn't seem to help either!
Anyone else have any thoughts?

The LuaBind email list wasn't very helpful either, so I'm thinking of switching binding libraries...

This topic is closed to new replies.

Advertisement