Persistent Data
-
@pianofire of course, il wait till i'm off to have a proper look
-
@pianofire said in Persistent Data:
@PB____ Sorry my fault I unshared it so the code changed. The new code is FPH33MNDNN
I have had a quick look at what you have done and it looks very impressive. That looks like an incredible amount of work! I will update my example using your code if that is OK (with credit of course)
Project not found! :/
-
Sorry I accidentally unshared some stuff the new code is: XP6L3MNDNN
-
Thankyou very much :)
-
I have been asked to give some more information about how to use this and how it works so first how to use it:
- Download the program (XP6L3MNDNN)
- Copy the functions into your program (Line 60 onwards)
- In your global variables declare
store = []
- Initialise any variables that you want to save e.g.
// Initialize values or load from the store if !loadStore() then // init on first run setValue( "test1", 99, "int" ) setValue( "test2", "", "str" ) setValue( "test3", pi, "float" ) setValue( "test4", { 0, 1, 2, 3 }, "vector" ) endif
- Read the current values from the store e.g.
// Get values from store test1 = getValue( "test1" ) test2 = getValue( "test2" ) test3 = getValue( "test3" ) test4 = getValue( "test4" )
When you want to save the current values to a file (or just at the end of your program)
- Set the current values in the store:
// Save current values to store setValue( "test1", test1, "int" ) setValue( "test2", test2, "str" ) setValue( "test3", test3, "float" ) setValue( "test4", test4, "vector" )
- Save the store to a file:
saveStore() // save store to file
And that's it!
-
OK so now how it works:
store is a list of structs with a key, value and type. This is a sort of dictionary in that the key is unique and can be used to retrieve the value. It only does a sequential search so will get quite slow if there are a lot of entries.
The type is needed because the store actually keeps everything as strings and converts to and from the actual data type.
So store acts as a backing store which mirrors the types and values of variables that you want to persist. There are load and save store methods that either save the current state of these values or restore a previously saved state.
The values are then stored in the file as variable length text records something like this:
4 5 test13 int3 1005 test23 str6 wibble5 test35 float8 6.2831865 test46 vector42 { 1.000000, 2.000000, 1.000000, 1.500000 }
The first number is the number of entries in the file. Then the next one is the length of the variable name. Next the name itself, then the length of the type, the type, length of the value, the value and so on.
The clearStore method empties the backing store and saves the empty store to the file.
-
Well, I don't know about anyone else but this helped me a ton and now I actually understand it and have added a saved high score to my game.
Thank you so much! -
Thanks for this. Got a high score that saves. Forgive my ignorance. Are "setValue" and "getValue" new inbuilt functions? They don't turn change colour when typed into the code editor.
-
To use this, you need to copy Pianofires code into your project (I think it's game id XP6L3MNDNN, or it might be FPH33MNDNN as mentioned in the initial post).
-
@nintisonfire No they are user defined functions in this project
-
The code for this has changed to NG25XMNDNN
-
this should be part of the FUZE default library.