Unlock, select, unselect characters.

Hi,

Have a little code here. Its unlocks Characters, set selected character as a main and automatically unselect previous one when new one are selected or unlocked. This is just for visual look, when it comes to save and load (with JSON) it’s not what you want to have. 

In really It’s pretty common thing in games, so how do you guys manage things like that?

Create tables which holds any of those separate, set visible, replace, remove from table, how to manage those?

local buttonY = 50 local unlockChar = {} local secelctedChar = {} local select\_unselectChar = {} local secelctedCharTable = {} local unsecelctedCharTable = {} for i = 1, 6 do local function manage(event) display.remove(unlockChar[i]) unlockChar[i] = nil table.insert(secelctedCharTable, 1, secelctedChar[i]) table.remove(secelctedCharTable, 3) secelctedCharTable[1].isVisible = true secelctedCharTable[2].isVisible = false table.insert(unsecelctedCharTable, 1, select\_unselectChar[i]) table.remove(unsecelctedCharTable, 3) unsecelctedCharTable[2].isVisible = true unsecelctedCharTable[1].isVisible = false mainCharacter:removeSelf() mainCharacter = nil mainCharacter = display.newImage("image"..i..".png", 200, 200) end unlockChar[i] = display.newRect(60, buttonY, 40, 40) unlockChar[1].isVisible = false unlockChar[i]:addEventListener("tap", manage) secelctedChar[i] = display.newImage("image"..i..".png", 60, buttonY) secelctedChar[i].isVisible = false secelctedChar[1].isVisible = true select\_unselectChar[i] = display.newImage("image"..i..".png", 60, buttonY) select\_unselectChar[i].alpha = 0.2 select\_unselectChar[i].isVisible = false select\_unselectChar[i]:addEventListener("tap", manage) buttonY = buttonY+70 end table.insert(secelctedCharTable, secelctedChar[1]) table.insert(unsecelctedCharTable, select\_unselectChar[1]) mainCharacter = display.newImage("image1.png", 200, 200)

Okay. More specific question. How I can set all table for isVisible = false and save it permanently? And why, in code above, the table doesn’t hold changed parameters after restart?

local loadsave = require("loadsave") local buttonY = 50 local unlockChar = {} unlockChar = loadsave.loadTable("unlockChar") for i = 1, 6 do local function manage(event) unlockChar[i].isVisible = false loadsave.saveTable(unlockChar, "unlockChar") end unlockChar[i] = display.newRect(60, buttonY, 40, 40) unlockChar[i]:addEventListener("tap", manage) buttonY = buttonY+70 end --unlockChar = loadsave.loadTable("unlockChar")

Have you tried something like GGData? It should resolve your problem.

https://github.com/GlitchGames/GGData

I am using loadsave module, it does the same thing as GGData, and have no problem for saving and loading files or tables with it. But for objects created with for loop and saving updated values of those objects it’s different thing, I just can’t figure out how it works.

According to these two tutorials, you need to enter the full name of the file like “unlockChar.json”. Is it possible that you are having a problem about that?

https://coronalabs.com/blog/2014/10/14/tutorial-saving-and-loading-lua-tables-with-json/

https://github.com/robmiracle/Simple-Table-Load-Save-Functions-for-Corona-SDK

Also, have you checked for your code if you are creating a new file at each restart or if the function is properly called?

The name doesn’t make difference. In really I can save those objects into any table I just can’t extract visual effect. Like setting touched images isVisible = false and keeping it as that. 

local loadsave = require("loadsave") local buttonY = 70 local unlockChar = {} for i = 1, 6 do local function setInvisible() unlockChar[i].isVisible = false end unlockChar[i] = display.newRect(70, buttonY, 40, 40) unlockChar[i]:addEventListener("tap", setInvisible) buttonY = buttonY+70 end

If I got it right, you should try adding an if check to your loading code for that.

Something like this:

loadedValue = tableLoaded[i].isVisible if (loadedValue == true) then char.isVisible = true else char.isVisible = false end

I don’t want to make all objects visible or not, but just specific ones, those which are touched, just as example above works. What it doesn’t do it doesn’t save anything what I and want to implement here.

I got it. I’m just saying, it probably saves but you are probably assigning default values to the buttons. That might be the reason you are not seeing the proper outcome.

Can you confirm that it doesn’t save by looking at project sandbox?

While display objects are technically a table, they have a lot of other information with them like the graphical object. The load-save stuff wasn’t designed around saving something like that instead it wants to save tables of strings and numbers and true/false values. It wasn’t meant to save the state of display objects. You may need a separate table that holds data about your object that you want to preserve and when you load it in, then you can apply that data back to the display objects.

Rob

Bgmadclown, Its doesn’t saves definitely.

Rob, that’s means that all data needs to be referred by writing it in before updating, but if there is a lot of objects its not efficient. Or if data would be passed into empty table how it would be applied for that specific object from which data was retrieved after updating? How to find what is what? And anyway I don’t see here how data from loop can be passed to the table without passing the object[i] itself with intention to assign it to the same object[i] again.

Let me back up a bit on this.  The only time you actually need to load data is if your app exits for some reason, such as a crash or the user kills the app. iOS users don’t kill their apps that often, but Android users do with more frequency.  Because of this, you should reasonably save data after you make a change to it, but the information will stay in memory until the app exists, this means you only need to load once.

All display objects will get re-created when the app starts. You can’t save them because the memory addresses for the textures are very likely going to be different each time the app runs. Since it seems you want to have a list of characters that are unlocked or not, maybe you should consider a table like (since I don’t know your game I’m going make up stuff):

local availableCharacters = {}

availableCharacters[1] = {}

availableCharacters[1].name = “Meanie Greenie”

availableCharacters[1].filename = “image1.png”

availableCharacters[1].unlocked = true

availableCharacters[2] = {}

availableCharacters[2].name = “Rocket Red”

availableCharacters[2].filename = “image2.png”

availableCharacters[2].unlocked = false

availableCharacters[3] = {}

availableCharacters[3].name = “Blue the Clue finder”

availableCharacters[3].filename = “image3.png”

availableCharacters[3].unlocked = false

Then you can use loadSave to save and load this table. When you load it and you need to generate your scene, you can loop over the table to create each display object and at that point you know if its locked or not. Use the information in this table to determine what information you need to pass to display.newImage(). 

Rob

I knew it’s easy before I made it complicated. Very great explanation, thanks Rob. 

Thanks for help guys.

Okay. More specific question. How I can set all table for isVisible = false and save it permanently? And why, in code above, the table doesn’t hold changed parameters after restart?

local loadsave = require("loadsave") local buttonY = 50 local unlockChar = {} unlockChar = loadsave.loadTable("unlockChar") for i = 1, 6 do local function manage(event) unlockChar[i].isVisible = false loadsave.saveTable(unlockChar, "unlockChar") end unlockChar[i] = display.newRect(60, buttonY, 40, 40) unlockChar[i]:addEventListener("tap", manage) buttonY = buttonY+70 end --unlockChar = loadsave.loadTable("unlockChar")

Have you tried something like GGData? It should resolve your problem.

https://github.com/GlitchGames/GGData

I am using loadsave module, it does the same thing as GGData, and have no problem for saving and loading files or tables with it. But for objects created with for loop and saving updated values of those objects it’s different thing, I just can’t figure out how it works.

According to these two tutorials, you need to enter the full name of the file like “unlockChar.json”. Is it possible that you are having a problem about that?

https://coronalabs.com/blog/2014/10/14/tutorial-saving-and-loading-lua-tables-with-json/

https://github.com/robmiracle/Simple-Table-Load-Save-Functions-for-Corona-SDK

Also, have you checked for your code if you are creating a new file at each restart or if the function is properly called?

The name doesn’t make difference. In really I can save those objects into any table I just can’t extract visual effect. Like setting touched images isVisible = false and keeping it as that. 

local loadsave = require("loadsave") local buttonY = 70 local unlockChar = {} for i = 1, 6 do local function setInvisible() unlockChar[i].isVisible = false end unlockChar[i] = display.newRect(70, buttonY, 40, 40) unlockChar[i]:addEventListener("tap", setInvisible) buttonY = buttonY+70 end

If I got it right, you should try adding an if check to your loading code for that.

Something like this:

loadedValue = tableLoaded[i].isVisible if (loadedValue == true) then char.isVisible = true else char.isVisible = false end

I don’t want to make all objects visible or not, but just specific ones, those which are touched, just as example above works. What it doesn’t do it doesn’t save anything what I and want to implement here.