0
\$\begingroup\$

i created tool that generate lua script on each level which contain lot function, and they named based on the object+id format. ex : trigger1(), etc; so every level may have same function name, what i need is call luaL_dofile on beginning level load.(+ registration ofcourse)

on game every level load i call do_file this lua script, so i can call it in the code. the thing is, when i try to change level to another level, i dont want close entire lua state and do rereigistration / initialization ,i just want close previous luaL_dofile and change it with new luaL_dofile on the next lua level script. since i want instaneous transition each level.

can anyone help me on this ?

\$\endgroup\$
0
\$\begingroup\$

You can just run the next file. You shouldn't lose your previous state or variables, but you might want to do some cleanup between.

However, what I'd suggest, is using different threads (which won't work like they might sound).

Basically, you've got your parent instance of a lua VM and you create one child thread for each level.

With some table magic, this also allows you to define globals in your parent VM, while the childs (i.e. the level scripts) will be able to access the globals, but they're not able to change anything outside their own scope.

Unfortunately, things changed for Lua 5.2, so I can't give you any snippet for the latest version.

For 5.1 you could do something like this:

LC = lua_newthread(L);
lua_newtable(LC);
lua_pushvalue(LC, -1);
lua_pushliteral(LC, "__index");
lua_pushvalue(LC, LUA_GLOBALSINDEX);
lua_settable(LC, -3);
lua_setmetatable(LC, -2);
lua_replace(LC, LUA_GLOBALSINDEX);
\$\endgroup\$
  • \$\begingroup\$ thanks man,i only have little knowledge on lua, as long im able call lua script from c ofcourse, so loading multiple file is ok then, the last script loaded will overwrite the same function ( ihope no mem issue due this when play lot consecutive level). for the moment ill go with multiple file solution. anyway im pretty curios about metatable you describe(like grouping?),i cant find benefit for my game because my level is called serially. but its worth to know this,now im trying learn more about lua for better understanding about this metatable :P. \$\endgroup\$ – rz_05 Feb 3 '15 at 15:46
  • \$\begingroup\$ @rz_05 Yes, stuff will be overwritten and you won't leak once garbage collection kicks in \$\endgroup\$ – Mario Feb 3 '15 at 18:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.