Update value from dictionary?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By 9BitStrider

Okay, I have all the global values for weapon energy stored within a dictionary. What I WANT to do is add energy to those values when the player grabs a weapon energy pickup.

if wpn_en > 0 and heal_delay == 1:
		
		var wpn_name = {
					1 : global.rp_coil[int($player.swap) + 1],
					2 : global.rp_jet[int($player.swap) + 1],
					3 : global.weapon1[int($player.swap) + 1],
					4 : global.weapon2[int($player.swap) + 1],
					5 : global.weapon3[int($player.swap) + 1],
					6 : global.weapon4[int($player.swap) + 1],
					7 : global.weapon5[int($player.swap) + 1],
					8 : global.weapon6[int($player.swap) + 1],
					9 : global.weapon7[int($player.swap) + 1],
					10 : global.weapon8[int($player.swap) + 1],
					11 : global.beat[int($player.swap) + 1],
					12 : global.tango[int($player.swap) + 1],
					13 : global.reggae[int($player.swap) + 1]
					}
		
		if global.player_weap[int($player.swap)] > 0 and global.player_weap[int($player.swap)] < 11:
			id = global.player_weap[int($player.swap)]
		else:
			id = global.player_weap[int($player.swap)] + global.player
		
		wpn_name[id] += 10
		wpn_en -= 10

The way I have this above, the value isn’t added properly and the global value isn’t updated. How would I be able to accomplish this?

:bust_in_silhouette: Reply From: Zylann

When following your code, I see that wpn_name is a temporary variable. Its keys are weapon IDs and values are, I assume, integers representing the energy of the weapon (which you obtain using some sort of player ID?).

Next you determine what is the ID of the weapon.

Finally, you increase the number within wpn_name corresponding to that weapon ID.

However, remember, wpn_name is a temporary variable. And what’s inside the dictionary are integers which you had previously copied. Integers are not references, so globals don’t get updated.
You need to copy the value back to the global dictionary if you want it to persist.

Another way though, is to not get the value, but the container holding it. Containers are references (Array and Dictionary), so modifying one will be equivalent to modify the global:

if wpn_en > 0 and heal_delay == 1:

    var player_swap = int($player.swap)

    var wpn_name = {
        1 : global.rp_coil,
        2 : global.rp_jet,
        3 : global.weapon1,
        4 : global.weapon2,
        5 : global.weapon3,
        6 : global.weapon4,
        7 : global.weapon5,
        8 : global.weapon6,
        9 : global.weapon7,
        10 : global.weapon8,
        11 : global.beat,
        12 : global.tango,
        13 : global.reggae
    }

    if global.player_weap[player_swap] > 0 and global.player_weap[player_swap] < 11:
        id = global.player_weap[player_swap]
    else:
        id = global.player_weap[player_swap] + global.player

    wpn_name[id][player_swap + 1] += 10
    wpn_en -= 10

(I didn’t test this code, you’ll need to figure out if I’m right with the rest of the code)

Thank you. This worked perfectly.

9BitStrider | 2019-10-16 14:28