reset cocos lua environment to support auto-updating game logic

reset cocos lua environment to support auto-updating game logic
0.0 0

   I implement game logic in lua language in my project. Now I meet some issues about update game logic.
   We want to update our lua files and game logic without forcing user to quit our games. 
   You know , in ios we could't restart our app directly ,and we don't want user to do boring "restart the app" operation. 
   Download and update files is easy, but how to update the game without quiting is a challenge.
   you know, after running old lua code, the lua stack is full of old global value/data/module(by "require"), simply executeScriptFile(newFilePath) is not a wise choice.
   so we have 3 ways:
      0. clear the old value in current lua environment.
          It's so messy and hard , and would bring many bugs.
          we need find all values and modules created by the code and clear them.
          we don't want to add any special rule or burden in programming too. 
      1. delete the old lua stack, create a new one and run the lua initialization/register code.
      2. some way to close cocos2dx engine and open a new one.
      I prefer the way 1(delete the old lua state), but cocos2dx doesn't support things like recreating a lua stack , supporting multiple lua stack .... etc
and there are still many script handlers(LUA_FUNCTION) in cocos2dx internally, if I delete the old lua stack , I should clear these handlers and take care of other related things too. 
      Before I start the job, I wonder is there anybody meet the same issue , so somebody could give me some suggestion to avoid wrong design.
      any resp will be appreciate.
      thanks  :)


I tried implement hot reload lua code before, It’s hard, very hard, I must say.

first thing need attention, like you said, registered script handlers, that cocos2dx keep reference them internally, if you really need to reload these lua functions, you should book these functions by yourself before registered, then update after code reloaded, cocos2dx keep all these script handlers(lua function) in a global table called “toluafix_refid_function_mapping”.

secondly, you should be carefull lua upvalues, they are also hard to update.

conclusion: if your requiremnts is restart game logic(initialization state), it’s easier to implement, meanwhile, you have to decide some special scripting rules like ‘no upvalues in script’.