loadsave.lua question. Saving To Table Overrides it with a single value

OK, so I’ve been doing a lot of research in order to fix my problem I have with “loadsave.lua”. Can’t seem to know what’s going on, or find a solution. The answer is probably so simple, but can’t see it. So here’s what I’m doing. If settings.lua doesn’t exist then create it. The problem I have is when I try to save a new value, it completely writes over the table with a single value, like a number

I have my main.lua

loadSave = require(‘loadsave’)

myData = loadSave.loadTable(“settiings.json”)

  – body

display.setStatusBar( display.HiddenStatusBar )

local composer = require(‘composer’)

–composer.gotoScene( “levelSelect” )

–local fonts = native.getFontNames( )

–for i,fontname in ipairs(fonts) do

– print(fonts[i])

–end

function main( … )

if (myData == nil) then

local m = {}

m.settings = {}

m.settings.unlockedLevels = 1

m.settings.FXVolume = 50

m.settings.musicVolume = 100

m.settings.levels = {}

m.settings.levels[1] = {}

m.settings.levels[1].globes = 0

m.settings.shadedGlobe = 4

loadSave.saveTable(m, “settings.json” )

end

composer.gotoScene( ‘levelSelect’ )

end

main()

**********************************

So I’m able to save a “settings.json” in system.DocumentsDirectory. Which looks like this.

******settings.json**********

{“maxLevels”:25,“settings”:{“shadedGlobe”:4,“levels”:[{“globes”:0}],“FXVolume”:50,“unlockedLevels”:1,“musicVolume”:100},“currentLevel”:1}

**************************

In my levelSelect, I have an options button which takes me to an options overlay. I’m able to get the file and read its values from settings.json. For example this is what I’m trying to do

***********options.lua**************

local settings = loadSave.loadTable(“settings.json”)

local soundFXButton = widget.newSlider( {

        label = “FX”,

        id = “FX”,

        width = 100,

        height = 20, 

        listener = FXListener,

        value = settings.FXVolume ** able to get value from settings.json

        } )

********trying to change value of settings.FXVolume and save it.

function FXListener( event ) 

    – body

    

    local sliderValue = event.value

    local logValue 

if (“ended” == event.phase) then

   

        

        logValue = sliderValue

       settings.FXVolume = logValue

       loadSave.saveTable(settings.FXVolume,“settings.json”)

   end

I’ve tried different ways of writing it with no luck. It just erases the table and puts the single value of settings.FXVolume.

Any help would be appreciated.

Try:

        logValue = sliderValue

       settings.FXVolume = logValue

       loadSave.saveTable( settings ,“settings.json”)

WOW! That did the trick. Thanks for the help. :slight_smile:

Ok, so there is another problem that arose. The “settings.json” table is still there but on upon saving the FXvolume it creates another FXVolume variable. So now there are 2 FXVolumes.

Check your capitalization. 

FXVolume is not the same as FXvolume (notice the upper case V vs. the lower case v).

​Your variable names need to use the exact same case.

I’ve figured it out…phew, Thanks!  :slight_smile:

Try:

        logValue = sliderValue

       settings.FXVolume = logValue

       loadSave.saveTable( settings ,“settings.json”)

WOW! That did the trick. Thanks for the help. :slight_smile:

Ok, so there is another problem that arose. The “settings.json” table is still there but on upon saving the FXvolume it creates another FXVolume variable. So now there are 2 FXVolumes.

Check your capitalization. 

FXVolume is not the same as FXvolume (notice the upper case V vs. the lower case v).

​Your variable names need to use the exact same case.

I’ve figured it out…phew, Thanks!  :slight_smile: