How do I get a map variable to update values properly?

I made a map variable, an enum for ammo type, and an integer to go with it. I thought this would have been the cleanest way to handle this, instead of making several ammo integers, especially in the instance I add more ammo types in the future.

However, it’s giving me some issues and I can’t really wrap my head around what is happening here. It seems like it would have just been easier to go with multiple single integer variables, but maybe I am missing something critical.

First of all I’m using GAS, with GAS Companion from the Unreal Marketplace to make things easier with blueprint, just so you have a better idea of how it’s all working together.

So I have this ammo type struct used with the weapon itself, to be able to identify which ammo to reference. I can easily get a reference to the weapon, and set its ammo no problem. The player has the map variable to know how much the player is carrying of each ammo type.

I have a Reload gameplay ability. The easiest way I could figure out modifying this map variable seems overly complex…

I’ve found for whatever reason, Set Array Elem doesn’t change the value at all. My first question is, why can’t I use this and when can I use this?

So I did some Googling, and the recommendation is to do a Remove, then Add with the new value.

I did this, and it works. But the map variable isn’t being affected anywhere else. My UI for example gets the value from this variable, and it stays stagnant. On the player, I have it printing the amount of each ammo type on tick (to debug), and it does not change. However, I have the ability printing the amount, and it does change as expected. So the ability will work as intended, it will subtract the ammo from my player and add it to the weapon, and if the player is out of ammo it will not add anything, etc. But I can’t for the life of me get it to show elsewhere.

This happens on both client and standalone/server. I don’t believe it’s a replication issue, as even if I print the amounts with authority the server amount does not change. Well, it changes how I expect printing from the ability, but not from other BPs.

I am getting the ammo reserve on the player, and I originally tried using Find to hook up the ammo type with the weapon to get the correct ammo type, and using that value. It seemed like a nice and clean simple way of doing it. It does not update. If I initially have 10 ammo on the player for example, it will always reload with 10 ammo.

But, if I take the ammo map variable, get the Keys, do a For Each Loop with Break, set the array index to a variable, checking if the key array element = the weapon ammo type, breaking the loop when true… I can then get the values of the ammo map variable, Get (a ref) – didn’t make a differnece if it was a ref or a copy by the way – using the array index variable that was previously set… THAT will always get a correctly updated amount. i.e. 10 ammo on the player gets put into the clip, it will be 0 ammo.

Thing is, in another BP, if I do either method, I can’t get the updated value either. I can’t comprehend what is going on here.

Why? Should I just not use a map? What is happening here that is making this so weird?

You can’t set an array element on a map – it’s not an array, it’s a map.

As a regular map can only have one entry per key, you just Add a new entry with an existing key, and it will replace the old one.

1 Like

That makes sense, but I only tried picking it because it showed as a contextual option and allows input from a map.

I did the add instead of remove and add.

I managed to get the UI to have the proper ammo on the player show properly, by using Find and matching it with the ammo enum from the weapon.

However, doing the exact same logic elsewhere, such as a character or component BP, it only shows the initial value and won’t update. It isn’t making sense to me.