Skip to main content
GameDev.net gamedev.net
🔒 Locked

calling a C++ method bound from Lua with Luabind

Started by sp00n Sep 29, 2008 at 4:12 AM 1 replies 1.8k views
Original Post
sp00n
sp00n
Hi All, I have been trying to integrate Lua into a project for the first time, using Luabind. I have a quake-style console, to which I would like to redirect Lua's print output to. the initialization code is as follows:

module(L)
[
	class_<cConsole>("Console")
		.def("print", &cConsole::Print)
		.def("get" ,&cConsole::Get)
]

m_SubSystems->AddSubsystem(100, GFX()); // init GFX by calling macro GFX(),
					// which creates GFX and inits it if
					// it does not exist already, and 					
					//returns a pointer to the GFX class.

cConsole* conptr = GFX()->m_pConsole;
luabind::globals(L)["con"] = &conptr   // assert fails here

luaL_dostring(L, "print = function(s) con:print(s) end");
The code compiles, but when I execute I get a failed assertion: Assertion Failed, luabind.../policy.hpp Line 487 Expression: crep && "you are trying to use an unregistered type" Where am I going wrong? This seems such a simple task but I feel like I have been banging my head against a brick wall! :)
-= sp00n =-There are 10 kinds of people in this world: those who understand binary and those who don't.
Cygon
Cygon
You're passing a pointer to a pointer to your console in these lines:

cConsole* conptr = GFX()->m_pConsole;luabind::globals(L)["con"] = &conptr   // assert fails here


Try
cConsole &console = *GFX()->m_pConsole;luabind::globals(L)["con"] = console;


Haven't checked it out actually compiling something, but maybe one level of indirection (Console *) is okay.

If the problem persists, just report back, I'll be around ;)
Professional C++ and .NET developer trying to break into indie game development.
Follow my progress: http://blog.nuclex-games.com/ or Twitter - Topics: Ogre3D, Blender, game architecture tips & code snippets.
sp00n
sp00n
Hi Cygon,

Thanks very much for the help. The code you posted has certainly helped me out. The program now compiles and executes. However, whilst shutting down, when I execute lua_close(L), the GC tries to delete the Console object, causing problems, as it has already been deleted elsewhere. (At least I know that the binding is working now!:) )

Also, my efforts to redirect Lua's print() function have failed - the line
luaL_dostring(L, "print = function(s) con:print(s) end")
executes without returning an error, but calls to print() from Lua in my main game loop do not get passed to the console. I tried calling con:print('test') instead, but this results in LuaL_dostring returning 0, so something is not working properly there.
-= sp00n =-There are 10 kinds of people in this world: those who understand binary and those who don't.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.