Welcome back to another entry of my Dev Diary!
So! I made a reloading system in Unreal Engine 5 (UE5) and I'm super impressed with myself, as I struggled for a long time with it. Unfortunately, there is no epic reload animation, but I was simply trying to create a visible and realistic projectile/bullet count. And by realistic I mean having the remaining bullets in the gun's magazine being dumped upon reload.
As you can see in the gif below, there is an ammo counter in the bottom left - which decreases with every shot fired. Upon pressing the assigned reload key 'R' the gun subtracts a magazine of another 30 bullets from the maximum ammo which is the red figure.
Creating the ammo counter was the hardest part for me, as I had no idea about text binding and referencing within UE5, but I'm really glad I did this, because it's very useful and important to know for future projects.
In my previous Dev Diary entry I showed you how I made the blueprint to stop the gun firing after shooting 30 bullets/projectiles. So there was an issue I found out once I added the ammo counter on the screen. Basically the bullet count would go into the negatives with every click of the input button. This meant I had to add a clamp to the subtraction amount. I set the minimum at zero and the maximum at 30, so that the input action would remove one bullet with every click until the live ammo count hit zero.
So that was a fun little troubleshooting task to figure out! Now onto the big stuff!
First things first, I created a new widget blueprint and made a simple ammo counter. Just a simple 30/210 bullet counter to keep it nice and simple for my first time coding an ammo/reloading based system.
So I managed to get this set up fairly easily and set it up to be visible in the viewport, however, this is where the issue of not knowing how to reference and bind came into play. I couldn't get the X's to display a real time count of the ammo in the gun. I spent a lot of time going through YouTube videos and UE5 forums, but wasn't finding anything that didn't fry my brain. So my professor helped me out and pointed me in the right direction.
In the graph of the widget blueprint, I needed to create a referencing variable which I named 'Weapon_Component' and set it as 'BP Weapon Component type' - which is the name of the blueprint where the gun's firing and reload system is set up.
After this I went back to the Weapon Component's blueprint and added a sequence to the 'Event Begin Play' function. The code is basically adding the XX/XXX ammo counter to the viewport. Then I'm setting the weapon component variable as a reference which will come in to play once I've created the text bind.
All I had to do to create a text bind was 'get' my weapon component variable and attach it to my public ammo variable to reflect the live bullet count.
I then did the exact same with the triple 'XXX' text and bound it to the max ammo variable which is how I got the '30/210' to display in the viewport.
I had all this set up, but no actual reloading system, but it was surprisingly easy to get my head around how to do it! This is the code I came up with:
Once setting up the new input action for reloading, I set the ammo variable to reset to it's original value of 30 every time the input is triggered.
On the completed plug I added a branch (if statement) and set the a condition to prevent the max ammo from going into negatives once the bullet count reaches zero. So if the variable is decided to be true, the ammo max variable will have 30 bullets subtracted from it's total.
There are a few issues with this system, which I hope to iron out in the future. Firstly, upon using all additional magazines of ammo, the gun is still able to reload infinitely. Finally, the gun's max ammo will be reduced by 30 with every press of the 'R' key until it reaches zero.
These are issues that I'll have to look into in the future. I tried using branches and conditionals, but it broke the code. So I'll take some time to think about the issue and come back to it with a fresh pair of eyes, as I feel like this issue shouldn't be extremely complex to resolve.
I am extremely happy with what I managed to make and hope to be able to tackle more challenging tasks in the future. I aim to fix this piece of code until it's fully functional, as this is important to know for future game jams and just my potential future career as well, considering a lot of games feature guns and similar systems in them.
Did you like this post? Tell us