Show user level has been completed

I’m going to start by trying to give you a short answer, because data storage is a really big topic.

First off, don’t think of the data in terms of complete lines, but rather individual cells in a spreadsheet. I mean, ultimately you do have to write the lines as complete strings, but don’t think like that; think of it like you are filling out an Excel spreadsheet. So then your data is a set of rows for each level, where the first column is the level number and the second number is the completion state:

1 true  
2 false  
3 true  

Then when you load the data back in, chunk it up in the same way. The code from this thread is really useful for splitting up the data:
http://developer.anscamobile.com/forum/2011/01/19/suggestions-how-parse-string-12-2312-144-1

Then use the data to fill out tables in the code, by looping through all the data line by line and writing something like:

levels[data[1]] = data[2]  

Now the long answer is, well, really long. To see just how big a can of worms this is, try looking up “xml” in a search engine.

Stuff like XML and JSON is all about organizing data. For example, XML for your level data could look like:

<completed><br> <level num="1">true</level><br> <level num="2">false</level><br> <level num="3">true</level><br></completed>  

What’s the point of all that scaffolding? Well let’s say you also want to store the highscore:

<hscore>12345</hscore>  
<completed><br> <level num="1">true</level><br> <level num="2">false</level><br> <level num="3">true</level><br></completed>  

Well would you look at that, now it’s easy to tell what each row means.

ADDITION: What a coincidence, this thread just popped up
http://developer.anscamobile.com/forum/2011/01/26/could-use-example-writing-and-reading-file-variable [import]uid: 12108 topic_id: 5352 reply_id: 18740[/import]

Hi Guys,

Just wanted to thank you all for your help and time on this little problem i’ve been having with loading a data.txt file into a table.

Bad news is that I’m REALLY struggling to get my head around how to get this to work at the moment so I’m going to move on and re-visit it later, just wanted to make sure you all know that I really appreciate all your help.

Fingers crossed i’ll figure it out and when I do i’ll post my results here for all to chuckle at :slight_smile:

Thanks
Lee [import]uid: 13654 topic_id: 5352 reply_id: 18920[/import]

Have you looked at the link at the end of my post? [import]uid: 12108 topic_id: 5352 reply_id: 19019[/import]

Yeh but i’m not sure if the original code to show the stars was causing it not to work or not?

I’ve been trying so many things has all got very confusing.

This was the code i used to show the stars using just a table, now i need to save it in a data.txt file to re-use every time the app is launched.

Table:

t[1] = true  
t[2] = false  
t[3] = true  

SHOW STARS based on true / false

local coords = { 100,300, 250,300, 450,300 }  
for i = 1, #levels do  
 if levels[i] then  
 local x, y = coords[i\*2-1], coords[i\*2]  
 local star = display.newImage(localGroup, "images/gameplay/star.png", x, y)  
 end  
end  

As i’m pretty much trying this with only HTML / PHP experience (basic php) its a case of trial and error along the way, from memory i’m not sure anything was going in the table if it was it may been in a way that my Star true / false code couldn’t understand?
[import]uid: 13654 topic_id: 5352 reply_id: 19020[/import]

Ok, the Json.lua file is in with my files, i’ve put this in my main.lua file.

function saveOutTable(path, table)  
 local path = system.pathForFile( "data.txt", system.DocumentsDirectory )  
   
 file = io.open( path, "w" )  
 file:write( Json.Encode(table) )  
 io.close( file )  
end  
function readInTable(path, baseDirectory)  
 local path = system.pathForFile( "data.txt", baseDirectory or system.DocumentsDirectory )  
  
 file = io.open( path, "r" )  
 if file then  
 local table = Json.Decode( file:read( "\*a" ) )   
 io.close( file )  
 return table  
 else  
 return nil  
 end  
end  
  

When this main.lua finishes running it then goes to my menu screen, by this point the table should already have loaded the data.txt file which has this in it:

1=true,2=false,3=true,4=false,  

I’m probably wrong but in my head this would have created table with

[1] = true  
[2] = false  
[3] = true  
[4] = false  

So this code should see the true status and show the stars on those true values but it shows nothing and does not error?

local coords = { 100,300, 250,300, 450,300 }  
for i = 1, #table do  
 if table[i] then  
 local x, y = coords[i\*2-1], coords[i\*2]  
 local star = display.newImage(localGroup, "images/gameplay/star.png", x, y)  
 end  
end  

How would i get it to print the table so I can see what its containing, i’ve tried

print(#table)  

That just shows 0 (zero)

Thanks, if that makes sense to anyone it’ll be a amazing :slight_smile:
[import]uid: 13654 topic_id: 5352 reply_id: 19024[/import]

I use this a lot:

print(table.concat(mytable, ", "))  

Sometimes I just do a simple loop with a print statement inside.
[import]uid: 9659 topic_id: 5352 reply_id: 19029[/import]

Or this:

[lua]for i, v in ipairs (mytable) do --or pairs
print("record " …i …“is” …v)
end[/lua] [import]uid: 8970 topic_id: 5352 reply_id: 19031[/import]

So I have been pouring over this thread sense my latest post:
https://developer.anscamobile.com/forum/2011/03/21/what-simple-way-keep-track-many-levels

I am just curious if you have actually solved the issues you were having previously or if anyone else has a complete solution?

Thanks! [import]uid: 9968 topic_id: 5352 reply_id: 28874[/import]

I’ve not manage to solve this problem yet but to be honest I did spend quite a few hours on it at the time but then gave up and moved on so have not re-visited it yet, I decided to finish building the levels and then start looking at this problem again. [import]uid: 13654 topic_id: 5352 reply_id: 28882[/import]

Im somewhat new to programming and corona/lua but the game
I been working past 2months+ (sent to apple yesterday!!) currently
locks and unlocks the levels as you complete the previous level.

Example complete level 1 -> level 2 (unlock) ect. . .

You can use my method (might not be best approach but currently
works like a charm in my game) but obviously you want to show
level is completed instead of locking which you can do.

Hope this helps here is the solution (my method):

In my main.lua (using BEEBE CLASS) I made some variables
and turned them global with BEEBE CLASS for those who are
new here is an example:

In main.lua:
local level1_completed_image = game.level1_completed_image
game.level1_completed_image = false

note:You can call it anything you want don`t have to call it
level1_completed_image.

Ok next depending on your type of game how you complete your
level to show player he/she completed the level is up to you but
for those who are new and following along here is a simple example.

in level1.lua: when you complete a level (again every game varies
as your game to win might be a point A to point B game or fighting
game ect. . .) anyhow when the player completes the level implement
this in your code: game.level1_completed_image = 1
game.saveValue( “complete.txt”, game.level1_completed_image)

note:AGAIN you can call it anything you want i`m trying explain this
as simple as possible with simple names.

Ok next in your level selection lua( for me I find it easy calling it
levelselect.lua but you can call your lua files anything you want)

in levelselect.lua: display a local image and give it a .isVisible = false
example: local star1 = display.newImage(“star.png”)
star1.isVisible = false

next you need to load the value (if you followed along it should be 1)
so here is how you load it (using BEEBE CLASS):

game.level1_completed_image = game.loadValue(“complete.txt”)
game.level1_completed_image = tonumber( game.level1_completed_image )

next make a function:

local function level_complete1(e)
if(game.level1_completed_image == 1)then
star1.isVisible = true
end
end
Runtime:addEventListener(“enterFrame”, level_complete1)

Basically up to this point here is what`s happening in your main.lua
game.level1_completed_image = false and when you complete level1
it changes from false to a number 1 and saves the value in a .txt file
when you go to level selection
the Runtime/Function read the game.level1_completed_image is a 1
and displays a star!!

hope this helps and if you have any question ill try help as fast as I can!
Good luck

IMPORTANT NOTE: my game structure is using Director class and Beebe class
so the example above might work best if you use them too and I want to say
Thank you to Ricardo (Director Class) and John (Beebe Class) both are great
classes and have help me so much keep up the good work! [import]uid: 30314 topic_id: 5352 reply_id: 28890[/import]

The Above example was a simple example to try help people understand
but you can make it much more complex depending the structure of your
game (lua files, using other save files or SQLite ect . . .)
But this should get you going or help you get an idea ! [import]uid: 30314 topic_id: 5352 reply_id: 28891[/import]

Thanks kurosaki88,

I’ll try to give that a go asap and let you know how it goes.

[import]uid: 13654 topic_id: 5352 reply_id: 28893[/import]

Thanks kurosaki88,

I’ll try to give that a go asap and let you know how it goes.

[import]uid: 13654 topic_id: 5352 reply_id: 28894[/import]

I am trying to implement this system, including the lock and unlocking of levels, and this post is extremely helpful. If someone from Ansca could post a how to from start to finish that would be extremely helpful as I’m sure most new developers have trouble with this. [import]uid: 32061 topic_id: 5352 reply_id: 29349[/import]

@ 4d32petersc

I think the reason that is hard to post a how to from start to finish
is because there are various ways to implement a lock, unlock
save,load or show user a image when complete level (star, ect…)
The above example I post I tried to explained it as simple as possible
for new users might sound a bit complicated at first but once
you get it going you will start find it easier then it looks.

I had a very hard time finding out how to lock unlock and save/load
my game thanks to different post and help finally I learned. [import]uid: 30314 topic_id: 5352 reply_id: 29352[/import]

I tell you what guys, after I’ve released my current game in a couple weeks I’ll probably have time available to whip up a sample for this problem and put it on Code Exchange. [import]uid: 12108 topic_id: 5352 reply_id: 29382[/import]

That would be awesome.

I’ve figured everything else out so far so I’m sure with this post and others I’ll get it, but sample code always make the process easier.

If it is a quick thing to implement I’d be willing to pay for it! I’m sure for you pros it would only take a second to get me started and then I could take over. [import]uid: 32061 topic_id: 5352 reply_id: 29384[/import]

Looks like peach has already done a tutorial about this:
http://developer.anscamobile.com/forum/2011/04/10/saveload-lockunlock-help [import]uid: 12108 topic_id: 5352 reply_id: 31991[/import]

Hey jhocking,

Thanks so much for that link, i’ve FINALLY got my levels to unlock and have used multiple graphics to show when they are locked, unlocked & completed.

Running through the code has helped so much and I now understand this much better, this have driven the project ahead really well now and it’s all thanks to the amazing corona community!!! [import]uid: 13654 topic_id: 5352 reply_id: 33212[/import]

jhocking, thanks for the link love :slight_smile:

Digibug, I am totally thrilled that my tutorial could help you out.

Peach [import]uid: 52491 topic_id: 5352 reply_id: 33392[/import]