Lua - data persistance

Started by
5 comments, last by Martin Perry 10 years ago

I have Lua script, that runs in every frame of simulation (eg. is restarted) and is called from C program.

I need to have some data persistent over time and change them from Lua script. How can I achieve this ? Persistent data are stored in table with certain name, so I can access this table from C application.

How can I store this table after script finish and restore it before it will run again ? Because with run again, values are set back to its default values.

Thank you

Advertisement

Can't you just have the script return the table after it's done so you can store it in some variable in your c program. Then all you have to do is just pass the table back into the script next time you run it.

Yes.. that was my thought.. but how to get table with "unknown" content to C ? Serialization seems not so fast solution and lua_topointer is more for debugging

Yes.. that was my thought.. but how to get table with "unknown" content to C ? Serialization seems not so fast solution and lua_topointer is more for debugging

A better question you could ask is why do you create the context again every frame and execute a file? You usually should create a context and load a file. After that you can call some (fixed) callback function every frame, this way the data is remembered.

Yes.. that was my thought.. but how to get table with "unknown" content to C ? Serialization seems not so fast solution and lua_topointer is more for debugging

A better question you could ask is why do you create the context again every frame and execute a file? You usually should create a context and load a file. After that you can call some (fixed) callback function every frame, this way the data is remembered.

Well.. that is good point.. I didnt really thought about it that way... I will give it a try

You could have a look at my lua script interface code, which get and set some globals strings, but you can use it for integer and boolean too. The python code is not very good but the lua one works 100%. I did stuffs that way since using stack parameters caused some strange bugs that only happened once randomly, but using a global instead solved the issue.

I solved it with tip from ProtectedMode. Thank you. Easy and elegant solution, that never crossed my mind

This topic is closed to new replies.

Advertisement