Is filename in the code
- local path = system.pathForFile( filename, system.DocumentsDirectory)
supposed to be left as filename, or do I change it to something like credits.txt?
Is filename in the code
supposed to be left as filename, or do I change it to something like credits.txt?
Leave that line just as it is. Do not change anything in utility.lua and it should work. Did the changes I mentioned earlier work?
Its still not working… Here is what I have now…
main.lua
[lua]–Save
local utility = require( “utility” )
local saveFile
saveFile = utility.loadTable(“settings.json”)
–perform check to see if file exists
if saveFile == nil then
saveFile = {}
saveFile.credit = 0
utility.saveTable(saveFile, “settings.json”)
end
–Load Credits
function loadCredit()
local prevCredit = credit.load()
if prevCredit ~= nil then
if prevCredit <= credit.score then
credit.set(credit.score)
else
credit.set(prevCredit)
end
else
credit.set(credit.score)
credit.save()
end
end[/lua]
[lua] --If credit Is Higher Than 0 Tell The Player What They Won
if addcredit > 0 then
credit = credit + addcredit
creditText.text = credit
creditText.anchorY = 0
creditText.anchorX = 0
creditText.x = display.contentCenterX + 100
creditText.y = display.contentCenterY + 75
saveFile.credit = credit
utility.saveTable(saveFile, “settings.json”)
–Display credit Won
local winText = display.newText(“You Won “…addcredit…” credit!”,0,0,“Helvetica”,32)
winText.x = _W*0.5; winText.y = _H*0.3
winText:setFillColor(0,0,0)
machineGroup:insert(winText)
–Transition Up, Fade, And Destroy Text
local trans = transition.to(winText,
{
time=2000,
y= winText.y -30,
alpha = 0,
onComplete = function()
display.remove(winText)
winText = nil
end
})
end[/lua]
utility.lua
[lua]–we need a few support functions
local M = {}
local json = require(“json”)
function M.saveTable(t, filename)
local path = system.pathForFile( filename, system.DocumentsDirectory)
local file = io.open(path, “w”)
if file then
local contents = json.encode(t)
file:write( contents )
io.close( file )
return true
else
return false
end
end
–simple load table function
function M.loadTable(filename)
local path = system.pathForFile( filename, system.DocumentsDirectory)
local contents = “”
local myTable = {}
local file = io.open( path, “r” )
if file then
local contents = file:read( “*a” )
myTable = json.decode(contents);
io.close( file )
return myTable
end
print(filename, “file not found”)
return nil
end
return M[/lua]
Is it giving you an error or is it not saving the credits?
If it is giving an error, what does it say?
It doesn’t give any errors and it does not save the credits.
If you like, you can attach your main.lua file and I will take a look tomorrow.
It wouldn’t let me attach the lua file because it says Im not permitted to attach that kind of file. I tried to post it on here but for some reason it would not show up in lua format. So I sent you a message with the code in lua format.
Thanks for taking the time to help me out!
Here is the main.lua.zip
Hi ssccard,
I have found the problem. It actually is saving your credits. If you click File then click on show project sandbox and look at the .json file, you will see that it is saving the credits. The reason it appears that it is not saving your credits is because of this chunk of code here at the beginning of your main.lua:
-- Set Starting credit local credit = 100 local bet = 1 --Can be 1,2,3,5 local betAllowed = true local slotColumn1 = {} local slotColumn2 = {} local slotColumn3 = {} --Save local utility = require( "utility" ) local saveFile saveFile = utility.loadTable("settings.json") --perform check to see if file exists if saveFile == nil then saveFile = {} saveFile.credit = 0 utility.saveTable(saveFile, "settings.json") end
In this code, you are setting the credits to 100 every time the app is open/restarted. This means that the credits will always show as 100 at the beginning. But your credits shouldn’t be set to a specific value every time. Instead, the credits should be set to the value of the saved credits. So when you first open the app, the saveFile credits should be 100 upon first creation then everytime after they will load the previous value. So your code should look like this:
local bet = 1 --Can be 1,2,3,5 local betAllowed = true local slotColumn1 = {} local slotColumn2 = {} local slotColumn3 = {} --Save local utility = require( "utility" ) local saveFile saveFile = utility.loadTable("settings.json") --perform check to see if file exists if saveFile == nil then saveFile = {} saveFile.credit = 100 utility.saveTable(saveFile, "settings.json") end -- Set Starting credit local credit = saveFile.credit
Take a look at this and let me know if this makes sense.