lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I am hoping that someone on this list could give me some suggestions
with a problem I'm having.  I have a singleton in my application that
contains an Execute() method that will attempt to run the scripts it
is passed.  First, it attempts to load a module named base.lua which
contains (among others) a function:

function Execute(script)
     local f = loadfile(script)()

Every script file that is called is wrapped in a simple function call
"function main()" with no arguments.  I'll list the C++ below, but the
error I keep getting is LUA_ERRRUN and it is not executing the
function in base.lua.

bool ScriptHandler::Execute(string script)
		// load the indicated module
		string path;
		path = GLOBALS->basepath();
		path += "modules\\base.lua";
		luaL_loadfile (ScriptMgr->GetState(), path.c_str());

		// Execute the script
		if (lua_isfunction (ScriptMgr->GetState(), -1))
			lua_pcall (ScriptMgr->GetState(), 0, 1, 0);

		// push functions and arguments
		lua_getglobal(ScriptMgr->GetState(), "execute");
		lua_pushstring(ScriptMgr->GetState(), script.c_str());

		// do the call (1 arguments, 1 result)
		int retcode = lua_pcall(ScriptMgr->GetState(), 1, 1, 0);
		if (retcode) {
			string error;
			error = "ScriptMgr::Execute() -> ";
			error += "Error running function main(): ";
			error += script;
			throw ScriptError(error, retcode);
		return true;
	catch(ScriptError err)
		return false;

My goal is for every single script file in my application to have
access to the functions in base.lua without having to use "require" or
"loadlib" or anything of that sort.  At the same time, I would like to
be able to have a single path through which all scripts are executed,
since some scripts will likely be written by other individuals at a
later date. Perhaps someone could review the code below and point me
in the right direction?

Thanks in advance for any pointers you may be able to provide.

Jason Murdick