Show user level has been completed

Hi all,

Can anyone point me in the right direction on how to show a user they have successfully completed a level.

For example the Bubble Ball game does exactly what i need:

The level select screen has buttons, those buttons show a star if you’ve already completed it.
All my buttons are in place, my levels are under way and now I guess I need to save a value somehow to use again later in order to choose a different image?
Any help would be really appreciated.

Thanks [import]uid: 13654 topic_id: 5352 reply_id: 305352[/import]

You lost me with your long paragraph - I don’t know what you mean in there, but you might want to look at the Beebe games examples from the Code Exchange:

http://developer.anscamobile.com/code/martian-control
http://developer.anscamobile.com/code/ghosts-vs-monsters
[import]uid: 9659 topic_id: 5352 reply_id: 18025[/import]

Use a table to keep track of which levels have been completed. Then when showing the level select screen loop through this table to check whether or not to show a star:

for i = 1, #table do if table[i] then local star = display.newImage("star.png") star.x = level.x star.y = level.y end end [import]uid: 12108 topic_id: 5352 reply_id: 18027[/import]

Sorry if I confused you lococo… Hard to explain what I’m trying to do, the FREE bubble ball game is the best way to explain what I’m after.

First time you play level 1 the selection button has no star.

If you complete level 1 and go back to play it again the selection but has a star on it.

This indicates that the user has completed that level already.

jhocking, thanks again for your reply your as helpful as ever, only snag is i’ve not done any work with tables at all so at the moment I’m not even sure how to create one, save data to it and then use your code (I"m guessing) to retrieve it and activate those stars.

I’ve been looking at the sample files etc… but at present not getting far, I don’t give up easily though!!

If I’m not asking too much jhocking could you show me a really simple bit of code to:

Create a table and then Save level name and status and then how your code would retrieve that?

I’ve tried a few bit but am not sure if it actually works in the simulator or if i’m messing up.

No worries if your not able to I know i’m asking alot!! :slight_smile:

Thanks [import]uid: 13654 topic_id: 5352 reply_id: 18072[/import]

Creating a table is easy; just type

table = {}  

Then adding values can be done in multiple ways. Here’s one way to add, say, level one:

table[1] = true  

Incidentally, I think you’ll want to use maxn() instead of # when looping through the table, look in the docs. [import]uid: 12108 topic_id: 5352 reply_id: 18080[/import]

Hi jhocking

I’m starting to get somewhere (I think)

Adding data to table:

t = {}
table.insert(t,1)
table.insert(t,3)
Printed results are:

1 1
2 3
Then I’m using your code(below) to see if 1 exists if so show star in set location if true. This kinda works (ish)
for i = 1, #t do
if t[1] then
local star1 = display.newImage( “images/gameplay/star.png”, 100, 300)
localGroup:insert(star1)
end
end

When i add another, for example:
for i = 2, #t do
if t[2] then
local star2 = display.newImage( “images/gameplay/star.png”, 150, 300)
localGroup:insert(star2)
end
It will show star 2 in the table not star 3. Basically it’s looking at the wrong column instead of the value.

Column — Value
1 -------- 1
2 -------- 3

Any ideas where I might be going wrong, I’m a new boy so sorry for asking such stupid q’s!!

thanks

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

Two things:

(1) In the forums, try to surround your code with <code> </code> tags.

(2) I think you mean “if t[i] then” not “if t[2] then”

[import]uid: 9659 topic_id: 5352 reply_id: 18105[/import]

Sorry about that lococo, I’m still finding my way around forums as well!!

I changed that to the 2 as specified, several hours later i’m getting closer but still not right, here is what I have so far.

Table being created and populated

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

Code to show the star if = true

for i = 1, #t do  
 if t[i] == true then  
 local star1 = display.newImage( "images/gameplay/star.png", 100, 300)  
 localGroup:insert(star1)  
 end   
end  
for i = 2, #t do  
 if t[i] == true then  
 local star2 = display.newImage( "images/gameplay/star.png", 250, 300)  
 localGroup:insert(star2)  
 end   
end  
for i = 3, #t do  
 if t[i] == true then  
 local star3 = display.newImage( "images/gameplay/star.png", 450, 300)  
 localGroup:insert(star3)  
 end   
end  
  

Terminal is showing this when i print it:

1 false  
2 false  
3 true  

But all i’m getting is all stars showing which is wrong, it should only show star 3?

I’ve tried lots of different ways, i.e. instead of true using 1, 2, 3 but with the same results.

If i change it to be all false none show up, or if i change it to the second star being true the first and second star show up?

It’s very odd and very annoying, I’m clearly missing something and have been reading lots on tables all night with no luck, any advice would be greatly appreciated.

Thanks all. [import]uid: 13654 topic_id: 5352 reply_id: 18125[/import]

I don’t understand why you’re doing three different sets of loops. It would seem that one loop ought to do the trick:

local coords = { 100,300, 250,300, 450,300 }  
for i = 1, #t do  
 if t[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  

A few notes:

  • I collected the coordinates in a local array.
  • I deleted “== true” from the if test.
  • I added localGroup as the first argument to newImage. Saves one line of code. :slight_smile:

Best,
Mike
[import]uid: 9659 topic_id: 5352 reply_id: 18174[/import]

You could also do something like this, where you create a table of tables, where each inner table has named elements (seen, x, y). This makes it pretty easy to expand later on when you might want to add other attributes to each level.

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

Just spitballin’… :slight_smile:
Mike
[import]uid: 9659 topic_id: 5352 reply_id: 18177[/import]

Thanks Mike,

Your first block of code worked perfectly and makes sense now, I’m starting to get my head around some of this now thanks to all the help i’m getting here!!

The stars show up exactly as expected now which is spot on, my last question of this “honest” is how to get the table to be created if it doesn’t exist.

I’ve looked around and there seems to be a few references to checking if files exist and if not creating it but I cant seem to find it for checking if a table exists and if not how to create that?

Thanks again for all your help guys :slight_smile: [import]uid: 13654 topic_id: 5352 reply_id: 18267[/import]

I’m not clear on whether you’re asking whether a table exists or a file exists. Either way it’s pretty easy. If you’re checking a table or any other variable, just test like this:

if not variable then  
 ...  
end  

You could also say “if variable == nil then” but I prefer the simpler version.

For a file, here’s what I do:

local path = system.pathForFile("data.txt", system.DocumentsDirectory)  
local file = io.open(path, "r")  
if file then  
 ...read from file...  
 io.close(file)  
end  

Good luck!
Mike
[import]uid: 9659 topic_id: 5352 reply_id: 18272[/import]

Thanks for such a quick reply Mike.

So to create a table if it doesn’t already exist I would need to do:

if not #t then  
t = {}  
end  

That code you showed me earlier to show the star is fab by the way but when they get to the level select screen having never played it wont load because the table hasn’t been created yet, so i need to create it once if it doesn’t exist.

Then when each level is finished i’m doing

t[1] = true  

When they then return to the level select screen your code works like a dream and shows the correct stars!!

A table is the right way to do this isn’t it? The do get stored on the device so when the re-launch the app it will still know what levels have been done?

Thanks for you help

Lee [import]uid: 13654 topic_id: 5352 reply_id: 18274[/import]

Hi Lee:

It’s “if not t then”, not “if not #t then” (no #).

I would think that your app would simply load the table from a file right at startup. That’s what mine does. Just go in and read the file. If the file’s not there, simply create an empty table. When the app finishes, write the table.

And yes, a table is definitely the right way to do it. You could get fancy and use a live SQLite database, but I think that’s overkill. :slight_smile:

Mike
[import]uid: 9659 topic_id: 5352 reply_id: 18279[/import]

Ha - Thanks Mike,

You’ve just opened up a whole load of new reading for me…

This is my First app so learning… loading, reading & writing from and to a file is not something i’ve ever done, the table been tripping me up alot so far but thanks to you guys i’m slowly getting there.

Ok so I need to read from a file when it first loads, if that file isn’t there create it, store all my levels completed data in my table until the app is closed and then write the table data to that file.

Is that correct?

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

Almost. Read from the file. If the file isn’t there, it means it’s the first time this app has ever been run, so just create an empty table internally. Add to the table as time goes on. Each time the user finishes a level, update the table internally and save it to your file. Also save it to the file when the app exits. I think that’s it. :slight_smile:
[import]uid: 9659 topic_id: 5352 reply_id: 18286[/import]

Sorted, I shall give that a go.

thanks Mike. [import]uid: 13654 topic_id: 5352 reply_id: 18288[/import]

*can’t delete posts on this forum, doh* [import]uid: 12108 topic_id: 5352 reply_id: 18300[/import]

Hi guys,

Wonder if you could help me out “again” sorry!! I’m doing pretty well but can quite figure out whats going wrong with the this code.

This code Creates my data file, saves data to show star 1 & 3

local path = system.pathForFile( "data.txt", system.DocumentsDirectory )  
   
-- io.open opens a file at path. returns nil if no file found  
local file = io.open( path, "r" )  
if file then  
 -- read all contents of file and put into levels table  
 local contents = file:read( "\*a" )  
 levels = { "\n" .. contents }  
 io.close( file )  
else  
 -- create data file it doesn't exist yet  
 file = io.open( path, "w" )  
 file:write( "1 = true\n2 = false\n3 = true" )  
 io.close( file )  
  
 levels = {}  
  
end  

To check whats happening i’ve printed the data file and table results to ensure they match, this is what i get:

  
Data File Results  
1 = true  
2 = false  
3 = true  
  
Table results  
1 = true  
2 = false  
3 = true  
  

Then on the level select screen this code is supposed to go through the levels table and show the correct stars but at the moment it’s only showing the first star, no matter what 1, 2 or 3 are set to?

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  

It looks to be loading the data correctly and putting it into the levels table correctly but only the first star is showing?

Can anyone spot what i’m missing here?

Hope this makes sense, thanks in advance.

Lee [import]uid: 13654 topic_id: 5352 reply_id: 18640[/import]

I’m pretty sure line 8 from your first code block is your problem:

levels = { "\n" .. contents }  

That would require Lua to compile the bare string as Lua code, which Corona doesn’t let you do:

http://developer.anscamobile.com/content/changes-lua

You have to do some more work to read the data into your table. As it happens, I’m trying to figure out a good way to do this myself. If I figure anything out, I’ll let you know.
[import]uid: 9659 topic_id: 5352 reply_id: 18668[/import]