CurrentAmmo Count Variable doesn't get stored on Clients

Hey there! Another day another replication question from a multiplayer newbie like myself :nerd_face:
I’ll try to make it super nice and clear!

I’m working on a Coop shooter that’ll have basic multiplayer stuff, but the more I read the more confused I get, so let’s pretend I’m absolutely new to the concept of replication (I’m sorry in advance if my wording isn’t technically accurate yet, and feel free to correct me so I can keep on learning)

Right now I’m working on the ability to swap weapons, and on this weapon swap I want to store the CurrentAmmo in another variable so that, when I pull out this weapon later, it has the same amount of ammo.

Here’s how I’ve done it so far:

  1. Weapon swap system: When player presses [1],[2],[3],[4] keys, then it triggers the SwitchWeapon event. This Event doesn’t replicates for now.

  2. The SwitchWeapon first update the Local AnimInstance (since every weapon has its own move set), then, depending on if it is a Client or the Server, makes a server call (is this what we call an RPC?) to Store the ammo of the currently equipped weapon on the PlayerController, before proceeding to Switch the Weapon.

Here is the StoreAmmoEquippedWeapon function, where I’m pulling what weapon type it is to know which ammoType to store:


Should the variables on the PlayerController be replicated here btw?

And here is the WeaponSwitch function, that gets rid of what’s populating the actual CurrentWeapon variable before repopulating it with the correct one (I’ve included only the switch for one of the weapon, otherwise it would have been too small! But others are identical, just spawns different weapons)

The issue now is that, while the CurrentAmmo value is working well on ServerSide, it keeps resetting to default on Clients everytime I’m switching weapons.

Any help is more than welcome! I’ve been trying a lot of stuff the past two days and I’m officially stuck. Don’t hesitate to put some explanation of why I’m wrong, as I really want to learn more about replication :grin:
I hope everything is clear, cheers in advance, guys!

Don’t use GetPlayerController(0) on server side for this, it will always return the first player / local player / server player. If the 2nd player switches weapon you’re gonna store/retrieve ammo from the 1st player.

In character blueprint, you should be able to use “GetController” instead, to get the controller of this character.

Noted! I’ve changed it and now it takes the CurrentAmmo variable that the Server stored, not itself. But that’s definitely progress, thank you!
As a rule of thumb, is it safe to store stuff on the PlayerController, or should I keep it on the PlayerCharacter for replicating stuff?

I think that the issue lies in the Retrieval of the CurrentAmmo variable that is happening OnServer. I’ll try to execute it locally instead, only the storage of the variable will happen on the Server.

I’m still a bit confused about the difference between the OnOwningClients and just a local Event.

No luck! Everything’s fine on the server, but still quite messy on the Client. It seems to always circle back to the MaxAmmo of the first weapon.
If anyone has a clue I’m interested :smiling_face_with_tear:

Using player controller should work but not sure if it makes sense conceptually, it would probably be better in character class, unless you want ammo to persist across respawns.

What is the point of the “Current Ammo” and “Max Ammo” variables in character class? Why not access directly CurrentWeapon->MaxAmmo and CurrentWeapon->CurrentAmmo ?

You say it’s circling back to max ammo of the first weapon, but I don’t see anywhere you setting CurrentAmmo=MaxAmmo, maybe there is a timing issue when you do that, or maybe you are not reading from the right variable ?

I’ll transfer the ammo management back in my character class then! It does work right now (not taking into account my initial issue) but I want to do things as clean as I can, so I learn things the right way.

About the duplication of the “Current Ammo” & “Max Ammo” I believe I’ve overthought the logic quite a lot, actually. Your solution seems to be the one I’m searching for, and that’ll greatly simplify the replication side of it.

I was never really setting my CurrentAmmo onto MaxAmmo, I was trying to store my CurrentAmmo on different variables depending on the weapon I was unequipping, to be able to get that value again on re-equipping it.
I’ll make another pass, transferring everything on my CurrentWeapon class, and I’ll update here with my progress!

Thank you @Chatouille for your help so far!

How is your weapon system setup? In mine I spawn and attach to the character. It’s either in hand (equipped) or on the back (Unequipped). I can have 3 weapons. These are all replicated actors. They have their own replicated inventory (attachments, ammo, cosmetics etc).

Current Ammo is stored directly in the weapons inventory. Unused ammo is stored in the main inventory.

My currentAmmo variable is used for the remaining Ammo in the magazine. As of yet, no ammo goes into the Players’ inventories (it is the next step I wanna implement actually).

But yes, I was way wrong now that I look at your way of doing stuff, and I believe that my confusion about replication is the culprit.

I’ll try to directly set the CurrentAmmo value of the Weapon (I have one MasterBP for structure and multiple ChildBPs for the actual weapons) and since it’s replicated it should work!

I’ll jump on it now and let you know, so we can set this as Resolved. I’m excited :grin:

1 Like

Okay, I know now why I was storing the “CurrentAmmo” data onto my CharacterBP : it because I was destroying my Weapon ChildBP everytime I would switch, to spawn the one I wanted. Which is not ideal when it comes to, well, store data.

As you said, I need to spawn all my weapons first and then just toggle them On and Off. Should I expect a hit on performance doing it that way?