0 votes

i have a save system for my game ,i am a newbie with saving data on files , on the save function i have a preety big dictionary with all that i need to save and a load function, but if i add a new 'var' to the save and load function the var is an invalid index until i save on the game again i want to make a for loop that for every invalid index i gave that 'var' the false value

here is the code:

extends Node2D
export var world = 1
var save_path = 'user://save.dat'
var world_data = {}

func Save():
    var data = {}
    data = {
        'hp':globals.hp,
        'max_hp':globals.max_hp,
        'max_mana':globals.max_mana,
        'heal_str':globals.heal_str,
        'pos_x':get_parent().get_node("player").position.x,
        'pos_y':get_parent().get_node("player").position.y,
        'xp':globals.xp,
        'spell3':globals.spells.dart,
        'spell4':globals.spells.polem,
        'boss1_is_alive':globals.bosses.boss1,
        'heal':globals.unlockables.potion,
        'heal_quantity':globals.unlockables.heal_quantity,
        'hp_orb1':globals.unlockables.hp_orb1,
        'mana_orb1':globals.unlockables.mana_orb1,
        'global_heal':globals.unlockables.potion,
        'key1':globals.keys.key1,
        'key2':globals.keys.key2,
        'door1':globals.doors.door1,
        'door2':globals.doors.door2,
        'door3':globals.doors.door3,
        'scene':world,
        'magic1':globals.magic1,
            'magic2':globals.magic2,
        'magic3':globals.magic3,
        'version':3
        }
globals.unlockables.heal_quantity = 10
globals.hp = globals.max_hp
var file = File.new()
var error = file.open_encrypted_with_pass(save_path,File.WRITE,'ola senhora com este belo par de cochas , que transa?kkkkk')
if error == OK:
    file.store_var(data)
    file.close()

func Load():
        var file = File.new()
    if file.file_exists(save_path):
        var error = file.open_encrypted_with_pass(save_path,File.READ,'senha')
    if error == OK:
        world_data = file.get_var()
        _update_stats()
        file.close()

func _update_stats():
for i in world_data.keys():
    if !is_instance_valid(i):
        i = false
match world_data.scene:
    1:
        pass
    2:
        # warning-ignore:return_value_discarded
        get_tree().change_scene("res://worlds/world2.tscn")
globals.loading_area = false
get_parent().get_node("player").position = Vector2(world_data.pos_x,world_data.pos_y)
globals.heal_str = world_data.heal_str
globals.unlockables.pot_is_here = world_data.heal
globals.unlockables.hp_orb1 = world_data.hp_orb1
globals.unlockables.mana_orb1 = world_data.mana_orb1
globals.bosses.boss1 = world_data.boss1_is_alive
globals.xp = world_data.xp
globals.unlockables.heal_quantity = world_data.heal_quantity
globals.unlockables.potion = world_data.global_heal
globals.spells.dart = world_data.spell3
globals.spells.polem = world_data.spell4
globals.max_hp = world_data.max_hp
globals.max_mana = world_data.max_mana
globals.hp = world_data.hp
globals.keys.key1 = world_data.key1
globals.keys.key2 = world_data.key2
globals.doors.door1 = world_data.door1
globals.doors.door2 = world_data.door2
globals.doors.door3 = world_data.door3
globals.magic1 = world_data.magic1
globals.magic2 = world_data.magic2
globals.magic3 = world_data.magic3
get_parent().get_node("player").position.x = world_data.pos_x
get_parent().get_node("player").position.y = world_data.pos_y
globals.is_loaded = false
asked Jul 7 in Projects by big_dumb_dumb (15 points)

1 Answer

+1 vote
Best answer

Hi, so my recommendation for this is always have a variable which holds an emtpy user (the starting point of your player), and when you add new features, you check the existing player's dict against that one which is "hardcoded" to see if there are any missing indexes. Here is what I did for one of my games before I finish loading:

func verifyAndFixPlayerDictionaryIntegrity():
    # verify all player dict keys
    for key in newPlayer.keys():
        if player.keys().find(key) == -1:
            player[key] = newPlayer[key]
    # verify all achievements are present
    for k in ACHIEVEMENTS.keys():
        if player["achievements"].keys().find(k) == -1:
            var achievement = {
                "unlocked": false,
                "value": 0,
                "sent": false
            }
            player["achievements"][k] = achievement
    self.save()

In your case, if you'd like to set the value to false, where it says player[key] = newPlayer[key], you'd have to set it to false and you're good to go.

answered Jul 8 by jtarallo (376 points)
selected Jul 8 by big_dumb_dumb
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.