Difference between revisions of "Article Tutorials/Loading And Saving"
From GiderosMobile
Line 1: | Line 1: | ||
__TOC__ | __TOC__ | ||
− | You will often want to save important information such as the player’s name, score, high score, current location in the game and object positions. | + | You will often want to save important information such as the player’s name, score, high score, current location in the game and object positions. |
− | |||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
local Player = "Jason" | local Player = "Jason" | ||
Line 22: | Line 21: | ||
print ("Score: ", Score2) | print ("Score: ", Score2) | ||
print ("HiScore: ", HiScore2) | print ("HiScore: ", HiScore2) | ||
− | </syntaxhighlight | + | </syntaxhighlight> |
− | Here, we’ve created two copies of each variable and assigned ‘nil’ to the second version just to prove the variables were blank between saving and loading. Of course, you won’t be doing this in your actual application. | + | Here, we’ve created two copies of each variable and assigned ‘nil’ to the second version just to prove the variables were blank between saving and loading. Of course, you won’t be doing this in your actual application. |
===Initialise Your Data=== | ===Initialise Your Data=== | ||
− | An easy way to initialise your application settings is to first check if a save file exists with your values. If not, define default values for the application until the user changes this. | + | An easy way to initialise your application settings is to first check if a save file exists with your values. If not, define default values for the application until the user changes this. |
− | |||
<syntaxhighlight lang="lua"> | <syntaxhighlight lang="lua"> | ||
local Player | local Player | ||
Line 44: | Line 42: | ||
file:close() | file:close() | ||
end | end | ||
− | </syntaxhighlight> | + | </syntaxhighlight> |
+ | |||
+ | This way you will always have values for your application to use. | ||
− | |||
'''Note: This tutorial was written by [http://bluebilby.com/author/waulokadmin/ Jason Oakley] and was originally available Here: http://bluebilby.com/2013/05/04/gideros-mobile-tutorial-loading-and-saving-data/''' | '''Note: This tutorial was written by [http://bluebilby.com/author/waulokadmin/ Jason Oakley] and was originally available Here: http://bluebilby.com/2013/05/04/gideros-mobile-tutorial-loading-and-saving-data/''' |
Latest revision as of 10:39, 26 August 2024
You will often want to save important information such as the player’s name, score, high score, current location in the game and object positions.
local Player = "Jason"
local Score = 23980
local HiScore = 349870
local Player2 = nil
local Score2 = nil
local HiScore2 = nil
local file=io.open("|D|settings.txt","w+")
file:write(Player .. "\n")
file:write(Score .. "\n")
file:write(HiScore .. "\n")
file:close()
file2=io.open("|D|settings.txt", "r")
Player2=file2:read("*line")
Score2=tonumber(file2:read("*line"))
HiScore2=tonumber(file2:read("*line"))
file2:close()
print ("Player: ", Player2)
print ("Score: ", Score2)
print ("HiScore: ", HiScore2)
Here, we’ve created two copies of each variable and assigned ‘nil’ to the second version just to prove the variables were blank between saving and loading. Of course, you won’t be doing this in your actual application.
Initialise Your Data
An easy way to initialise your application settings is to first check if a save file exists with your values. If not, define default values for the application until the user changes this.
local Player
local Score
local HiScore
local file = io.open("|D|settings.txt","r")
if not file then
Player = "Nobody"
Score = 0
HiScore = 0
else
Player=file:read("*line")
Score=tonumber(file:read("*line"))
HiScore=tonumber(file:read("*line"))
file:close()
end
This way you will always have values for your application to use.
Note: This tutorial was written by Jason Oakley and was originally available Here: http://bluebilby.com/2013/05/04/gideros-mobile-tutorial-loading-and-saving-data/