Jump to content
  • Advertisement
Sign in to follow this  

LUA 5.1, new require problem: Forcing a reload?

This topic is 4314 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

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_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
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!