🎉 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!

Load scripts without running them

Started by
0 comments, last by trolocsis 20 years ago
Hi! I would like to load a bunch of scripts when my game runs, keep a array of them and then when I need to, run them. I would like it to look something like this: struct Script { char name[100]; PointerToScript* p; }; void Init() { m_mainL = lua_open(); // this functions should be accessable from the later loaded // scripts lua_register(m_mainL, "somefunction1", myFuct1); lua_register(m_mainL, "somefunction2", myFunct2); } void LoadScript(char* name,char* path) { Script scr; strcpy(scr.name,name); scr.PointerToScript = LuaFunctionThatWouldLoadTheScript(m_mainL,path); m_scripts.add(scr); // m_scripts is a dynamic array of Script } void RunScript(char* name) { for(int i = 0; i < m_scripts.count(); i++) if(strcmp(m_scripts->name,name) == 0) LuaFuctionToRunTheScript(m_scripts->PointerToScript); } I've named the Lua fuctions I'm looking for with obvious names. Is something like this possible?
Advertisement
luaL_loadfile will load an arbitrary lua file. It doesn't 'execute' until a lua_pcall. The proper way to load files though is to:

luaL_loadfile() || lua_pcall()

for each lua file you want to load. You may want to have multiple virtual machines. if the script doesn't actually call the function within it, then this will work perfectly fine. The delayed execution works how you want it.

This topic is closed to new replies.

Advertisement