- Edited
Hi people !
I'm working on a game with a locked/unlocked levels grid. I already try something but it's doesn't run like I expected.
What I need to do : - Save in a JSON file dictionnaries for each levels with levels name, levels path,levels locked state. - Be able to use this JSON to update the levels grid with the good locked state for each level
What I already try :
- I've read the Godot documentation about saving game
- I've created two scenes test, one with a button to unlock the other.
- in a singleton script called Level Manager I wrote this :
## LEVEL MANAGER : level dictionnaries
var lvl1 = {
name = "lvl 1",
path = "res://levels/level1.xml",
locked = false
}
var lvl2 = {
name = "lvl 2",
path = "res://levels/level2.xml",
locked = true
}
func grid() :
var grid = [lvl1,lvl2]
return grid
`## LEVEL MANAGER : save function
func save_game():
var savegame= File.new()
savegame.open("res://data/savegame.bin", File.WRITE)
Save levels grid data
var grid = grid()
for level in grid :
var data = level
savegame.store_line(data.to_json())
savegame.close()## LEVEL MANAGER : load function
func load_game() :
var savegame = File.new()
var newGrid = []
if !savegame.file_exists("res://data/savegame.bin"):
return #Error! We don't have a save to load
var currentline = {} # dict.parse_json() requires a declared dict.
savegame.open("res://data/savegame.bin", File.READ)
while (!savegame.eof_reached()):
currentline.parse_json(savegame.get_line())
newGrid.append(currentline)
savegame.close()
return newGrid`
PROBLEMS 1) load_game() return an array with three line filled with the level 2 dict, instead of a line lvl1 and a line lvl2, why ? 2) I don't know how to change a dictionnary values 3)How can I wrote a unlocked_next_level() function without write the next level path ? example : I'm on the first level. I click on the unlock level 2 button which call unlocked_next_level(). the function know that i'm on the first level and that the level 2 is the second thanks to JSON file.
What I'm not sure how to do 4) Is it a good idea to make a dictionnary for each level ?