Advertisement

Load scripts without running them

Started by June 29, 2004 03:26 PM
0 comments, last by trolocsis 20 years, 4 months 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?
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