Navigation

    Fuze Arena Logo
    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Popular
    • Users
    • Groups
    • Help
    • Discord

    Persistent Data

    Coding
    11
    33
    4312
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • pianofire
      pianofire Fuze Team last edited by

      No problem. Feel free to improve on it too!

      1 Reply Last reply Reply Quote 1
      • PB____
        PB____ last edited by

        I've noticed that you don't declare your function scoped variables.

        It is likely that someone uses a variable called i in their for loop. If they do this in the global scope and use getValue within that for loop, the i value of the global varible i would be changed by getValue. This could be fixed as follows:

        // Get the value for the specified key
        function getValue( key )
            var i = 0
            var result = "Undefined"
            var found = false
            while i < len(store) and !found loop
                if store[i].key == key then
                    found = true
                    result = store[i].value
                else
                    i += 1
                endif
            repeat
            if found then
                if store[i].type == "int" then
                    result = int( result )
                endif
                if store[i].type == "str" then
                    result = str( result )
                endif
                if store[i].type == "float" then
                    result = float( result )
                endif
            endif
        return result
        

        I now var is not a keyword (you could use int in stead), it's a personal preference, mainly since boolean found = false doesn't appear to be technically correct either.

        1 Reply Last reply Reply Quote 1
        • LinkCraft
          LinkCraft F last edited by

          Wait I’m confused is this so you can access data from any program

          1 Reply Last reply Reply Quote 0
          • PB____
            PB____ last edited by

            @LinkCraft02 I think you might have misread "a way of persisting data across programs" in stead of " a way of persisting data across program runs" ?

            I think you can only access the file that belongs to project you are running.

            1 Reply Last reply Reply Quote 1
            • pianofire
              pianofire Fuze Team last edited by

              I have updated this in line with the @PB____ recommendations.

              Also it now supports vector types

              1 Reply Last reply Reply Quote 0
              • P
                password1 @pianofire last edited by

                @pianofire please post a usage example.

                pianofire 1 Reply Last reply Reply Quote 0
                • pianofire
                  pianofire Fuze Team @password1 last edited by

                  There are some examples in the download:

                  // 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
                  
                  // Get values from store
                  test1 = getValue( "test1" )
                  test2 = getValue( "test2" )
                  test3 = getValue( "test3" )
                  test4 = getValue( "test4" )
                  
                  // Show current values
                  finished = false
                  while !finished loop
                      clear()
                      c = controls( 0 )
                      printAt( 0, 0, "Press X to exit" )
                      printAt( 0, 1, "test1 : ", test1 )
                      printAt( 0, 2, "test2 : ", test2 )
                      printAt( 0, 3, "test3 : ", test3 )
                      printAt( 0, 4, "test4 : ", test4 )
                      finished = c.x
                      update()
                  repeat
                  
                  // Change values
                  test1 = test1 + 1
                  test2 = test2 + "wibble"
                  test3 = test3 * 2
                  test4 = { test4.x + 1, test4.y * 2, test4.z - 1, test4.w / 2 }
                  
                  // Save current values to store
                  setValue( "test1", test1, "int" )
                  setValue( "test2", test2, "str"  )
                  setValue( "test3", test3, "float" )
                  setValue( "test4", test4, "vector" )
                  
                  saveStore() // save store to file
                  
                  1 Reply Last reply Reply Quote 0
                  • PB____
                    PB____ last edited by

                    @pianofire I can't seem to download from the code NDZQHDNDNN anymore.
                    I did evolve the code to my preferences anyway (since you're in my friends-list, you could check it out on NDSF6DND1Z, it's part of my template project that I intend to use for my new projects)

                    1 Reply Last reply Reply Quote 0
                    • pianofire
                      pianofire Fuze Team last edited by

                      @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)

                      PickleCatStars 1 Reply Last reply Reply Quote 0
                      • PB____
                        PB____ last edited by

                        @pianofire Thanks :-) I hope the work pays off. Since it's my template project, I hope that the work I put in it, will benefit all my future projects, so as a net result, it saves me work overall :)
                        And of course I'm OK with it if you can improve the file storage example based on my code :-). I'd like to clarify my thought process about how I start the file:

                        // file format (to enable compatibility check)
                        file_writeLine(file, "PB")
                        // reserved for updates to the file format:
                        file_writeLine(file, 0)
                        

                        Currently projects can only have one file, and it's not possible to share files across different projects. But still I thought it was a good idea to mark a file format at the start of the file, just in case the mentioned limitations change in the future. Of course this means that I hope that other people won't mark their files with the same opening as I did, to prevent potential errors in the future :-)

                        The second line (0) is to mark a version of the format. In case people download an updated version of a program, and I need to check how to handle the format of an older file (in case the download mechanism doesn't include saved files in the future, since it has been suggested that that behavior is a bug).

                        1 Reply Last reply Reply Quote 1
                        • waldron
                          waldron F last edited by

                          could this be simplified to save a sprites location using maps

                          pianofire 1 Reply Last reply Reply Quote 0
                          • pianofire
                            pianofire Fuze Team @waldron last edited by

                            @waldron I am not sure what you mean. It can already save vectors (such as a sprite position). There is one in the example above

                            waldron 1 Reply Last reply Reply Quote 0
                            • waldron
                              waldron F @pianofire last edited by

                              @pianofire of course, il wait till i'm off to have a proper look

                              1 Reply Last reply Reply Quote 1
                              • PickleCatStars
                                PickleCatStars F @pianofire last edited by

                                @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! :/

                                1 Reply Last reply Reply Quote 0
                                • pianofire
                                  pianofire Fuze Team last edited by

                                  Sorry I accidentally unshared some stuff the new code is: XP6L3MNDNN

                                  1 Reply Last reply Reply Quote 0
                                  • PickleCatStars
                                    PickleCatStars F last edited by

                                    Thankyou very much :)

                                    1 Reply Last reply Reply Quote 0
                                    • pianofire
                                      pianofire Fuze Team last edited by pianofire

                                      I have been asked to give some more information about how to use this and how it works so first how to use it:

                                      1. Download the program (XP6L3MNDNN)
                                      2. Copy the functions into your program (Line 60 onwards)
                                      3. In your global variables declare
                                      store = []
                                      
                                      1. 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
                                      
                                      1. 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)

                                      1. 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" )
                                      
                                      1. Save the store to a file:
                                      saveStore() // save store to file
                                      

                                      And that's it!

                                      1 Reply Last reply Reply Quote 5
                                      • pianofire
                                        pianofire Fuze Team last edited by pianofire

                                        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.

                                        Persistent Data.png

                                        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.

                                        1 Reply Last reply Reply Quote 8
                                        • SteveZX81
                                          SteveZX81 F last edited by

                                          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!

                                          1 Reply Last reply Reply Quote 6
                                          • N
                                            nintisonfire last edited by

                                            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.

                                            pianofire 1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post