Sign in to follow this  
matches81

LUA 5.1, new require problem: Forcing a reload?

Recommended Posts

Hello there again and sry for bothering you so soon again, but I am facing another problem: Before using LUA´s require command to load other LUA source files I was easily able to reload my scripts on run-time of the game: I just stored a vector of loaded files in my LUA-"interpreter" class, and loaded the files again on demand, which replaced the version that had been loaded before with the new one. Now with require that won´t work anymore, because require stores loaded files in package.loaded to avoid loading files more than once (just as I do in my class) and refuses to load files again. Now, according to the LUA Reference manual, I should be able to set package.loaded[fileName] to nil in order to force a reload. There are problems with that: When I loaded 'foo.lua' which requires 'a.lua', 'b.lua' and 'c.lua', my class only stores 'foo.lua' in its vector, not knowing that 'a.lua', 'b.lua' and 'c.lua' were loaded by require. So, setting package.loaded['foo.lua'] to nil will force a reload of 'foo.lua' but the other files won´t be loaded again. Therefore iterating over my vector with loaded filenames and setting package.loaded to nil for every filename in there won´t suffice. My second idea was to just set everything in package.loaded to nil and therefore force a reload for every file I pass in after that. This is the code that should achieve this:
lua_pushstring(this->m_pLUAState, "package");
lua_gettable(this->m_pLUAState, LUA_GLOBALSINDEX); // global package should be on stack top
lua_pushstring(this->m_pLUAState, "loaded");
lua_gettable(this->m_pLUAState, -2); //package.loaded should be on stack top now
int size = luaL_getn(this->m_pLUAState, -1);	//size should contain size of package.loaded
for(int i = 1; i<=size; i++)
{
	lua_pushnil(this->m_pLUAState);
	lua_rawseti(this->m_pLUAState, -2, i);
}
lua_pop(this->m_pLUAState, 2);
however, luaL_getn(...) returns 0, so I don´t set anything to nil. Why does luaL_getn return 0? How can I clear package.loaded correctly from C++ to force a reload for every source file I have? Should I implement my own "require"-like function in my LUA-interpreter class and use that to have all loaded source files in the interpreter´s vector?

Share this post


Link to post
Share on other sites

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