Sign in to follow this  
Martin Perry

Lua - data persistance

Recommended Posts

Martin Perry    1553

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

Share this post


Link to post
Share on other sites
Ku134    176

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. 

Share this post


Link to post
Share on other sites
ProtectedMode    1339

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.

Share this post


Link to post
Share on other sites
Martin Perry    1553

 

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

Share this post


Link to post
Share on other sites
Vortez    2714

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.

Edited by Vortez

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this