Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
How do I use dataSaver? — Gideros Forum

How do I use dataSaver?

ZizanymanZizanyman Member
edited June 2015 in General questions
I am using dataSaver in my project, and I am confused about how it saves files. When I run this line of code, would it automatically create a file, or do I need to create one manually?
dataSaver.saveValue("|D|hiscore", hiscore)
local hiscore = dataSaver.loadValue("|D|hiscore")
hiscoreText:setText(hiscore)
The reason I am confused is because when I run my game in gideros player, it will remember the hi score in my code until I close the player. It even will remember the value throughout scenes. But once I close the player and open it again, it thinks that the hiscore is nil.

Comments

  • SinisterSoftSinisterSoft Maintainer
    You need to save it before exit. It doesn't automatucally save.
    Coder, video game industry veteran (since the '80s, ❤'s assembler), arrested - never convicted hacker (in the '90s), dad of five, he/him (if that even matters!).
    https://deluxepixel.com
  • jeromegsqjeromegsq Member
    edited June 2015
    In my init file i have this :
    local fileLoad = io.open("|D|save","r")
    if not fileLoad then
           dataSaver.saveValue("highscore", 0)
     
    	print("no file, it was created")
    end
    and after in my code :

    Save :
    local score = 10
    dataSaver.saveValue("highscore", score )
    Load :
    local res = dataSaver.loadValue("highscore")
    Voila :)

  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    initially you could also do something like:
    local res = dataSaver.loadValue("highscore")
    if res == nil then
        dataSaver.saveValue("highscore", 0)
    end
    And you can also use something like this in your main file, so you won't have to do write operation on each update (cause write operations are really costly)
    stage:addEventListener(Event.APPLICATION_SUSPEND, function()
        dataSaver.saveValue("highscore", score )
    end)
    stage:addEventListener(Event.APPLICATION_EXIT, function()
        dataSaver.saveValue("highscore", score )
    end)
    So it would only save score upon suspending or exiting app
    +1 -1 (+2 / -0 )Share on Facebook
  • @jeromehsq and @ar2sawseen, I tried both methods, and for some reason, neither worked. I also added this line of code to the "if res == nil then" statement:
    print ("no file")
    and it doesn't print this out when I start the game. So it seems to see a highscore file, but that it is 0.

    Also, if I use io.open, do I need to have an actual file called "save" in my project directory, or does it handle that part for me when I use dataSaver.saveValue?

    Thanks! :)
  • Never mind! I just forgot to add:
    hiscore = res
    :D
    Thanks for all your help!
Sign In or Register to comment.