Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat
saving datas with HTML5 export - Gideros Forum

saving datas with HTML5 export

Hello, I'm back with a question that I'm afraid to already know the answer...
Is there a tricky way to save small data (a highscore for example) locally with an html5 app? or do I need to connect my appp to a DB and blabla bla...
thanks !
My meditation plan :
SinisterSoft: “ I don't create classes that much - classes are good for making things simpler but imho for frame rate they are also death by a thousand cuts.”
Totebo: “ Best quote ever.”
🤔

Comments

  • hgy29hgy29 Maintainer
    Accepted Answer
    Yes you can, except if the user is browsing in private mode or similar scenario. |D| folder in gideros apps is backed with a browser side DB (indexdb)

    Likes: jimlev

    +1 -1 (+1 / -0 ) Share on Facebook
  • jimlevjimlev Member
    Nice ! Not sure that I understood every details but, now, as I know it's possible, I'm gonna make some tests and tries.

    Likes: MoKaLux

    My meditation plan :
    SinisterSoft: “ I don't create classes that much - classes are good for making things simpler but imho for frame rate they are also death by a thousand cuts.”
    Totebo: “ Best quote ever.”
    🤔
    +1 -1 (+1 / -0 ) Share on Facebook
  • MoKaLuxMoKaLux Member
    edited October 1
    there is this piece of code:
    require "json"
     
    function saveData(filepath, value)
    	local contents = json.encode(value)
    	local file = io.open("|D|"..filepath, "w") -- create file
    	file:write(contents) -- save json string in file
    	io.close(file)
    end
     
    function getData(filepath)
    	local value
    	local file = io.open("|D|"..filepath, "r")
    	if file then
    		local contents = file:read("*a") -- read contents
    		value = json.decode(contents) -- decode json
    		io.close(file)
    	end
     
    	return value
    end
    And then at the start of your app:
    	g_myprefs = "|D|yourfilename.txt"
    -- init prefs
    local mydata = getData(g_myprefs) -- try to read information from file
    -- if no prefs file, create it
    if not mydata then
    	mydata = {}
    	mydata.g_language = g_language
    	mydata.g_iswarnings = g_iswarnings
    	...
    	saveData(g_myprefs, mydata) -- create file and save datas
    else
    	g_language = mydata.g_language
    	g_iswarnings = mydata.g_iswarnings
    	...
    	if g_iswarnings == nil then g_iswarnings = true end
    	if g_isextrawarnings == nil then g_isextrawarnings = true end
    end
    To save your data anywhere in your code you can do:
    -- save prefs
    function mySavePrefs()
    	mydata = {}
    	mydata.g_language = g_language
    	mydata.g_iswarnings = g_iswarnings
    	...
    	saveData(g_myprefs, mydata) -- save new datas
    end

    Likes: jimlev, plicatibu

    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
    +1 -1 (+2 / -0 ) Share on Facebook
  • jimlevjimlev Member
    thanks a lot ! I was on the road to that result using an old piece of code of my own.
    I also found an example on the forum on that page: http://forum.giderosmobile.com/discussion/6105/html5-showcase-alpha-discussion/p3

    I have a question. My own example is quite close to your snippet. But it doesn't use JSON formating. In my old project, I was using a ".txt" file to save the content of a table (the code I found is from a 6y old project). Do you use JSON for convenience or it is a necessity today with Gideros local saving system ?
    My meditation plan :
    SinisterSoft: “ I don't create classes that much - classes are good for making things simpler but imho for frame rate they are also death by a thousand cuts.”
    Totebo: “ Best quote ever.”
    🤔
  • MoKaLuxMoKaLux Member
    edited October 1
    I don't use json, but I think you must include the plugin when you export your app/game otherwise that won't work. You need json for this line of code:
    local contents = json.encode(value)
    I save the data as plain text but you can change the extension to anything (eg: yourfilename.dat)
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
Sign In or Register to comment.