How can i get my weapons to remember their ammo count when switched back to?

I have my weapons set up in an array for switching, i pick them up off the ground which destroys the pickup, but when i scroll back to the weapon i have already fired the ammo is refreshed. i just want the weapon to remember its previous ammo count? Any ideas?

I am very new to this so apologies if this is an easy fix.

Yeah, inside your weapon create a variable for AmmoCount and make it an integer and set its value to however much ammo you want to start with. Then when you fire the weapon, get the AmmoCount and then subtract 1 from it. Then set this as the new AmmoCount. Then whenever you switch weapons, it will know what the ammo count is. The only reason it’s resetting is that you’re not keeping track of it decreasing.
Finally, you’ll have to code a system to handle what happens when you run out of ammo. I.E. play a reload animation and then set the AmmoCount back to its default value.

Thank you for answering, the attachment below shows the system i have already set up. Is this what you mean? if so this still doesnt remember the previous ammo count, it depletes as it should whilst firing but when i go back to the gun it is back at the starting ammount of ammo.

279681-ammocount.png

Oh, I think it’s because you’re spawning an actor of that specific weapon class when you switch weapons, is that correct? That’s going to result in the actor spawning at the default values. Store the ammo count for each weapon in the player controller and then pull from there after you spawn the new weapon. So create a var for each weapon’s ammo inside the player controller like ShotgunAmmo, PistolAmmo etc. and then at the beginning of your switch weapon event, first get the current ammo count and set the corresponding ammo count in your player controller before running the rest of the switch code. Then you’ll always have the value saved even if your player character dies.

Yes i believe that’s how it works, see what your saying makes sense…now putting your words into action is the difficult part, well at least for me lol. Can this work even though i’m using the array above?

so are you saying delete the ammo count var from the weapon master completely? and have the ammo counts in the player?

Nope. Keep the ammo count var in the weapon and also create another one for each weapon inside the Player Controller - Specifically the controller and not the player otherwise you’ll run into the same issue again when the player dies and respawns. To do this, open up whatever player controller you’re using (defined in the game mode BP) and then add in an integer var for each weapon you have in the game so ShotgunCurrentAmmo, PistolCurrentAmmo etc. Then from wherever you are calling the Spawn Weapon function, just before you do that, get the AmmoAmount from the weapon in question, then add a “GetPlayerController” node, drag off from there and cast to your specific player controller and then you’ll be able to drag out from that pin and set your specific weapon’s CurrentAmmo. So drag out and type Set ShotgunCurrentAmmo for example. Then just plug the AmmoAmount from the currently equipped weapon into the CurrentAmmo var for that same weapon type and you should be good to go.

I’d give you more specific instructions with pictures but you didn’t post all of your code so I have no idea when you’re calling the SpawnWeapon function. Did you follow a tutorial to put this together or is it something you designed yourself?

if you remove a actor from the level and then respawn that item, it will spawn with default values. put the ammo count variable in a place which is not destroyed till the end of the level etc. also i would recommend creating a component instead and storing ammo values and all info of each weapon in there using a structure array. and attach the component to the character.

Hey man,

Firstly I appreciate you putting the time into helping me out here, this is from a tutorial i followed but i setup the ammo system myself which is probably why it doesnt work as i want it to lol. See the attached picture to see the rest of the code for when Spawn Weapon Function is called.

You’re welcome man, it’s the only way to learn really :slight_smile:
Okay, so if I understand things correctly what you’re doing right now is grabbing each weapon and storing them in an array and then pulling from the array to switch back to that weapon? I’m guessing the array is an array of your weapon types so each weapon is stored at a specific index correct?
I’m trying to figure out when the most efficient time to store and recall the ammo would be and it depends on how dynamic the code is. If each weapon has a hardcoded index in an array of those weapon types, then it’s a lot easier. If they all get stored at a dynamically changing index, then it’s more difficult.
From what I can see of this network though, you’d want to grab the CurrentAmmo from the player controller and set it in between the delay and the SpawnWeaponF function call. The difficulty is matching up the index of the weapon with it’s correct ammo type. If you know how to do that then you’re laughing.

As to SupDeity’s point, creating a component might help make the system more re-usable but honestly, it’s just another way to store information so if you’re not re-using said component multiple times, there’s no real benefit.

Well so far i now have the ammo depleting from an AmmoAmount Variable in the player character but all guns are sharing the same ammo pool, for obvious reasons. I’ve left the player controller out of it for now as 1. im not sure how to mess with that and 2. im not planning on having the player respawn once dead.

I tried setting variable after delay, before spawn weapon F and it didn’t seem to have any effect.

Getting the ammo to deplete from where it is now feels on the right track but now i have to figure out a way of getting the weapons to not share the same pool of ammo. I thought about an array for the ammo but im just not sure.

Thanks for the help so far though.

I don’t want to sound rude/mean, but if you’re not sure how to mess with a player controller and you’re running into this many issues trying to get some =ammo working, it might be a good indication that you need some more time with the engine before working on your current project. I know it can be frustrating to hear, but take it from me, it will only make your game better if you work on something else for a while and then come back to this once you’ve got some more experience. I had to do the same with my own project and it was really worth it.
I actually really highly recommend Jonathan Daily’s tutorial series on making games with Blueprint. I purchased a subscription to pluralsight myself when I was looking to get better with blueprint and it was so worth the money. Creating Gameplay Systems using Blueprint Features in Unreal Engine | Pluralsight
I’m in no way affiliated with Pluralsight, they’ve just got some great tutorials up there. You can get a single month’s subscription for $35 and they give you unlimited access to all of their videos so if you really take advantage, you could get a load of experience in a single month.
Either that or just take the time to watch some of the great YouTube videos that are out there… My top recommendations are:
Matthew Palaje - Search his channel for “Let’s create”:
https://www.youtube.com/channel/UCqPKRjVm36xmxu1OiegTWWg/videos

Tesla Dev: https://www.youtube.com/channel/UC3QBWg9pMnaFF-q0qjXPDEg

Mathew Wadstein: https://www.youtube.com/channel/UCOVfF7PfLbRdVEm0hONTrNQ

tell you what man, I read all the posts…

simply put - when you spawn your new weapon actor from class, you need to immediately set it’s ammo count to the stored variable

If where that variable is being stored is the issue, its simply a matter of storing that information elsewhere

That being said, if you’re still struggling with this by next week, I’d be willing to get on discord with you, screen share - and knock this problem out, as this should be relatively simple

let me know…

Haha its fine, your most probably right.

All the tutorials i’ve watched have just never mentioned storing anything in the player controller, it’s always been the player character. See when i locate the controller it’s editable or openable in C++ only so i dunno if i have to create an instance of it or what to be able to use it in blueprint. Anyhow I will check out these links you’ve shared as i do know i have a lot to learn.

Thanks.

Hey vicecore,

Yeah i’m gonna keep on with it with the information i have from all of you and see if i can sort it.

If i can’t and i have exhausted all ideas then i would definitely appreciate some help, so i may be back to take you up on that.

Cheers

Yeah, that’s why I suggested the Pluralsight tutorials because they’re made by professionals so they actually know how to do things in the best way possible. Even the guys at Epic often do things the wrong way and even mention that you shouldn’t do it this way but never explain why and I’m always left thinking why the F did you even show it to us this way then?
Sorry, I can’t be more help, but the entire setup you’re using is a little confusing to look at so it’d take a lot of work for me to figure out what’s going wrong and how to fix it and I don’t have enough free time at the moment.
You really should watch those Matthew Palaje tutorials though. They’ll help you a lot and they’re all really quick.

Hey again everyone

I’ve finally figured it out, so i thought i would share how i managed to get it working! It might be messy to some and may not be the best way but it works!

So first i created the Ammo variables in the Player Character.

280022-variables-stored-in-character.png

Then I created a Custom Event in my Weapon Master BP Named it AmmoCheck. Which I then called in each of the WeaponChild BP’s, I used this event to decrement the ammo fired.

I then called the Ammo check at the end of my Input Fire code (which for me is in Player Character).

At this point all the Weapons were sharing the same ammo pool, which i definitely didn’t want so i ended up creating an Enum of Ammo Types, along side this i created a variable in my WeaponMaster BP of this Enum.

The Key to getting it all working was doing a Check with a Branch (as i was before) but this time with the Enum of ammo types which i set to the actual ammo variables using the select node (drag off the Type Pin to get this node.)

And hey presto! It worked!

I couldn’t have managed this had i not gotten all the answers and information you all provided so i’d just like to say Cheers! And if anyone has a similar issue they can get some sort of answer from the info here.

Good stuff. Glad you got it sorted. Sorry I couldn’t help more than I did.