Level lock/unlocking

No worries, I know you have a life outside the matrix :stuck_out_tongue:

Ok so now in the terminal I have:

2013-01-09 00:39:42.170 Corona Simulator[35179:903]
world1: unlocked=true
world2: unlocked=false
world3: unlocked=false
world4: unlocked=false
world5: unlocked=false
world6: unlocked=false
world7: unlocked=false
2013-01-09 00:39:42.170 Corona Simulator[35179:903] 1 true
2013-01-09 00:39:42.170 Corona Simulator[35179:903] 2 false
2013-01-09 00:39:42.170 Corona Simulator[35179:903] 3 false
2013-01-09 00:39:42.170 Corona Simulator[35179:903] 4 false
2013-01-09 00:39:42.170 Corona Simulator[35179:903] 5 false
2013-01-09 00:39:42.171 Corona Simulator[35179:903] 6 false
2013-01-09 00:39:42.171 Corona Simulator[35179:903] 7 false
2013-01-09 00:39:42.172 Corona Simulator[35179:903] world1 is unlocked!

We good?

I changed W to world as you can tell. This is definitely A LOT more complex than I had anticipated but F it, I’m riding this red pill till the bitter end : ) [import]uid: 72845 topic_id: 34528 reply_id: 138025[/import]

Was wondering if we could continue with this? have the next 2 days off work : ) [import]uid: 72845 topic_id: 34528 reply_id: 139019[/import]

Hello Neo!
OK, let’s move on… next step is a function to individually set (update) one of these values, i.e. if the player has cleared an entire world, you want to set the next one as unlocked.

--first, open your file and set a string "content" to the entire value of it  
local path = system.pathForFile( "worlddata.txt", system.DocumentsDirectory )  
local locfile = io.open( path, "r" )  
local content = locfile:read( "\*a" ) ; io.close( locfile )  
--next, create an empty string "newContent"  
local newContent = ""  
--reopen your file to "write" mode... this will clear the entire thing, as a side note!  
locfile = io.open( path, "w" )  
--set your values... in this case, you want to set the "world3" unlocked to "true"  
local worldToUpdate = "world3"  
local valueToSet = "true"  
--loop through the string "content" and search for the proper pattern.  
--as it progresses line by line, it does one of two things:  
--1) if the first part matches (worldToUpdate), it "fixes" that line and appends it to "newContent"  
--2) if it doesn't match, it simply uses the previous line's content and appends it to "newContent"  
for thisWorld, currentValue in string.gmatch( content, "(%w+):%s([%w\%d\%p]-)\n" ) do  
 if ( thisWorld == worldToUpdate ) then  
 newContent = newContent..worldToUpdate..": unlocked="..valueToSet.."\n"  
 else  
 newContent = newContent..thisWorld..": "..currentValue.."\n"  
 end  
end  
--loop is done... now write "newContent" to your text file  
locfile:write( newContent )  
--close your file and nil out all local variables.  
io.close( locfile ) ; path = nil ; content = nil ; newContent = nil ; locfile = nil  
--DONE!  

Let me know if this works… I’m typing all of this really, really fast so I possibly made a small syntax error somewhere. :slight_smile:
[import]uid: 200026 topic_id: 34528 reply_id: 139040[/import]

Assuming I put this in my main.lua my terminal output is.
2013-01-15 13:14:50.836 Corona Simulator[10810:903]
world1: unlocked=true
world2: unlocked=false
world3: unlocked=true
world4: unlocked=false
world5: unlocked=false
world6: unlocked=false
world7: unlocked=false
2013-01-15 13:14:50.836 Corona Simulator[10810:903] 1 true
2013-01-15 13:14:50.837 Corona Simulator[10810:903] 2 false
2013-01-15 13:14:50.837 Corona Simulator[10810:903] 3 true
2013-01-15 13:14:50.837 Corona Simulator[10810:903] 4 false
2013-01-15 13:14:50.837 Corona Simulator[10810:903] 5 false
2013-01-15 13:14:50.837 Corona Simulator[10810:903] 6 false
2013-01-15 13:14:50.838 Corona Simulator[10810:903] 7 false
2013-01-15 13:14:50.838 Corona Simulator[10810:903] world1 is unlocked!
2013-01-15 13:14:53.843 Corona Simulator[10810:903] in load splashscreen file
[import]uid: 72845 topic_id: 34528 reply_id: 139044[/import]

This is really helpful but I wondered what would happen if I provided an update to my app?
So to clarify they install myApp v1.0 and unlock world 1 which I store in the worlddata text file.
I then release myApp v1.1 which they install, will this remove the text file?

Thanks.
E [import]uid: 158620 topic_id: 34528 reply_id: 139065[/import]

@FL,
Yes, that looks correct. So now you have the following methods in your ā€œarsenalā€:

  1. Create the text file on load, if it doesn’t already exist
  2. Read parts of a text file into Lua variable(s)
  3. Update the text file to make changes

And that gives you all the tools to work with file saving/reading, correct? Am I missing anything? The next step is just practicing with string matching patterns, so you can enhance these tools even further (more complicated strings, multiple bits of info in each line, etc.)

@Elliott,
Certainly, the text file will remain after an app update! The files in the Documents folder are only removed if the user completely deletes the app from the device.

Brent [import]uid: 200026 topic_id: 34528 reply_id: 139125[/import]

Sounds good, my next question is how do I translate this to displaying the proper images for locked and unlocked? [import]uid: 72845 topic_id: 34528 reply_id: 139196[/import]

What I would do is store those values in a simple table as you read them from the text file (during the text reading loop, you populate this table). The structure can be as easy as:

local worldsUnlocked = {true,false,false,false,false,...[etc.]}

Then, when you loop through your routine to output the world images (lock images, whatever), you just say:

for i=1,#worldsUnlocked do if ( worldsUnlocked[i] == false ) then --display lock icon over top or something end [import]uid: 200026 topic_id: 34528 reply_id: 139224[/import]

I can tell I’m almost there I just need a little more advice.

So I have

local worldsUnlocked = {true,false,false,false,false,false,false,false}  
for i=1,#worldsUnlocked do  
if ( worldsUnlocked[i] == false ) then  
display.newImage("buttonlocked.png" ) --display lock icon over top or something  
end  
end  

World1 is unlocked and the buttonunlocked.png is displayed. How would I make it so instead of the image popping up on start up, have it appear on my world 2,3,4,etc images instead of the unlocked images. I have world1, world2, world3.png, etc. Thanks for sticking this out with me. [import]uid: 72845 topic_id: 34528 reply_id: 139234[/import]

From a texture memory standpoint, I would suggest just overlaying the same ā€œlockā€ image (with transparent background) over the proper world. But I’m not sure the visual look you’re going for. Of course, if a world is unlocked, you just don’t overlay anything… [import]uid: 200026 topic_id: 34528 reply_id: 139286[/import]

Yea I have no idea how to even begin to do that lol. One of background scrolls and one doesn’t. Incorporating what I’ve learned to what I have already is making me think I need to go to school for this. Are their any sit down class room environments that teach this kind of stuff? [import]uid: 72845 topic_id: 34528 reply_id: 139360[/import]

Hi FL,
Most likely there’s not a ā€œsit downā€ class in your area for learning Corona, but we live in a digital-learning world now, so there are some resources I can recommend:

Brian Burton has several books, catering to new Corona users… they’re even on sale ($5 off) until the end of January (use promo code ā€œCoronaGeekā€):
http://www.burtonsmediagroup.com/books/

Michelle Fernandez has a book as well, on Amazon:
http://www.amazon.com/Corona-Mobile-Game-Development-ebook/dp/B007X3UAE8/ref=pd_sim_kstore_3

I suggest you invest in something like this, as it will introduce you to all of the core topics of Corona… and the prices are very reasonable, if you consider what a ā€œcollege courseā€ would cost (several hundred bucks, over a thousand?)

Brent [import]uid: 200026 topic_id: 34528 reply_id: 139363[/import]

Was wondering if we could continue with this? have the next 2 days off work : ) [import]uid: 72845 topic_id: 34528 reply_id: 139019[/import]

Hello Neo!
OK, let’s move on… next step is a function to individually set (update) one of these values, i.e. if the player has cleared an entire world, you want to set the next one as unlocked.

--first, open your file and set a string "content" to the entire value of it  
local path = system.pathForFile( "worlddata.txt", system.DocumentsDirectory )  
local locfile = io.open( path, "r" )  
local content = locfile:read( "\*a" ) ; io.close( locfile )  
--next, create an empty string "newContent"  
local newContent = ""  
--reopen your file to "write" mode... this will clear the entire thing, as a side note!  
locfile = io.open( path, "w" )  
--set your values... in this case, you want to set the "world3" unlocked to "true"  
local worldToUpdate = "world3"  
local valueToSet = "true"  
--loop through the string "content" and search for the proper pattern.  
--as it progresses line by line, it does one of two things:  
--1) if the first part matches (worldToUpdate), it "fixes" that line and appends it to "newContent"  
--2) if it doesn't match, it simply uses the previous line's content and appends it to "newContent"  
for thisWorld, currentValue in string.gmatch( content, "(%w+):%s([%w\%d\%p]-)\n" ) do  
 if ( thisWorld == worldToUpdate ) then  
 newContent = newContent..worldToUpdate..": unlocked="..valueToSet.."\n"  
 else  
 newContent = newContent..thisWorld..": "..currentValue.."\n"  
 end  
end  
--loop is done... now write "newContent" to your text file  
locfile:write( newContent )  
--close your file and nil out all local variables.  
io.close( locfile ) ; path = nil ; content = nil ; newContent = nil ; locfile = nil  
--DONE!  

Let me know if this works… I’m typing all of this really, really fast so I possibly made a small syntax error somewhere. :slight_smile:
[import]uid: 200026 topic_id: 34528 reply_id: 139040[/import]

Assuming I put this in my main.lua my terminal output is.
2013-01-15 13:14:50.836 Corona Simulator[10810:903]
world1: unlocked=true
world2: unlocked=false
world3: unlocked=true
world4: unlocked=false
world5: unlocked=false
world6: unlocked=false
world7: unlocked=false
2013-01-15 13:14:50.836 Corona Simulator[10810:903] 1 true
2013-01-15 13:14:50.837 Corona Simulator[10810:903] 2 false
2013-01-15 13:14:50.837 Corona Simulator[10810:903] 3 true
2013-01-15 13:14:50.837 Corona Simulator[10810:903] 4 false
2013-01-15 13:14:50.837 Corona Simulator[10810:903] 5 false
2013-01-15 13:14:50.837 Corona Simulator[10810:903] 6 false
2013-01-15 13:14:50.838 Corona Simulator[10810:903] 7 false
2013-01-15 13:14:50.838 Corona Simulator[10810:903] world1 is unlocked!
2013-01-15 13:14:53.843 Corona Simulator[10810:903] in load splashscreen file
[import]uid: 72845 topic_id: 34528 reply_id: 139044[/import]

This is really helpful but I wondered what would happen if I provided an update to my app?
So to clarify they install myApp v1.0 and unlock world 1 which I store in the worlddata text file.
I then release myApp v1.1 which they install, will this remove the text file?

Thanks.
E [import]uid: 158620 topic_id: 34528 reply_id: 139065[/import]

@FL,
Yes, that looks correct. So now you have the following methods in your ā€œarsenalā€:

  1. Create the text file on load, if it doesn’t already exist
  2. Read parts of a text file into Lua variable(s)
  3. Update the text file to make changes

And that gives you all the tools to work with file saving/reading, correct? Am I missing anything? The next step is just practicing with string matching patterns, so you can enhance these tools even further (more complicated strings, multiple bits of info in each line, etc.)

@Elliott,
Certainly, the text file will remain after an app update! The files in the Documents folder are only removed if the user completely deletes the app from the device.

Brent [import]uid: 200026 topic_id: 34528 reply_id: 139125[/import]

Sounds good, my next question is how do I translate this to displaying the proper images for locked and unlocked? [import]uid: 72845 topic_id: 34528 reply_id: 139196[/import]

What I would do is store those values in a simple table as you read them from the text file (during the text reading loop, you populate this table). The structure can be as easy as:

local worldsUnlocked = {true,false,false,false,false,...[etc.]}

Then, when you loop through your routine to output the world images (lock images, whatever), you just say:

for i=1,#worldsUnlocked do if ( worldsUnlocked[i] == false ) then --display lock icon over top or something end [import]uid: 200026 topic_id: 34528 reply_id: 139224[/import]

I can tell I’m almost there I just need a little more advice.

So I have

local worldsUnlocked = {true,false,false,false,false,false,false,false}  
for i=1,#worldsUnlocked do  
if ( worldsUnlocked[i] == false ) then  
display.newImage("buttonlocked.png" ) --display lock icon over top or something  
end  
end  

World1 is unlocked and the buttonunlocked.png is displayed. How would I make it so instead of the image popping up on start up, have it appear on my world 2,3,4,etc images instead of the unlocked images. I have world1, world2, world3.png, etc. Thanks for sticking this out with me. [import]uid: 72845 topic_id: 34528 reply_id: 139234[/import]