Please help! Saving a table or data in a table

I am trying to save data in a table. I am using the loadsave module created by robmiracle located here

http://developer.coronalabs.com/code/super-simple-lua-table-save-and-load-file-functions


It seems easy enough however, for some reason it not working for me.  Here’s what I am trying to do. Create a table from a lua file and change the value of the element of the table from another lua file then save the table.  Here’s what I tried and I’ve been at this for a while. 

File1.lua


loadsave=require(“loadsave”)

myTable = loadTable(“myTable.json”)  – this load myTable everytime I open the app

myTable = {}
myTable.score1 = 100
myTable.score2 = 50

saveTable(myTable, “myTable.json”)

file2. lua 


file1=require(“file1”)

loadsave=require(“loadsave”)

myTable = loadTable(“myTable.json”)  – this load myTable everytime I open the app

The questions I have how can I can the value of MyTable.score1 from file2.lua and still save it?? I tried this:

file1.myTable.score1=20  --change value of myTable.score1=20 in file1.lua

saveTable(myTable, “myTable.json”) --save the table

Any help would be appreciated.Thanks

First of all:

myTable = loadTable(“myTable.json”)  – this load myTable everytime I open the app

myTable = {}
myTable.score1 = 100
myTable.score2 = 50

saveTable(myTable, “myTable.json”)

 

probably isn’t doing what you think.  You immediatly overwrite what you loaded when you set myTable = {}.  This is probably more of what you meant:

 

myTable = loadTable(“myTable.json”)  – this load myTable everytime I open the app

if myTable == nil then

myTable = {}
myTable.score1 = 100
myTable.score2 = 50

saveTable(myTable, “myTable.json”)

end

 

If you intend to use this like a module, then you need a little more code:

 

myTable = loadTable(“myTable.json”)  – this load myTable everytime I open the app

if myTable == nil then

myTable = {}
myTable.score1 = 100
myTable.score2 = 50

saveTable(myTable, “myTable.json”)

end

return myTable

 

In your first file.  Then in your 2nd file, you can do this instead:

 

myTable = = require(“file1”)

loadsave=require(“loadsave”) – needed to set the save table function for later

– myTable = loadTable(“myTable.json”)  – this load myTable everytime I open the app  *** you don’t need this line any more

Then you can access myTable as needed and then call loadsave.saveTable(myTable, “myTable.json”) when you need to save things.

EDIT: Rob beat me to it within just a few minutes  :slight_smile:

The reason it’s not working is that you need to “modularize” your score lua (file 1).

Off the top of my head, I’d recommend something similar to this (I haven’t tested the code, but it should work fine…)

scores.lua


local M = {};

loadsave = require(“loadsave”);

local DATA_FILE = “myTable.json”;
 

M.saveScores = function()
    saveTable(M.myTable, DATA_FILE);
end
 

M.myTable = loadTable(DATA_FILE)  – this load myTable everytime I open the app

if (M.myTable == nil) then
    M.myTable = {};
    M.myTable.score1 = 100;
    M.myTable.score2 = 50;
    M.saveScores();
end

return M;

…then you can call and manipulate it from any other module you want:

file2.lua


local scores = require(“scores”);

scores.myTable.score1 = 20;

scores.saveScores();

@ Rob and ingemar,

Thank you for the quick response. I am not sure why this is not working for me. It seems simple enough. Here’s exactly what I did:

scores.lua


module(…, package.seeall)

local json = require(“json”)

local loadsave = require(“loadsave”)

myTable = loadTable(“myTable.json”)  – this load myTable everytime I open the app

if myTable == nil then

myTable = {}

myTable.score1 = 100

myTable.score2 = 50

saveTable(myTable, “myTable.json”)

end

return myTable

in my second file (File2.lua) I did this:

File2.lua


local json = require(“json”)

 local loadsave = require(“loadsave”)

scores = require(“scores”);

– require “scores”

  scores.myTable.score1=2000

   print(scores.myTable.score1)

    

  loadsave.saveTable(myTable, “myTable.json”)

Here is the error I am getting. “scores.lua:8 attempt to call global ‘loadTable’ (a nil value)”

I also try the code ingemar provided it didn’t work for me either. Anything else I might be doing wrong? Thanks again for the help.

Sorry I must have gotten cut paste happy:

myTable = _ loadsave. _loadTable(“myTable.json”)  – this load myTable everytime I open the app

if myTable == nil then

myTable = {}

myTable.score1 = 100

myTable.score2 = 50

saveTable(myTable, “myTable.json”)

end

return myTable

Many thanks to Rob for putting this together.    I wish I would have found it before I wrote my own custom clunky code to save settings, levels, etc.    All converted now and much cleaner.   

First of all:

myTable = loadTable(“myTable.json”)  – this load myTable everytime I open the app

myTable = {}
myTable.score1 = 100
myTable.score2 = 50

saveTable(myTable, “myTable.json”)

 

probably isn’t doing what you think.  You immediatly overwrite what you loaded when you set myTable = {}.  This is probably more of what you meant:

 

myTable = loadTable(“myTable.json”)  – this load myTable everytime I open the app

if myTable == nil then

myTable = {}
myTable.score1 = 100
myTable.score2 = 50

saveTable(myTable, “myTable.json”)

end

 

If you intend to use this like a module, then you need a little more code:

 

myTable = loadTable(“myTable.json”)  – this load myTable everytime I open the app

if myTable == nil then

myTable = {}
myTable.score1 = 100
myTable.score2 = 50

saveTable(myTable, “myTable.json”)

end

return myTable

 

In your first file.  Then in your 2nd file, you can do this instead:

 

myTable = = require(“file1”)

loadsave=require(“loadsave”) – needed to set the save table function for later

– myTable = loadTable(“myTable.json”)  – this load myTable everytime I open the app  *** you don’t need this line any more

Then you can access myTable as needed and then call loadsave.saveTable(myTable, “myTable.json”) when you need to save things.

EDIT: Rob beat me to it within just a few minutes  :slight_smile:

The reason it’s not working is that you need to “modularize” your score lua (file 1).

Off the top of my head, I’d recommend something similar to this (I haven’t tested the code, but it should work fine…)

scores.lua


local M = {};

loadsave = require(“loadsave”);

local DATA_FILE = “myTable.json”;
 

M.saveScores = function()
    saveTable(M.myTable, DATA_FILE);
end
 

M.myTable = loadTable(DATA_FILE)  – this load myTable everytime I open the app

if (M.myTable == nil) then
    M.myTable = {};
    M.myTable.score1 = 100;
    M.myTable.score2 = 50;
    M.saveScores();
end

return M;

…then you can call and manipulate it from any other module you want:

file2.lua


local scores = require(“scores”);

scores.myTable.score1 = 20;

scores.saveScores();

@ Rob and ingemar,

Thank you for the quick response. I am not sure why this is not working for me. It seems simple enough. Here’s exactly what I did:

scores.lua


module(…, package.seeall)

local json = require(“json”)

local loadsave = require(“loadsave”)

myTable = loadTable(“myTable.json”)  – this load myTable everytime I open the app

if myTable == nil then

myTable = {}

myTable.score1 = 100

myTable.score2 = 50

saveTable(myTable, “myTable.json”)

end

return myTable

in my second file (File2.lua) I did this:

File2.lua


local json = require(“json”)

 local loadsave = require(“loadsave”)

scores = require(“scores”);

– require “scores”

  scores.myTable.score1=2000

   print(scores.myTable.score1)

    

  loadsave.saveTable(myTable, “myTable.json”)

Here is the error I am getting. “scores.lua:8 attempt to call global ‘loadTable’ (a nil value)”

I also try the code ingemar provided it didn’t work for me either. Anything else I might be doing wrong? Thanks again for the help.

Sorry I must have gotten cut paste happy:

myTable = _ loadsave. _loadTable(“myTable.json”)  – this load myTable everytime I open the app

if myTable == nil then

myTable = {}

myTable.score1 = 100

myTable.score2 = 50

saveTable(myTable, “myTable.json”)

end

return myTable

Many thanks to Rob for putting this together.    I wish I would have found it before I wrote my own custom clunky code to save settings, levels, etc.    All converted now and much cleaner.