top of page
Search

Dev Log#14 Added Reload Did a bit of maths for the ammo. Learnt about TMaps.

So adding reload functionality to the game required just as much as work in Unreal Editor as it did in Visual studio.


Ammo:

Firstly in order to reload you need ammo so this was created as a replicated variable using its own dedicated rep notify function. This is handled on the weapon class because different types of weapon will require different amounts of ammo. Weapon class is the parent of all weapon classes so putting shared functionality here is just the logical solution.


The ammo functionality just uses a basic clamp function that subtracts 1 from the current ammo and clamps it between 0 and the ammo magazine capacity. the only thing left to do is call the use ammo function when the shoot button is pressed in the combat component after a ammo amount check.  I created a enum class called weapon types and each gun will have its own enum. This is because each weapon will have a different amount of ammo that it can carry. To be able to implement this feature I replicated the ammo reserves which is just an int32 variable.


I also created a TMap that implements the weapon types from the enum I created as the key and the corresponding values are the ammo reserves. I learnt that TMaps use a hash algorithm to associate the key index with the appropriate elements that are known as values. I like to think of them as mini tables. As hash maps are also referred to as hash tables. I use the TMaps function called emplace which assigns a new value to the specified key without creating temporary copies of that new key index, which differentiates it from the add function that does create temporary copies. This leads to unnecessary overhead so it make sense to use emplace as its more optimal.


Maths time this needs its own section to help me and anyone reading this understand.

So lets say our player's current ammo is 3 and the maximum ammo that the gun can hold is 5.

We need to know the amount of room left in the magazine which is simple, the ammo subtracted from the ammo capacity so in this example 5 - 3.

which gives us RoomLeftInMag = 2.

Now the amount we can reload by is also determined by how much ammo we are holding in our reserves. If our AmmoReserves = 3 then we could use our RoomLeftInMag as the amount to reload by but if AmmoReserves = 1 then we can no longer use RoomLeftInMag as the player shouldn't reload back to full ammo, if their ammo reserves is less than RoomLeftInMag.

What we need to do is identify the smaller value between RoomLeftInMag and AmmoReserves using the FMath function "Min" passing in both of these variables. We'll store this in a value called Least. So now we always know which value is smaller RoomLeftInMag or AmmoReserves. We can clamp RoomLeftInMag between 0 and the variable Least then return this value as this will be inside an int32 function since all ammo variables are int32.

So if AmmoReserves = 1 and RoomLeftInMag = 3. Least = 1. AmountToReload = 1.

if AmmoReserves = 3 and RoomLeftInMag = 2. Least = 2. AmountToReload = 2.


The amount to reload formula's result is stored inside an int32 variable called reload amount.

The reload amount is subtracted from the ammo reserves TMap which gets updated and then the ammo reserves replicated variable is set by the updated TMap value.

Reloading:

Inside Unreal's editor I had to create another montage blueprint for that contains a reload animation. What's nice about montages is that it allows you to combine animations with sound cues so that you now have a animation with sound so all you have to do in the code is call the montage blueprint.

Since my weapons have very different meshes one reload animation won't fit all of them. I'll have to adjust each one. The good thing about montages is that you can differentiate between different animations and sound cues through the use of section names.

Using a switch case statement for the weapon types I'm able to change the section name and store this in a local FName variable. Note this has to be identical to the section name in the montage blueprint.


The Animation Instance class has a function called montage jump to section which will allow the montage blueprint to play the appropriate animation and sound cue after passing in the section name.


I also had to a create an enum class for my combat state this is to discern whether or not the player is reloading by default its set to unoccupied which means the player has a gun is walking around. This is because the animation instance class required some more booleans to determine when to use my aim offsets, fabrik and to transform the right hand. These are all features that help place the characters hands correctly on the gun when the character has one equipped. The reload animation needs these disabled otherwise it messes up the reload animation. The combat states provide additional gameplay safety measures for example if the player is in the reloading state then shooting is disabled as you shouldn't be able to reload and shoot.

 
 
 

Comments


bottom of page