Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
value updates only when i restart the game | dataSaver — Gideros Forum

value updates only when i restart the game | dataSaver

Recently I made a game with "easy", "normal", "hard" modes. You can choose any mode on the menu.lua page(which is first scene loading in main.lua). When you end the game for example in "easy" mode, this happens:
1) in game.lua "onExitBegin" listener
easyVal = math.max(easyVal, times)
DataSaver:saveValue("easy", easyVal)
Note: easyVal is global value declared in menu.lua via
easyVal = dataSave:loadValue("easy")
2) then go the the menu page in menu.lua easyVal is declared in Menu:init() and again here it is printed

Thus highscore was updated, the score doesn't change after exiting game.lua and updating easyVal. Can you please point me to a solution?

Comments

  • MoKaLuxMoKaLux Member
    edited March 2022 Accepted Answer
    This is my setup:
    - the data save class (I put it in its own file, those are declared as global)
    require "json"
     
    function saveData(filepath, value)
    	local contents = json.encode(value)
    	local file = io.open(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(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
    - then in init I declare my global variables
    g_configfilepath = "|D|myprefs.txt"
    g_language = application:getLanguage()
    g_omodelpath = nil
    ...
    - then before the game starts I try to read the data (I put it in main.lua before scene manager)
    function myLoadPrefs(xconfigfilepath)
    	local mydata = getData(xconfigfilepath) -- try to read information from file
    	if not mydata then -- if no prefs file, create it
    		mydata = {}
    		mydata.g_language = g_language
    		mydata.g_omodelpath = g_omodelpath
    ...
    		saveData(g_configfilepath, mydata) -- create file and save datas
    	else -- prefs file exists, use it!
    		g_language = mydata.g_language
    		g_omodelpath = mydata.g_omodelpath
    ...
    	end
    end
    - then I load the data by calling the function myLoadPrefs
    -- let's load
    myLoadPrefs(g_configfilepath) -- load prefs
    - then there is the scene manager (also in main.lua)
    scenemanager = SceneManager.new(
    	{
    		["menu"] = Menu,
    		["thestage"] = TheStage,
    	}
    )
    scenemanager:changeScene("menu")
    stage:addChild(scenemanager)
    - then anywhere in the game (game, menu, ...) I can save the new data with this line
    		mySavePrefs(g_configfilepath)
    - and the mySavePrefs function (please note it is a global function, I put it just under the myLoadPrefs function in main.lua
    function mySavePrefs(xconfigfilepath)
    	local mydata = {} -- clear the table
    	mydata.g_language = g_language
    	mydata.g_omodelpath = g_omodelpath
    ...
    	saveData(xconfigfilepath, mydata) -- save new data
    end
    Ouf! :) hope this helps!?

    See also https://wiki.gideros.rocks/index.php/Start_Here#Save.2Fread_data_persistently

    Likes: definisto

    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
    +1 -1 (+1 / -0 )Share on Facebook
  • MoKaLux said:

    This is my setup:
    - the data save class (I put it in its own file, those are declared as global)

    require "json"
     
    function saveData(filepath, value)
    	local contents = json.encode(value)
    	local file = io.open(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(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
    - then in init I declare my global variables
    g_configfilepath = "|D|myprefs.txt"
    g_language = application:getLanguage()
    g_omodelpath = nil
    ...
    - then before the game starts I try to read the data (I put it in main.lua before scene manager)
    function myLoadPrefs(xconfigfilepath)
    	local mydata = getData(xconfigfilepath) -- try to read information from file
    	if not mydata then -- if no prefs file, create it
    		mydata = {}
    		mydata.g_language = g_language
    		mydata.g_omodelpath = g_omodelpath
    ...
    		saveData(g_configfilepath, mydata) -- create file and save datas
    	else -- prefs file exists, use it!
    		g_language = mydata.g_language
    		g_omodelpath = mydata.g_omodelpath
    ...
    	end
    end
    - then I load the data by calling the function myLoadPrefs
    -- let's load
    myLoadPrefs(g_configfilepath) -- load prefs
    - then there is the scene manager (also in main.lua)
    scenemanager = SceneManager.new(
    	{
    		["menu"] = Menu,
    		["thestage"] = TheStage,
    	}
    )
    scenemanager:changeScene("menu")
    stage:addChild(scenemanager)
    - then anywhere in the game (game, menu, ...) I can save the new data with this line
    		mySavePrefs(g_configfilepath)
    - and the mySavePrefs function (please note it is a global function, I put it just under the myLoadPrefs function in main.lua
    function mySavePrefs(xconfigfilepath)
    	local mydata = {} -- clear the table
    	mydata.g_language = g_language
    	mydata.g_omodelpath = g_omodelpath
    ...
    	saveData(xconfigfilepath, mydata) -- save new data
    end
    Ouf! :) hope this helps!?

    See also
    Thnx for useful info :) I decided the problem. It wasn't in the datasave functions. I just set timer 24 milleseconds after scene comes to update textfield values with saved data and it has worked!

    Likes: MoKaLux

    +1 -1 (+1 / -0 )Share on Facebook
Sign In or Register to comment.