how to work with a pre-existing number

hello , i’m a newbie of lua language and i want to ask a silly question. I’m making a simple tool that keeps track of a number and there are 3 buttons :
-1
+1
+10
i want to change the number in real time and once closed the app , next time i open it, it should start from the number of previous session ( e.g if i, reach let’s say, 55 by pressing those 3 buttons ,once re-opened i want to start from 55 to change the number upward or downward ) right now i’m using a Hihgscore type system that just don’t fit very well.

Hi, you can save the number and then get it when needed:

-- saving
local function save(baseDir,fileName,srcTable)
   baseDir  = baseDir or system.DocumentsDirectory

   local encodedTable = json.encode( srcTable )

   io.writeFile( encodedTable, fileName, baseDir )

   return encodedTable
end

--getting data 
local function load(baseDir,fileName)
   baseDir  = baseDir or system.DocumentsDirectory

   local encodedData = io.readFile( fileName, baseDir )

   local restoredTable = json.decode(encodedTable)

   return restoredTable
end

How to use it:

-- saving data

local myTable = {
  num = 12,
}
save(system.DocumentsDirectory,data.json,myTable)
print(encodedTable)

-- getting data

load(system.DocumentsDirectory,data.json)
print(restoredTable)

And I think you will need these modules: just put folder into your app/game folder and type this in you main.lua (or scene file):

require "extensions.io"
require "extensions.string"
require "extensions.table"

Modules:
[extensions.zip (8.4 KB) ](http://)

And these modules are not mine, @roaminggamer gave me these.

1 Like

More details on the extenstions:

Source for all of SSK2: https://roaminggamer.github.io/RGDocs/pages/SSK2/

2 Likes

thank you both very much