Updating the values of a dictionary's keys

Godot Version
Godot 4.5 stable

Question
I use a dictionary to stock the data of my game’s weapons, including the current ammo count, like these:

var weapons = {
		1 : {
			name = "Type 45 pistol",
			damage = 1,
			currentAmmo = 15,
			fireAnimation = "pistol_fire",
			idleAnimation = "pistol_idle",
			pellets = 1,
			dispersion = 0,
			rateOfFire = 0.9,
			equipped = true
		},

But I don’t manage to update the ammo count (currentAmmo) when taking a ammo pick-up, who use this line:

WeaponDictionary.weapons[weaponNumber].currentAmmo += ammo

What I’m doing wrong ? Should I use another method ?
Thank

Hi,

The same code works fine on my side:

func _ready():
	var weapons = {
		1 : {
			name = "Type 45 pistol",
			damage = 1,
			currentAmmo = 15,
		}
	}
	
	weapons[1].currentAmmo += 3
	print(weapons[1].currentAmmo)  # Output: 18

Here’s what I would do, in order (you may have done some of these things already):
1/ Check if the code is actually being executed (you can add a print for that).
2/ Check if weaponNumber has a correct value.
3/ Check if ammo is not equal to 0 (the code may work but adding 0 doesn’t do anything).
4/ Check if you’re changing the correct dictionary (could be a duplicated version of another one).

Let me know if that helps.

1 Like

Ok, weaponNumber was not correct, it is now and it work. I feel dumb but thank.

1 Like

You’re not, don’t worry, everybody does mistakes like this :smile:
Just remember to always look for the most basic things first with simple prints, will same you some headaches with debugging your code.

Glad I could help.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.