Advertisement

Lua Bind problem [Solved]

Started by January 21, 2004 10:45 PM
1 comment, last by _the_phantom_ 20 years, 10 months ago
I've got a slight problem with LuaBind, and lack of experiance with the system has got me kinda stumped. I've a bit of program which needs to be changed often and so i thought, aha! perfect for some scripting So, i set about recrafting it in Lua5.0 and LuaBind and everything compiles fine however i'm getting a runtime error and i dont understand why, i've probably missed something simple knowing my luck :o I'm setting up Lua and LuaBind thusly

void Init_Lua(lua_State * &luaVM)
	{
		luaVM = lua_open();
		lua_baselibopen(luaVM);
		luabind::open(luaVM);

		// register in the function to create the new instances of the classes

		luabind::function(luaVM,"NewHLBased",&ParserMakers::NewHLBased);
		luabind::function(luaVM,"NewQuakeWorldBased",&ParserMakers::NewQuakeWorldBased);
		luabind::function(luaVM,"NewQuake2Based",&ParserMakers::NewQuake2Based);
		luabind::function(luaVM,"NewQuake3Based",&ParserMakers::NewQuake3Based);
		luabind::function(luaVM,"NewBF1942Based",&ParserMakers::NewBF1942Based);
		luabind::function(luaVM,"NewAAOBased",&ParserMakers::NewAAOBased);
		luabind::function(luaVM,"NewStandardBased",&ParserMakers::NewStandardBased);
		luabind::function(luaVM,"NewAltStandardBased",&ParserMakers::NewAltStandardBased);
		luabind::function(luaVM,"NewUnrealBased",&ParserMakers::NewUnrealBased);

		luabind::class_&ltutility::serverdetails>(luaVM,"serverdetails");
		luabind::class_&ltMicroThreadSystem::MicroThread>(luaVM,"Microthread");
		luabind::class_&ltMicroThreadSystem::MicroThreadMgr>(luaVM,"MicrothreadMgr");
		luabind::class_&ltParsers::parsermarkers>(luaVM,"parsermarkers");
                
		MakeParser = luabind::functor&ltMicroThreadSystem::MicroThread*>(luaVM,"GetParser");	// Get a function object of the Parser maker (function object is declared extern in a header file and included in this file as part of the ParserMaker namespace


		lua_dofile(luaVM, "pingservers.lua");
	}


All the above function pointers point to functions which exist within the cpp file and everything seems to go off without a problem, lua inits (i see some debug txt i put at the top of the file) with no parsing errors.

Then i come to use the functor I bound above :
   
MicroThreadSystem::t_MTPTR NewParser(utility::serverdetails &details, MicroThreadSystem::MicroThreadMgr * parent)
{
	MicroThreadSystem::t_MTPTR parser;
//	parser.reset(ParserMakers::NewHLBased(details, parent));

	parser.reset(static_cast&ltMicroThreadSystem::MicroThread*>(ParserMakers::MakeParser(details,parent)));
	return parser;
}


Its during the various calls to functions in the LuaBind Functor object i get an exception thrown :
   
typename default_policy::template generate_converter<Ret, lua_to_cpp>::type converter;

					m_called = true;
					lua_State* L = m_func->lua_state();
					detail::stack_pop p(L, 1); // pop the return value


					// get the function

					m_func->pushvalue();

					push_args_from_tuple<1>::apply(L, m_args);
					if (lua_pcall(L, boost::tuples::length<Tuple>::value, 1, 0))
					{ 
#ifndef LUABIND_NO_EXCEPTIONS
						throw luabind::error(L);  // error comes from there

#else
						error_callback_fun e = detail::error_callback::get().err;
						if (e) e(L);
	
						assert(0 && "the lua function threw an error and exceptions are disabled."
							"if you want to handle this error use luabind::set_error_callback()");
						std::terminate();
#endif
					}

I've done the normal debug stuff, the tuple contains a valid copy of the data I want to be passed to Lua and the Lua state is set fine.

Extra info that might be needed :
MicroThreadSystem::t_MTPTR is a Boost::shared_ptr to a MicroThreadSystem::MicroThread object, base class which all the other parser object derive from.
MicroThreadSystem::MicroThreadMgr controls a set of MicroThreads
utility::serverdetails is a struct containing a string and 2 ints
Parsers:: parsermarkers is a struct containing std::strings only

Small sample of the Lua Script its calling :
   
function GetParser(details,parent)
	print("Entering GetParser")
	markers = parsermarkers();
        if details.gameid == GAME_UNREAL
	or details.gameid == GAME_UT
	or details.gameid == GAME_UT2K3
	or details.gameid == GAME_U2 
	then
		markers.name = "\\player_"
		markers.nameend = "\\frags_"
		markers.score1 = "\\frags_"
		markers.score1end = "\\ping_"
		return NewUnrealBased(details,parent,markers)
	end
end
Note: "Entering GetParser" is never displayed [edited by - _the_phantom_ on January 22, 2004 12:50:22 PM]
meh, i think i''ve found part of the problem, i was trying to bind the function before the file had been loaded, not my best moment that

Still getting an exception but my test txt is now being printed so i must have another error, if anyone has any ideas why what could be causing the error at the same place then i''d appricate it if uwould share, however, i''ll be doing my own bug hunting later today

*goes to sleep a bit happier*
Advertisement
right, well, i''ve fixed my problem

I moved the functor binding below the Lua script load firstly, then added a bunch of readwrite defs to all the members of parsemarker struct and a readonly def to the gamedetails one.
However, that didnt fix my problem, so on a whim i added a do nuffin constructor to both the struct and as a def for the parsermaker struct and its all working fine now and doing just what i hoped it would do :D

This topic is closed to new replies.

Advertisement