LUA Hot Reload memory data is lost after overloading
In Ksframework, all UI Lua scripts can be overloaded. Some of the memory data in the script will be lost after overloading, such as:
-- record How many times a UI interface has been opened Local 0 function Uilogin:onopen () 1 End return Uilogin
As above, each time the script reload, the Opencount variable is reinitialized to 0, which is inconsistent with the actual requirements.
How do I resolve memory loss?
For this reason, the cookie mechanism is introduced in Ksframework, which saves the state value to avoid being affected by the script overload, and the above code uses the cookie mechanism:
functionUilogin:onopen ()LocalOpencount= Cookie.get ('Uilogin.opencount') if notOpencount ThenOpencount=0 EndOpencount= Opencount +1Cookie.set ('Uilogin.opencount', Opencount)EndreturnUilogin
What is a cookie?
Cookies are common in HTTP development, where the data stored on the user's local terminal in order to identify the user is called a browser cache.
HTTP is a stateless protocol, such as the development of HTTP Web site in PHP language, the developer of the code changes just refresh the browser can immediately see their own changes, without the process of start-stop operation, development is very convenient. This is one of the reasons why the PHP language is very hot.
Introducing a cookie mechanism
Ksframework uses Lua for UI development, supports hot overloading to quickly modify code, and the most important consideration for thermal overloading of LUA code is the loss of Lua running memory state.
Therefore, the Ksframework reference introduces the cookie mechanism of the HTTP domain into game development, and all local state values are stored in the cookie, separating the logic from the state. The process of writing code, which is a logical process, does not affect the current state.
Specific implementation of cookies
The specific implementation of the cookie is very simple, it is just a hashtable, get/set operation, get or set arbitrary object:
Figure: Code implementation of cookies
Using the example
An example of a QuickStart random announcement: Every time you reload a LUA script, it will be re-randomly. Is there any way to let this example, for the first time, load a random number?
This number is saved to a cookie. After we have modified the script logic, we do the Lua script overload, which is when we get back the random values from the cookie.
-- local rand = cookie.get ( " uibillboard.randomnumber " ) Span style= "color: #0000ff;" >if not rand then Rand = math.random (1 , 3 ) Cookie.set ( " uibillboard.randomnumber " ) Span style= "color: #0000ff;" >end
Simply put-- Save the state information in a cookie and separate it from the logical code .
Of course, the cookie mentioned here is different from the HTTP cookie, only the name borrowed to solve a similar problem.
Copyright notice
Text/public kelly[mr-kelly] (Jane book author) email: [email protected]
Original link: http://www.jianshu.com/p/eebd5cfce87f
Copyright is owned by the author, please contact the author to obtain authorization, and Mark "Jane book author".
Ksframework Series
GitHub Address: Https://github.com/mr-kelly/KSFramework
Welcome to GitHub to mention issues.
Ksframework (Integrated u3d hot Reload) README
Ksframework:unity3d Development Framework Quick Start
Kengine Planning Guide: Editing and compiling the configuration table
Kengine:unity3d resource Packaging, loading, debugging monitoring
Ksframework FAQ: Lua scripting Hot reload, memory state data loss?