Advertisement

lua_dofile(l, lua_tostring(l, 1));

Started by July 30, 2003 01:01 AM
2 comments, last by Ironica 21 years, 3 months ago
Hey, I just got caught by a weird problem. Let me explain... My program opens a Lua script called "lua/Config.lua", and that contains some settings for the program. One of the options in that file is the Lua script to load after opening libraries: init = "lua/init.lua" If my program can''t find that setting, it will load the default file (which is lua/init.lua anyway).

const char *lua_initfile;
lua_getglobal(l, "init");
if(lua_isstring(l, 1)){
	lua_initfile = lua_tostring(l, 1);
	cout <<lua_initfile; // Make sure it reads ok

}else{
	lua_initfile = "lua/init.lua";
}
openstdlibs(l);
if(lua_dofile(l, lua_initfile)) cout <<"Error!";
Now if lua/Config.lua doesn''t contain the init option, works perfectly. But if it does, it outputs that "Error!". The filename is definately typed correctly in lua/Config.lua, why won''t it run the script? When it outputs the setting, just to make sure it''s correct, it does output perfectly fine! When the init setting = lua/init.lua, both blocks in the if statement should result in lua_initfile pointing to "lua/init.lua", which they definately do (as the test shows me). I really can''t see the difference, anyone else see it?
I''m not sure, but my guess is you need to remove the global "init" from the stack via lua_pop.

Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>

Advertisement
The string that lua gave you might no longer be valid because of garbage collection. Copy the string to a buffer outside of the Lua state, or don''t extract the string until right before you use it. It looks like you''re getting the string, then loading the libaries, then trying to use the string. After loading the libraries, the string might not be valid any more because Lua could have invalidated it during garbage collection. Check the value of the string right before you use it.
Worked! Thanks, hehe. Don''t know why I didn''t think of that =/

This topic is closed to new replies.

Advertisement