Lua help

Started by
3 comments, last by alich 19 years, 1 month ago
I have a Lua interpreter up and running from my C++ application, and was wondering if there's a way to retrieve Lua's error messages. Right now they just print to stdout or stderr, but I'd like to print them on my in-game console. Also, is this the simplest way to get a global value from Lua to C++?

	Real GetReal( lua_State* lua, const Char* szVarName )
	{
		lua_pushstring(lua, szVarName);
		lua_gettable(lua, LUA_GLOBALSINDEX);
		Real rValue = lua_tonumber(lua, 1);
		lua_pop(lua,1);
		return rValue;
	}
This works for now, but it seems there should be a simpler way. Thanks for any help.
Advertisement
If I remember correctly there's a function called _ALERT (or something like that) that gets called when an error happens.. Of course, I could be completely wrong since I don't have any experience about it myself.
Ad: Ancamnia
No, you were right. _ALERT works great! Thanks!
I think the use of _ALERT is depreciated...

The manual has a section on error handling, and you should write a nice "do script" function with P-calls aanyways, something like this:

////////////////////////////////////////////////////////////////////////////////// my_dofile: a better replacement for lua_dofile that handles// errors more gracefully//int my_dofile(lua_State* L, int nresults, std::string& name) {	// load the script	int rc = luaL_loadfile(L, name.c_str());	if ( rc ) 		return rc;	// there was a compile-time error. it's on the top of the stack	// there were no compilation errors. run the script	// if there is a runtime error it is on the top of the stack	return lua_pcall(L, 0, nresults, 0);}


Then I use that function my higher level ScriptManager code:

int ScriptManager::doScript(std::string& filename, int numResults/* = 0*/) {	lua_State* L = luaUniverse; // luaUniverse is a member variable.			int err = my_dofile(luaUniverse, numResults, filename);	if (err) {		std::string errMsg("Lua error: ");		errMsg += lua_tostring(L, -1);		lua_pop(L, 1);		Ogre::LogManager::getSingleton().logMessage(errMsg);//GLApp.error( errMsg );	}	return err;		}


There any error message is logged to Ogre's log.
the other way is to register the panic funtion, something like this will work. The panic function will be called if an error occurs outside the protected env, this may occur in various places depending on your implementation.

//Registerlua_atpanic(L, func_panic);..// the functionint func_panic(lus_State*L){// handle error		return 0;}

This topic is closed to new replies.

Advertisement