Sign in to follow this  

Lua help

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

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.


//Register
lua_atpanic(L, func_panic);
.
.
// the function
int func_panic(lus_State*L)
{
// handle error
return 0;
}

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this