Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Can't load json on desktop instant game but works fine on mobile. — Gideros Forum

Can't load json on desktop instant game but works fine on mobile.

Hi everyone.
At the start of the game, I'm loading settings parameters from a json file. If the file is not present, then the file is created and then loaded. I'm using the datasaver module from github. Now the process works fine on the mobile version but doesn't seem to work on desktop. I've tried using both firefox and chrome. This has completely stumped me.
Any and all help deeply appreciated.

Comments

  • MoKaLuxMoKaLux Member
    edited June 2019
    hello @pm1891 this is my code
    You just need to add JSON in Plugins.


    json class:
    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
     
    --[[
    --try to read information
    local someData = getData("someData")
     
    -- if no information, create it
    if not someData then
    	someData = {"some text", 42}
    	saveData("someData.txt", someData)
    	print("Creating someData")
    else
    	print("Read someData", someData[1], someData[2])
    end
    ]]

    Then some global variables:
    g_configfilepath = "mkgiArabMdMT1.txt"
    g_language = application:getLanguage()
    g_theme = "dark"
    g_israndom = false
    g_istimer = false
    g_timerdelay = 10
    g_isaudio = true
    g_isgfx = true
    g_issfx = true
    g_istranslation = true
    g_isen = true
    g_level = 1
    g_currentlevel = 1

    Then create the json file (if it doesn't exist create it)
    Note that these are global functions.
    -- init prefs
    local mydata = getData(g_configfilepath) -- try to read information from file
     
    -- if no prefs file, create it
    if not mydata then
    	mydata = {g_language, g_theme, g_israndom, g_istimer, g_timerdelay, g_isaudio, g_isgfx, g_issfx, g_istranslation, g_isen, g_level}
    	saveData(g_configfilepath, mydata) -- create file and save datas
    else
    	g_language = mydata[1]
    	g_theme = mydata[2]
    	g_israndom = mydata[3]
    	g_istimer = mydata[4]
    	g_timerdelay = mydata[5]
    	g_isaudio = mydata[6]
    	g_isgfx = mydata[7]
    	g_issfx = mydata[8]
    	g_istranslation = mydata[9]
    	g_isen = mydata[10]
    	g_level = mydata[11]
    end

    And then later in the app save new parameters to the json file:
    You can call this function from anywhere in your code.
    -- save prefs
    function mySavePrefs(xlanguage, xtheme, xisrandom, xistimer, xtimerdelay, xisaudio, xistranslation, xlevel)
    	local mydata = {xlanguage or g_language, xtheme or g_theme, xisrandom or g_israndom,
    				xistimer or g_istimer, xtimerdelay or g_timerdelay, xisaudio or g_isaudio, xisgfx or g_isgfx,
    				xissfx or g_issfx, xistranslation or g_istranslation, xen or g_isen, xlevel or g_level}
    	saveData(g_configfilepath, mydata) -- save new datas
    	g_language = mydata[1]
    	g_theme = mydata[2]
    	g_israndom = mydata[3]
    	g_istimer = mydata[4]
    	g_timerdelay = mydata[5]
    	g_isaudio = mydata[6]
    	g_isgfx = mydata[7]
    	g_issfx = mydata[8]
    	g_istranslation = mydata[9]
    	g_isen = mydata[10]
    	g_level = mydata[11]
    end

    Hope this help.
    That works for me in both android and desktop.
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • example of calling the mySavePrefs (from parameters screen for example)
    -- LISTENERS
    btnTranslate:addEventListener("click", -- is translation
    	function()
    		-- save translation
    		g_istranslation = not g_istranslation
    		mySavePrefs(nil, nil, nil, nil, nil, nil, nil, nil, g_istranslation, nil, nil)
     
    		if g_istranslation then
    		else
    		end
    end)
    Voilà.
    my growING GIDEROS github repositories: https://github.com/mokalux?tab=repositories
  • antixantix Member
    I thought running in a browser meant you could not save local files?
  • antixantix Member
    I also use something similar to this...
    Prefs = Core.class()
     
    require("json")
     
    function Prefs:init() -- startup
      self.effectsEnabled = true -- default prefs
      self.effectsVolume  = 0.20
      self.musicEnabled   = true
      self.musicVolume    = 0.15
      self.noAds          = false
     
      self:loadPrefs() -- load prefs file
    end
     
    function Prefs:savePrefs() -- save prefs file
      local file = io.open("|D|prefs.json", "w+")
      if file then
        local p = {
          effectsEnabled  = self.effectsEnabled,
          effectsVolume   = self.effectsVolume,
          musicEnabled    = self.musicEnabled,
          musicVolume     = self.musicVolume,
          noAds           = self.noAds,
        }
        file:write(json.encode(p))
        file:close()
      else
        print("PREFS: unable to open file for writing")
      end
    end
     
    function Prefs:loadPrefs() --load prefs file
      local file = io.open("|D|prefs.json", "r")
      if file then
        local p = json.decode(file:read( "*a" ))
        for key, value in pairs(p) do -- overwrite internal values
          self[key] = value
        end
        file:close()
      else
        self:savePrefs() -- save default prefs file
      end
    end
    I have extra encryption steps in my production code

    Likes: MoKaLux

    +1 -1 (+1 / -0 )Share on Facebook
  • Apollo14Apollo14 Member
    edited June 2019
    @pm1891 as an option, you can store/update JSON file somewhere on web
    there're services with simple api (jsonbin.io, etc)
    (but probably they won't help you if you need json for every user)
    > Newcomers roadmap: from where to start learning Gideros
    "What one programmer can do in one month, two programmers can do in two months." - Fred Brooks
    “The more you do coding stuff, the better you get at it.” - Aristotle (322 BC)
  • pm1891pm1891 Member
    Thank you guys so much for taking an interest in this issue.
    @MoKaLux and @antix - Thanks for providing the code snippets. I'm using something similar too to load and save game preferences which works perfectly on mobile. It's deskptop browser that I'm having trouble too. Like you mentioned @antix, saving data in a browser is pretty difficult.

    Anyways, I looked around and apparently saving locally is possible in javascript using the
    HTML5 localStorage Object. Any idea as to how that can be implemented using the JS plugin? :)
  • pm1891pm1891 Member
    Wow! Very fast reply.You guys are awesome.
    Checking JS as I write this. Thanks for the link.

    Likes: MoKaLux

    +1 -1 (+1 / -0 )Share on Facebook
  • pm1891pm1891 Member
    Okay, so this is the code I've cobbled together. But the value I'm getting is nil. So what am I doing wrong? :(

    --------------------------------------------------

    local platform = string.lower(application:getDeviceInfo())
    if platform == "web" and (not mydata.levelselect) then

    JS.eval([[

    var M = { name: "John", maxLevels: 500, city: "New York" };
    localStorage.setItem('favouriteData', JSON.stringify(M));

    ]])

    local data = JS.eval([[ JSON.parse(localStorage.getItem('favouriteData')); ]])
    mydata.levelselect = data
    --------------------------------------------
    mydata.levelselect is showing a nil value.

    p.s - How do I post code on this forum?

  • hgy29hgy29 Maintainer
    JS.eval only handle string return values. Just return the JSON encoded string and decode it in lua.

    Likes: MoKaLux

    +1 -1 (+1 / -0 )Share on Facebook
  • pm1891pm1891 Member
    It's still not working. Still getting a nil value. What am I missing?
  • olegoleg Member
    image.png
    538 x 262 - 36K

    Likes: pm1891

    my games:
    https://play.google.com/store/apps/developer?id=razorback456
    мій блог по гідерос https://simartinfo.blogspot.com
    Слава Україні!
    +1 -1 (+1 / -0 )Share on Facebook
  • olegoleg Member
    pm1891 said:

    It's still not working. Still getting a nil value. What am I missing?

    maybe so?
    local data = JS.eval([[ localStorage.getItem('favouriteData'); ]])
    my games:
    https://play.google.com/store/apps/developer?id=razorback456
    мій блог по гідерос https://simartinfo.blogspot.com
    Слава Україні!
  • pm1891pm1891 Member
    Already tried that. No change.
  • pm1891pm1891 Member
    Already tried that. No change.
  • olegoleg Member
    so it does not work?
    JS.eval( localStorage.setItem('name', 'John'))
    print(JS.eval( localStorage.getItem('name'))
    my games:
    https://play.google.com/store/apps/developer?id=razorback456
    мій блог по гідерос https://simartinfo.blogspot.com
    Слава Україні!
  • pm1891pm1891 Member
    It's working on chrome but not on firefox. Haven't tried other browsers yet.
  • olegoleg Member
    edited June 2019
    I have not written on JS for a long time, but it seems
    on FF it is necessary to write as follows:

    window.localStorage;
    my games:
    https://play.google.com/store/apps/developer?id=razorback456
    мій блог по гідерос https://simartinfo.blogspot.com
    Слава Україні!
  • pm1891pm1891 Member
    I'll try that then. Thank you so much for all your help.

    Likes: MoKaLux

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