Ive seen sample code on how to save and load high scores to games but Im trying to save the credits won on a slot machine app but it doesn’t seem to be working. Do anyone know where I can look at some sample code of one that works?
Hi ssccard,
I would look here. http://coronalabs.com/blog/2013/09/03/tutorial-understanding-in-app-purchases/
It’s a tutorial for in-app purchases but the same theory can be applied for saving slot machine credits. Just copy the utility.lua code in the tutorial and put it in a utility.lua file in your app’s folder. Then in your main.lua (or whatever file you have the slot machine) use this code from the in-app purchase tutorial.
local utility = require( "utility" ) local mySettings mySettings = utility.loadTable("settings.json") --perform check to see if file exists if mySettings == nil then mySettings = {} mySettings.isPaid = false utility.saveTable(mySettings, "settings.json") end
You can change this code to fit your needs. Perhaps something like this:
local utility = require( "utility" ) local saveFile saveFile = utility.loadTable("settings.json") --perform check to see if file exists if saveFile == nil then saveFile = {} saveFile.credits = 0 utility.saveTable(saveFile, "settings.json") end
Then when you user gets credits, you add credits to this. You can then save the number after each spin or when the user exits the game. Here is an example for saving after each spin:
local function spin() if win == true then credits = credits + amountWon elseif win == false then credits = credits - amountLost end saveFile.credits = credits utility.saveTable(saveFile, "settings.json") end
This is the method I would use.
Another option can be found here. http://thatssopanda.com/corona-sdk-tutorials/how-to-load-and-save-data-with-corona-sdk/
Hope this helps!
Eric
Ok so here is what I got right now:
utility.lua i removed everything about stores because I don’t need that right now:
[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]
main.lua setting locals:
[lua]–Save Credits
local utility = require( “utility” )
local mySettings
local restoring
mySettings = utility.loadTable(“settings.json”)
if mySettings == nil then
mySettings = {}
mySettings.isPaid = false
utility.saveTable(mySettings, “settings.json”)
end
main.lua code to save after checking for wins:
--If Credits Is Higher Than 0 Tell The Player What They Won
if addCredits > 0 then
credits = credits + addCredits
creditsText.text = credits
creditsText.anchorY = 0
creditsText.anchorX = 0
creditsText.x = display.contentCenterX + 100
creditsText.y = display.contentCenterY + 75
saveFile.credits = credits
utility.saveTable(mySettings, “settings.json”)
--Display Credits Won
local winText = display.newText(“You Won “…addCredits…” Credits!”,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]
When I place a bet and then win or loose to have more than the original credits, exit, then rebuild I still have the default credits. Where am I going wrong?
It looks like you are not using “saveFile” as the name. Right now you are using “mySettings” as the name so that is the name of the file. Try replacing
local utility = require( "utility" ) local mySettings mySettings = utility.loadTable("settings.json") --perform check to see if file exists if mySettings == nil then mySettings = {} mySettings.isPaid = false utility.saveTable(mySettings, "settings.json") end
with
local utility = require( "utility" ) local saveFile saveFile = utility.loadTable("settings.json") --perform check to see if file exists if saveFile == nil then saveFile = {} saveFile.credits = 0 utility.saveTable(saveFile, "settings.json") end
Basically you have table called “saveFile” and in this table you can store different variables or properties. So lets say you wanted to have the credits, a high score, and a number than keeps track of how many times you spin, you would make a new property for each. Like this:
local utility = require( "utility" ) local saveFile saveFile = utility.loadTable("settings.json") --perform check to see if file exists if saveFile == nil then saveFile = {} saveFile.credits = 0 saveFile.highScore = 0 saveFile.spinCount = 0 utility.saveTable(saveFile, "settings.json") end
If you still don’t understand why, let me know and I will try to explain better.
Hope it helps!
Looking over the code how does the game know what to save the file as and what it is called? Does it just do that itself?
I’ve commented the answers to your question in this code:
local utility = require( "utility" ) --This is the name of the table that stores the properties to be saved. local saveFile --"settings.json" is the name of the file that is being saved. In the file, you can find the table called saveFile saveFile = utility.loadTable("settings.json") --perform check to see if file exists if saveFile == nil then saveFile = {} --This is the line of code that creates the table to store the properties saveFile.credits = 0 --This is a property that is stored in the saveFile table saveFile.highScore = 0 --This is a property that is stored in the saveFile table saveFile.spinCount = 0 --This is a property that is stored in the saveFile table utility.saveTable(saveFile, "settings.json") --This is the code that saves the saveFile table end
In the simulator, if you click file and click “show project sandbox”, you will see where it is saving the file for the simulator. If you open the settings.json file, you will see the properties that are stored in the saveFile table.
On a phone, the location of the saved file depends on this line from the utility.lua file.
- local path = system.pathForFile( filename, system.DocumentsDirectory)
This line is telling the app to save the file in the phone’s Documents directory. This can be changed to the cache or the temp but for your purposes, the documents directory is fine.
Does this help answer your question?
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?
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.
Hi ssccard,
I would look here. http://coronalabs.com/blog/2013/09/03/tutorial-understanding-in-app-purchases/
It’s a tutorial for in-app purchases but the same theory can be applied for saving slot machine credits. Just copy the utility.lua code in the tutorial and put it in a utility.lua file in your app’s folder. Then in your main.lua (or whatever file you have the slot machine) use this code from the in-app purchase tutorial.
local utility = require( "utility" ) local mySettings mySettings = utility.loadTable("settings.json") --perform check to see if file exists if mySettings == nil then mySettings = {} mySettings.isPaid = false utility.saveTable(mySettings, "settings.json") end
You can change this code to fit your needs. Perhaps something like this:
local utility = require( "utility" ) local saveFile saveFile = utility.loadTable("settings.json") --perform check to see if file exists if saveFile == nil then saveFile = {} saveFile.credits = 0 utility.saveTable(saveFile, "settings.json") end
Then when you user gets credits, you add credits to this. You can then save the number after each spin or when the user exits the game. Here is an example for saving after each spin:
local function spin() if win == true then credits = credits + amountWon elseif win == false then credits = credits - amountLost end saveFile.credits = credits utility.saveTable(saveFile, "settings.json") end
This is the method I would use.
Another option can be found here. http://thatssopanda.com/corona-sdk-tutorials/how-to-load-and-save-data-with-corona-sdk/
Hope this helps!
Eric
Ok so here is what I got right now:
utility.lua i removed everything about stores because I don’t need that right now:
[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]
main.lua setting locals:
[lua]–Save Credits
local utility = require( “utility” )
local mySettings
local restoring
mySettings = utility.loadTable(“settings.json”)
if mySettings == nil then
mySettings = {}
mySettings.isPaid = false
utility.saveTable(mySettings, “settings.json”)
end
main.lua code to save after checking for wins:
--If Credits Is Higher Than 0 Tell The Player What They Won
if addCredits > 0 then
credits = credits + addCredits
creditsText.text = credits
creditsText.anchorY = 0
creditsText.anchorX = 0
creditsText.x = display.contentCenterX + 100
creditsText.y = display.contentCenterY + 75
saveFile.credits = credits
utility.saveTable(mySettings, “settings.json”)
--Display Credits Won
local winText = display.newText(“You Won “…addCredits…” Credits!”,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]
When I place a bet and then win or loose to have more than the original credits, exit, then rebuild I still have the default credits. Where am I going wrong?
It looks like you are not using “saveFile” as the name. Right now you are using “mySettings” as the name so that is the name of the file. Try replacing
local utility = require( "utility" ) local mySettings mySettings = utility.loadTable("settings.json") --perform check to see if file exists if mySettings == nil then mySettings = {} mySettings.isPaid = false utility.saveTable(mySettings, "settings.json") end
with
local utility = require( "utility" ) local saveFile saveFile = utility.loadTable("settings.json") --perform check to see if file exists if saveFile == nil then saveFile = {} saveFile.credits = 0 utility.saveTable(saveFile, "settings.json") end
Basically you have table called “saveFile” and in this table you can store different variables or properties. So lets say you wanted to have the credits, a high score, and a number than keeps track of how many times you spin, you would make a new property for each. Like this:
local utility = require( "utility" ) local saveFile saveFile = utility.loadTable("settings.json") --perform check to see if file exists if saveFile == nil then saveFile = {} saveFile.credits = 0 saveFile.highScore = 0 saveFile.spinCount = 0 utility.saveTable(saveFile, "settings.json") end
If you still don’t understand why, let me know and I will try to explain better.
Hope it helps!
Looking over the code how does the game know what to save the file as and what it is called? Does it just do that itself?
I’ve commented the answers to your question in this code:
local utility = require( "utility" ) --This is the name of the table that stores the properties to be saved. local saveFile --"settings.json" is the name of the file that is being saved. In the file, you can find the table called saveFile saveFile = utility.loadTable("settings.json") --perform check to see if file exists if saveFile == nil then saveFile = {} --This is the line of code that creates the table to store the properties saveFile.credits = 0 --This is a property that is stored in the saveFile table saveFile.highScore = 0 --This is a property that is stored in the saveFile table saveFile.spinCount = 0 --This is a property that is stored in the saveFile table utility.saveTable(saveFile, "settings.json") --This is the code that saves the saveFile table end
In the simulator, if you click file and click “show project sandbox”, you will see where it is saving the file for the simulator. If you open the settings.json file, you will see the properties that are stored in the saveFile table.
On a phone, the location of the saved file depends on this line from the utility.lua file.
- local path = system.pathForFile( filename, system.DocumentsDirectory)
This line is telling the app to save the file in the phone’s Documents directory. This can be changed to the cache or the temp but for your purposes, the documents directory is fine.
Does this help answer your question?