Please help Save/Load Game (Lock/Unlock) Levels

Please help as this is the last thing my game needs before I send
it to apple for review (my 1st app) I been reading forums and there
is a few good ones that have help me understand somewhat on
how to save and load my game and lock/unlock game levels
example:(player beats level 1 and so level 2 becomes playable)

But so far I have not manage to implement it to my game as i`m
somewhat new to programming and lua so I would appreciate it
greatly if anyone can help me implement a save and load to my game,
example:(if a player beats 10 levels and quits the game when they return
the 10 levels are still beaten) And also I want to lock my levels and in order
to unlock the next level you will need to beat the previous level.

My game structure is the following:
-Using Director Class
-All my levels are in different .lua (level1.lua, level2.lua, level3.lua)
-in each level when you win you get a message with a button that
tells you next level and takes you to the next level. (level1 -> level2)
-I also have a selectlevel.lua which is where all the levels are placed
so if you hit level1 then takes you to level1.lua.

I greatly appreciate any help and if I figure this out I will gladly post
the solution so maybe this can help others out there in same position
as me.

Thanks
[import]uid: 30314 topic_id: 7917 reply_id: 307917[/import]

The easiest way should be in saving the game progress in simple textfiles.
http://developer.anscamobile.com/content/files

The better way would be in a database, i think.
http://developer.anscamobile.com/content/data-storage [import]uid: 42078 topic_id: 7917 reply_id: 28149[/import]

Thanks for the links, but I was hoping anyone can help me
in a much more simple way as I`m somewhat new to programming
and lua and like I mention above I have read many forums regarding
this topic and the docs but I have not been able to fully understand.
So if anyone can please explain this or any help is greatly appreciated!

Question: What is a better (why?) save method SQLite or Files? [import]uid: 30314 topic_id: 7917 reply_id: 28200[/import]

I have a loadlevel module that I used in between levels.

for example:

If player hits the new Game Button then
from mainmenu.lua --> loadlevel.lua --> level1.lua

If player hits the resume button then
from mainmenu.lua --> loadlevel.lua --> whatever level.lua saved in my database

While in game, if the player hits restart then
from level.lua --> to loadlevel.lua --> back to the level.lua

in my loadlevel.lua:
This is an example from my game. Not sure if it will work for
your game, but it will give you a general idea.

[lua]
local goToLevel = function()

if _G.newGame then

director:changeScene( “level1”, “crossfade”)
print(“new level”)

elseif not _G.newGame then

print(“continue level”)

local continueLevel = (settings:loadVar(“level”))

director:changeScene(continueLevel, “crossfade”)
end
end

changelevelTimer = timer.performWithDelay( 1000, goToLevel, 1 )

[import]uid: 12455 topic_id: 7917 reply_id: 28219[/import]

Here’s a link to something that might be useful.

http://developer.anscamobile.com/forum/2011/02/02/how-can-i-easily-implement-save-and-load-system-0

Make sure you check out the link above before you start reading
further.

All Done with SQLite

This is how I use it.

[lua]–// Step 1.

– just as Ricardo suggested, I created a settings.lua with the SQLite3

– in my main.lua, I put the following at the top
local director = require (“director”)

local settings = require(“settings”)

– _G.newGame = false //this is a global variable i used in my game to detect a new game so I can reset all my local score/bullets/life etc.

– Create a main group
local mainGroup = display.newGroup()

– Main function
local function main()

– Add the group from director class

mainGroup:insert(director.directorView)

director:changeScene( “loadmainmenu2” )

return true

end

–// Step 2:
– In my level1.lua, I save the highScore/life/gameScore/ and etc
– when the game first started so I can carry though variables to next level…
– this is how I do it.

– initial value for my variables
local bestScore = 0
local gameScore = 0
– gameCount is life…LOL… i started with gameCount and just kind of ran with it.
local gameCount = 5
local levelBullet = 25

local nextLevel = “level2” – this is module level2.lua
local gameInit = function()

–==================== begin savings/loading process =============================
– HIGH SCORE OR SAVE IT
if type(settings:loadVar(“highscores”)) == “nil” then
settings:saveVar( “highscores”, bestScore )
else

local loadedBestScore = settings:loadVar(“highscores”)
bestScore = tonumber(loadedBestScore)
end

– GAME SCORE OR SAVE IT
settings:saveVar( “gamescore”, gameScore )

– CANNONBALL LEFT OR SAVE IT
settings:saveVar( “bullet”, levelBullet )

– LIFE LEFT OR SAVE IT
settings:saveVar( “life”, gameCount )

– add whatever codes you want in here if any.
end
–// Step 3. …still in level1… when the player beats the
– level then update the above Variables into your Database.
– Notes: This may not work for you game, but in my game, I only
– update the Variables if the player “click” the next Level Button.
– Example: if the player restart the level, the old
– life/gameScore/bestScore/ etc will be loaded… Not sure if this make sense.

– in my enterFrame function
– i created this to update my highscore, but not save it until
– the player hit the next level button

local loopGame = function ()
if gameScore > bestScore then

local newScore = gameScore
setBest( newScore )
end
end

–// step 4:
– update the Variables when player hit next level button
local function onlevelTouch (event)

– this is my globale variable may not be applicable to your game
_G.newGame = false

– save the high Score
settings:saveVar( “highscores”, bestScore )
– save the level completed
settings:saveVar( “level”, nextLevel)

– GAME SCORE OR SAVE IT
settings:saveVar( “gamescore”, gameScore )

– CANNONBALL LEFT OR SAVE IT
settings:saveVar( “bullet”, levelBullet )

– LIFE LEFT OR SAVE IT
settings:saveVar( “life”, gameCount )
– end of savings process

– add whatever you want it to happen when player hit next button here
end
–// step 5: level2.lua module…

local bestScore = tonumber(settings:loadVar(“highscores”))

local gameScore = tonumber(settings:loadVar(“gamescore”))

local levelBullet = tonumber( settings:loadVar(“bullet”))

local gameCount = tonumber(settings:loadVar(“life”))

local nextLevel = “level3”

–finally: when the player beats level2 then just save it using the same method i mentioned above.
[import]uid: 12455 topic_id: 7917 reply_id: 28218[/import]

@teex84,

Thanks for help I already had read the link you put up like 10x lol
but I could not understand it well as i have very little experience
with SQLite. But now that you post your code and explained it
Im starting to see a light at the end of the tunnel LOL im trying
to go over and over your codes and explanation to see if I understand
it better. What would be the easiest way to save when player hits
next level after level is completed? (because my game has no score
openfeint ect is just a simple complete task game and advance to
next level) I just want to save game when player hits next level and
when player starts game to load game (and all the levels completed
are still completed)

Thanks for your help! [import]uid: 30314 topic_id: 7917 reply_id: 28222[/import]

Update: So far I manage to lock and unlock levels using
global variables (don`t know if this is best approach) but
it seems to be working fine. I also manage to show a lock
image when level is locked and remove the image when
level is unlocked by using Runtime event listener and
global variables. The only problem I have right now is
how do I save my game when I beat a level and when
app launches it loads the last saved state? ( I would
imagine is not as complicated now that my game is
pretty much completed) please help anyone !

bonus: how can i delete all data with a reset button
to start all over if user wants to delete saved data?

Thank You!! [import]uid: 30314 topic_id: 7917 reply_id: 28344[/import]

you can try something like this. I’m sure there are other more
efficient ways to do this, but i’m also new to programming, so
this is how I would do it.

Note: code is untested.

[lua]-- ASSUME THIS IS THE FIRST TIME THE PLAYER PLAY THE GAME
–// Step1.
– in your selectionlevel.lua do this

local levelCompleted = 0

if type(settings:loadVar(“level”) == “nil” then
settings:saveVar(“level”, levelCompleted) – zero level completed
else
levelCompleted = tonumber(settings:loadVar(“level”)) – load saved levels
end
– in your selectionlevel.lua still
– this is noob code here, I’m sure there are more efficient ways of doing this.

local level1Touch = function(event)
if event.phase ==“ended” then
if levelCompleted => 0 then
local gotoSelectedLevel = function()
director:changeScene(“level1”)
end
levelTimer = timer.performWithDelay(1000, gotoSelectedLevel, 1)
end
end
end
local level2Touch = function(event)
if event.phase ==“ended” then
if levelCompleted => 1 then
local gotoSelectedLevel = function()
director:changeScene(“level2”)
end
levelTimer = timer.performWithDelay(1000, gotoSelectedLevel, 1)
end
end
end

–etc…

–// Step2:
– in your level1.lua do this

– BRAND NEW GAME
– levelCompleted = 0 // remember from selectlevel.lua
local levelCompleted = tonumber(settings:loadVar(“level”)

– save the variable into your database when player hit next level button
– or as soon as they beat the level you can save it
– which ever way works for you. I prefer the first method.

local nextLevelButton = function (event)
if event.phase == “release” then
levelCompleted = levelCompleted + 1

settings:saveVar( “level”, levelCompleted )
director:changeScene(“level2”)
end
end
– in your level2.lua load the levelCompleted from previous level
– Note: I keep my variable local to each level.lua

– levelCompleted = 1
local levelCompleted = tonumber((settings:loadVar(“level”))

– pretty much same code here
local nextLevelButton = function (event)
if event.phase == “release” then
levelCompleted = levelCompleted + 1

settings:saveVar( “level”, levelCompleted )
director:changeScene(“level3”)
end
end
–and that’s how I would do it. Again, this is untested, but it should give you an idea.
[import]uid: 12455 topic_id: 7917 reply_id: 28357[/import]

Thanks for all your help, since my game is structure already
and is playable and levels are lock, and they unlock when player
completes previous level I thought there might be a way every time
players completes a level it saves that current state or
the global variable which is the one that unlocked my next
level.

currently on my levelselect.lua (which is where you pick levels)
i have a function on level buttons (level1 level2 level3 ect)
each level button is given a event listener and the function
is for example if level1_complete = true then director changes
scene to my level2.lua.

on my main.lua my _G.level1_complete = false

when a player completes a level then level1.complete = true

and since level1.complete = true then my level 2 (activates)
and can now take you to level 2, as this is working fine
(im new to programming and is my 1st app)dont know
if this was the best way but it seems to work great and game
is running smoothly.

So this seems to be working fine and I`m wondering what is
the easiest path for me now to get my game to save because
if you unlocked 10 levels when you restart the app all the levels
are locked again.

Is there a way so every time player completes a level and
level1_complete = true (unlocked level2) my game saves or
save the current global variable (level1_complete = true)
so when user launches app it reads the saved variables
and loads it or loads from previous state?

Please help or point me in right direction this is the last thing
my game needs before I send to apple and is pretty frustrating
since most forum or tutorial expect you to have understanding
in SQLite or save files. [import]uid: 30314 topic_id: 7917 reply_id: 28371[/import]

You’re creating and updating your global variables, but you’re
Not saving them into a database. Everytime you restate the app,
The variables are resetted. You need to save them. The above example shows
You how to save, but you must figure it in your game where you want to implement
Your saving/loading process. [import]uid: 12455 topic_id: 7917 reply_id: 28426[/import]

Thanks again for your response, I figure im not saving them and thats why is resetting when I exit app. And thats pretty
much where i`m at right now trying to save them and loading
them when I start up app lol. I would like to save them everytime
player completes level or when they hit next level (after level is
completed) [import]uid: 30314 topic_id: 7917 reply_id: 28460[/import]

kurosaki88 - I am in the exact same boat with the exact same structured game as you (no scoring, director, etc). I have read everything out there and like you I have little development experience but my game is almost done.

If someone would post a youtube on how to implement this that would be ideal. I am sure this is going to be a huge question for all new developers which is what Corona definitely caters to. Or, post a game with code.

Thanks!

[import]uid: 32061 topic_id: 7917 reply_id: 29221[/import]

@ 4d32petersc

Hi, check this link below I think would be very helpful and if you still
have further question, post and Ill try get back to you asap and help
as much as I can.
Check Out this link: http://developer.anscamobile.com/forum/2011/01/21/show-user-level-has-been-completed [import]uid: 30314 topic_id: 7917 reply_id: 29279[/import]