0

My question is similar to this

I am using godot game engine and it uses gdscript. I request the moderators to let it stay at least till I get a proper reply. I am at the end of my project this is the only thing I can't accomplish. So, hoping someone will help me here.

I upload three dictionaries to google play as a saved game file when, it gives me json data when I back. Now, I want to convert the integer values back to int, boolean back to bool.

My dictonaries look like this:

Dictionary containing information on levels:

GameDataManager.level_info =    'sand': {
        1:{ 
            'unlocked' : true, 
            'high_score' : 0, 
            'stars' : 0,
            'goals' : {
                'score' : 500
            },
            'level_to_load' : 'res://scenes/levels/sand/1.tscn'
            },
        2:{ 
            'unlocked' : false, 
            'high_score' : 0, 
            'stars' : 0,
            'goals' : {
                'score' : 2400
            },
            'level_to_load' : 'res://scenes/levels/sand/2.tscn'
            }
          },
'ice': {
            1:{ 
                'unlocked' : true, 
                'high_score' : 0, 
                'stars' : 0,
                'goals' : {
                    'score' : 700
                },
                'level_to_load' : 'res://scenes/levels/sand/1.tscn'
                },
            2:{ 
                'unlocked' : false, 
                'high_score' : 0, 
                'stars' : 0,
                'goals' : {
                    'score' : 2400
                },
                'level_to_load' : 'res://scenes/levels/sand/2.tscn'
                }
              }

There is more but this is a sample.

Booster dictionary looks like this:

BoosterManager.booster_info =       'powers' : {
            'plus_bomb' : {
                'total' : 0,
            },
            'hammer' : {
                'total' : 0,
            },
            'swap_pieces' : {
                'total' : 0,
            },
        },
        'booster' : {
            'color_bomb' : {
                'total' : 0,
                'set' : false,
            },
            'adjacent_bomb' : {
                'total' : 0,
                'set' : false,
            },
            'column_or_row_bomb' : {
                'total' : 0,
                'set' : false,
            },
        }
    }

Now when I load this data back I do this for converting the data types back to their original state:

func _on_game_load_success(data):
    var game_data: Dictionary = parse_json(data)
    GameDataManager.level_info.clear()
    BoosterManager.booster_info.clear()
    ChipsManager.chips_info.clear()
    GameDataManager.level_info = game_data['level_info']
    BoosterManager.booster_info = game_data['booster_info']
    ChipsManager.chips_info = game_data['chips_info']
    for i in GameDataManager.level_info:
        var temp_i = GameDataManager.level_info[i]
        str(i)
        GameDataManager.level_info[i] = temp_i
        for j in GameDataManager.level_info[i]:
            var temp = GameDataManager.level_info[i][j]
            int(j)
            GameDataManager.level_info[i][j] = temp
            GameDataManager.level_info[i][j]['high_score'] = int(GameDataManager.level_info[i][j]['high_score'])
            GameDataManager.level_info[i][j]['stars'] = int(GameDataManager.level_info[i][j]['stars'])
            GameDataManager.level_info[i][j]['unlocked'] = bool(GameDataManager.level_info[i][j]['unlocked'])
            for k in GameDataManager.level_info[i][j]['goals']:
                GameDataManager.level_info[i][j]['goals'][k] = int(GameDataManager.level_info[i][j]['goals'][k])
    for i in BoosterManager.booster_info['booster']:
        BoosterManager.booster_info['booster'][i]['total'] = int(BoosterManager.booster_info['booster'][i]['total'])
        BoosterManager.booster_info['booster'][i]['set'] = bool(BoosterManager.booster_info['booster']['set'])
    for i in BoosterManager.booster_info['power']:
        BoosterManager.booster_info['power'][i]['total'] = int(BoosterManager.booster_info['booster'][i]['power'])
    GameDataManager.save_data()
    BoosterManager.save_data()

But when I try to reference it like this GameDataManager.level_info[lvl_type][lvl_num] where lvl_type is string and lvl_num is int I get a null error. Can anyone help please?

Theraot
  • 31,890
  • 5
  • 57
  • 86
  • In Python, it is a common mistake to modify the dictionary which you are iterating on. Maybe there is a similar behavior here, and you should iterate on a copy of `GameDataManager.level_info`. – Frodon Dec 02 '20 at 16:48
  • Haven't tried that. I will try it and get back to you. – Hamza Memon Dec 02 '20 at 16:53
  • @HamzaMemon Did you solved it? – Spyrex Feb 02 '21 at 15:29
  • Removed python tag. – Theraot Apr 24 '21 at 11:57
  • I could not get the json to parse. You didn't describe the error either. I'm giving up on reproducing the problem. See [How to create a Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) – Theraot Apr 24 '21 at 12:08
  • @Spyrex Yes. I am sorry for the late response. Had not visited stack overflow since quite some time. I have pasted the answer below and it works. Basically, I am just looping through all key value pairs and converting them back. – Hamza Memon Apr 25 '21 at 12:31
  • @Theraot Maybe you didn't read the last two line, when I try to reference them I get an error.Also I have already posted how my dictionaries look like and the code I am using to try to convert, I don't think one would need anything else to recreate the problem. Yes the question is long but I had to show the code I had written along with a sample. – Hamza Memon Apr 25 '21 at 12:35

1 Answers1

0

I solved the problem but forgot to post it here. This is the code I came up with and it works.

func _on_game_load_success(data):
    var game_data: Dictionary = parse_json(data)
    var new_dict = {
        'sand' : {},
        'ice' : {},
        'spooky' : {},
        'city' : {}
    }
    for i in game_data.level_info:
        if i in new_dict.keys():
            for j in game_data.level_info[i]:
                new_dict[i][int(j)] = game_data.level_info[i][j]
        else:
            new_dict[i] = GameDataManager.level_info[i]
    for i in new_dict:
        for j in new_dict[i]:
            new_dict[i][j]['stars'] = int(new_dict[i][j]['stars'])
            new_dict[i][j]['high_score'] = int(new_dict[i][j]['high_score'])
            new_dict[i][j]['unlocked'] = bool(new_dict[i][j]['unlocked'])
            for k in new_dict[i][j]['goals']:
                new_dict[i][j]['goals'][k] = int(new_dict[i][j]['goals'][k])
    GameDataManager.level_info.clear()
    GameDataManager.level_info = new_dict
    BoosterManager.booster_info.clear()
    BoosterManager.booster_info = game_data.booster_info
    for i in BoosterManager.booster_info['booster']:
        BoosterManager.booster_info['booster'][i]['set'] = bool(BoosterManager.booster_info['booster'][i]['set'])
        BoosterManager.booster_info['booster'][i]['total'] = int(BoosterManager.booster_info['booster'][i]['total'])
    for i in BoosterManager.booster_info['powers']:
        BoosterManager.booster_info['powers'][i]['total'] = int(BoosterManager.booster_info['powers'][i]['total'])
    ChipsManager.chips_info.clear()
    ChipsManager.chips_info = game_data.chips_info
    ChipsManager.chips_info['chips'] = int(ChipsManager.chips_info['chips'])
    GameDataManager.save_data()
    BoosterManager.save_data()
    ChipsManager.save_data()