🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

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

Started by
2 comments, last by Ironica 20 years, 11 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?
Advertisement
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>

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