How Can I unlock my levels?

Hi there!

In my game I have 5 worlds with 12 levels in one of them, and I would like to do this:

To go to the second world, you have to pass the first, and to go to third, yo hace to pass the second.

To make this is very easy, but I don´t know how to save the current world that you are. For example, I unlock the second world because a pass the first world. I leave the application, and when I run again, the second world is lock.

How Can I do this?

Goodbye! :wink:

Check out Rob’s json tutorial for info on how to save data in your game:

http://omnigeek.robmiracle.com/2012/02/23/need-to-save-your-game-data-in-corona-sdk-check-out-this-little-bit-of-code/

Thanks for answering, but I tried this:

local loadsave = require(“loadsave”)

myTable = {}

myTable.level = 0

loadsave.saveTable(myTable,  “myTable.json”)

Then I changed the value of mytable.level and update the table:

myTable.level = 0

loadsave.saveTable(myTable,  “myTable.json”)

I can enter to next level, but when I close my application, the value change and I can´t enter to the nest level.

What Can I do?

Without seeing your level code, I’m going to assume that you’re listening for a specific myTable.level value for the first level, instead of a range of values. For example:

if myTable.level == 1 then

Instead of 

if myTable.level \>= 1 then

That is just me shooting from the hip. If you provide more of your code it would help in troubleshooting.

I am going to explain my game.

Fisrt, I have a menu with storyboard. In this code, there are 5 buttons and the code that I put before. In onRelease of all buttons, I have a function that send you to next level. To go to the next level the variable of myTable.level has to be in 1. When I put this value?

The first button of the menu is unlocked, and send you to the first level of the first world. It continue to the level 12, and in this level I changed the myTable.level to 1 and I put “loadsave.saveTable(myTable,  “myTable.json”)” to update the table.

When I pass the level 12, it send you to the menu. Here, I can use the second button. But, when I relaunch the game, I can´t push the second button unless I pass the first.

I think that I don´t save the table in the json file, because I dont´t see it in my game directory :S

What Can I do?

First off, the .json files would normally end up in the resource directory on your machine. it would look something like this:

AppData\Roaming\Corona Labs\Corona Simulator\Sandbox\mytestgameasidaosbdfaq8789adbs9a9s8

You can find it by clicking on file, then Show Project Sandbox. Make sure you have implemented the .json code correctly first, and use the test examples to confirm that files are in fact being created in this directory.

Explaining how your game should work is always good, but without the actual code that your menu screen running, it’s difficult to tell exactly where you went wrong. Maybe you just need to work on the json usage; if you don’t have .json files in the directory listed above, it’s safe to say you haven’t.

*UPDATE*

BTW I just suggested .json because it’s a popular option that I use for saving info. It’s definitely possible to use other methods to save values between sessions. Sql, ICE and others are available in the code exchange. I just use .json, I find it to be good as far as size and ease of use, and Rob made it super-easy to implement. My game Eagle Screech! uses it well (humblebrag) so I know that it works, but there are other options available as well

I have the .json file with the word “null”.

Here is the code of the menu: http://pastebin.com/C3MuetHC

I hope you understand the code :stuck_out_tongue:

Thanks!

If the .json file is populated with only null, that means that a function is essentially corrupting the .json string you are attempting to save. I can see that you don’t appear to have it implemented correctly, as there is no .json module required at the beginning of your file.

I’d suggest starting from the beginning with the .json tutorial examples. They do work, and will give you a better understanding of how to save data between game sessions. 

I have the .json module required, because I am using this: https://github.com/robmiracle/Simple-Table-Load-Save-Functions-for-Corona-SDK

Ah, my bad. I’m not really familiar with how Rob wrote his json library. However, taking a look at your code, it might be because in the below line

loadsave.saveTable(storyboard.clevel, "clevel.json")

I don’t see a table for storyboard.clevel listed. I do see one for storyboard.state.clevel, so you might try that.

Also, Rob would have to weigh in here, but I don’t think it’s the best idea to use the storyboard prefix as Corona already has this term earmarked for a module. Changing it might be in your best interests moving forward, but I could be wrong on that.

I solve writing this: loadsave.saveTable(storyboard.state.clevel, “clevel.json”)

But, I know the problem.

In the menu, I give the value 0 to the variable. When I pass the level 12, I change the value to 1. Then, the program return to the  menu, and I can go to the second world. I relaunch the game, and the value change to 0.

What Can I do to change once the variable´s value?

PD:

Menu (varibale = 0 ) -> Level 12 (variable = 1) -> Menu (variable = 1) -> Can go to world 2.

Relaunch.

Menu (variable = 0 )

The menu is always the same.

Check out Rob’s json tutorial for info on how to save data in your game:

http://omnigeek.robmiracle.com/2012/02/23/need-to-save-your-game-data-in-corona-sdk-check-out-this-little-bit-of-code/

Thanks for answering, but I tried this:

local loadsave = require(“loadsave”)

myTable = {}

myTable.level = 0

loadsave.saveTable(myTable,  “myTable.json”)

Then I changed the value of mytable.level and update the table:

myTable.level = 0

loadsave.saveTable(myTable,  “myTable.json”)

I can enter to next level, but when I close my application, the value change and I can´t enter to the nest level.

What Can I do?

Without seeing your level code, I’m going to assume that you’re listening for a specific myTable.level value for the first level, instead of a range of values. For example:

if myTable.level == 1 then

Instead of 

if myTable.level \>= 1 then

That is just me shooting from the hip. If you provide more of your code it would help in troubleshooting.

I am going to explain my game.

Fisrt, I have a menu with storyboard. In this code, there are 5 buttons and the code that I put before. In onRelease of all buttons, I have a function that send you to next level. To go to the next level the variable of myTable.level has to be in 1. When I put this value?

The first button of the menu is unlocked, and send you to the first level of the first world. It continue to the level 12, and in this level I changed the myTable.level to 1 and I put “loadsave.saveTable(myTable,  “myTable.json”)” to update the table.

When I pass the level 12, it send you to the menu. Here, I can use the second button. But, when I relaunch the game, I can´t push the second button unless I pass the first.

I think that I don´t save the table in the json file, because I dont´t see it in my game directory :S

What Can I do?

First off, the .json files would normally end up in the resource directory on your machine. it would look something like this:

AppData\Roaming\Corona Labs\Corona Simulator\Sandbox\mytestgameasidaosbdfaq8789adbs9a9s8

You can find it by clicking on file, then Show Project Sandbox. Make sure you have implemented the .json code correctly first, and use the test examples to confirm that files are in fact being created in this directory.

Explaining how your game should work is always good, but without the actual code that your menu screen running, it’s difficult to tell exactly where you went wrong. Maybe you just need to work on the json usage; if you don’t have .json files in the directory listed above, it’s safe to say you haven’t.

*UPDATE*

BTW I just suggested .json because it’s a popular option that I use for saving info. It’s definitely possible to use other methods to save values between sessions. Sql, ICE and others are available in the code exchange. I just use .json, I find it to be good as far as size and ease of use, and Rob made it super-easy to implement. My game Eagle Screech! uses it well (humblebrag) so I know that it works, but there are other options available as well

I have the .json file with the word “null”.

Here is the code of the menu: http://pastebin.com/C3MuetHC

I hope you understand the code :stuck_out_tongue:

Thanks!

If the .json file is populated with only null, that means that a function is essentially corrupting the .json string you are attempting to save. I can see that you don’t appear to have it implemented correctly, as there is no .json module required at the beginning of your file.

I’d suggest starting from the beginning with the .json tutorial examples. They do work, and will give you a better understanding of how to save data between game sessions. 

I have the .json module required, because I am using this: https://github.com/robmiracle/Simple-Table-Load-Save-Functions-for-Corona-SDK

Ah, my bad. I’m not really familiar with how Rob wrote his json library. However, taking a look at your code, it might be because in the below line

loadsave.saveTable(storyboard.clevel, "clevel.json")

I don’t see a table for storyboard.clevel listed. I do see one for storyboard.state.clevel, so you might try that.

Also, Rob would have to weigh in here, but I don’t think it’s the best idea to use the storyboard prefix as Corona already has this term earmarked for a module. Changing it might be in your best interests moving forward, but I could be wrong on that.