Download

Weapon switching for FPS

So I’m making an FPS with 3 weapons and using a child class system for the weapons. I can’t really figure out how to keep the weapon data (Ammo count etc) in these weapons when I switch weapons. I would like to use the numericals as the switch system i.e pressing 1,2,3 for the different weapons. Would really be glad if someone helped.

It really depends how you’re thinking of doing it, but typically, it makes most sense to keep the info in a save game.

Even if you did find a way to keep it in the weapons, when you stopped playing and started again, the system wouldn’t know what was what.

When the player picks up weapon X, you actually spawn that weapon, right? When the player quits and re-starts the game, they need to know what weapons they had. That has to go in a save game, so the rest of the weapon info might as well also.

If you don’t care about saving the game, then you can keep the info in the player.

Here’s something about save games:

I was trying to save the data in a blueprint data structure. I just wasn’t able to arrange them in an array data structure using a integer variable as the index. But it was not saving the data and was buggy and behaved weirdly. Any idea how i can implement this?

I’m working on an open world FPS and I store pick up items (weapons, ammo etc) in an actor component inventory system. The weapon stores mag ammo count in itself. It reloads from inventory supply.

The Inventory System itself has an actor object reference slot for each weapon (Primary, Secondary, Pistol).

Each Slot has a dedicated item input key (e.g. 1, 2, 3)
On Press 1 -> equip primary slot weapon -> Set “Equipped Weapon” (IS_Weapon_SK AO reference) = Struct Primary -> set “Current Equipped Slot” (enum [weapon slot]) etc and so forth.

IS_struct.jpg

When a weapon is picked up (looted) I set a reference for it in the struct.

If it’s equipped (in hand) I set “Equipped Weapon” (IS_Weapon_SK AO reference). This var is the go to reference for the weapon being used at the current moment in time. All specs, firing, reloading logic are here.

When I shoot or reload, the “Equipped Weapon” is what’s making the ammo changes. I shoot, deduct local ammo… reload, increase local, deduct from IS ammo supply.

When I swap weapons I use the Equipped Weapon to “re-set” the appropriate slot, then set self to the new weapon.
e.g. Equipped weapon → get Current Equipped Slot → (primary)… update struct Primary Slot = equipped weapon… Set “Equipped Weapon” (Secondary/Pistol etc), set “Current Equipped Slot” (enum [weapon slot]).

On Weapon Drop I grab all ammo in the weapon and add it to the Inventory ammo pool.

It’s a pretty complex process. Key for me is to keep all “item” logic in the Items class. current ammo, ammo logic (deduction,reloading), firing logic etc. None of this needs to be in my character class. All it needs is an input that calls an event in the item class.

e.g.

Fire Pressed -> is equipped? -> Is Valid (Equipped Weapon) -> Equipped Weapon (Fire Event)
Secondary Fire Pressed -> is equipped? -> Is Valid (Equipped Weapon) -> Equipped Weapon (Secondary Fire Event)
Reload Pressed -> is equipped? -> Is Valid (Equipped Weapon) -> Equipped Weapon (Reload Event)
ADS/Pressed -> is equipped? -> Is Valid (Equipped Weapon) -> Equipped Weapon (ADS Event)

There’s a lot more logic to the above examples, but it’s enough to get my point across.

This is quite good but a bit too complex for what I need. My weapon system is more like this tutorial vid
https://www.youtube.com/watch?v=94HpeqlgKAs&list=PL4G2bSPE_8um-3MIHOTwFwjWZdWO24S9O&index=12.

Problem with this video is that only 2 weapons are shown and i can’t figure out what todo when more than 2 are involved

Keeping with the “authors tutorial” I recommend you use nested branches. It won’t alter “his structure”.

Switch Weapons (Func)-> Class comparison Branch (is primary):
True -> keep same
False -> Class Comparison Branch (is secondary):
True -> Process secondary change
False -> Process as side change


I highly recommend you not put a lot of value behind the structure of the tutorial. As a simple learning exercise it’s fine though.

Really appreciate the help man. I did understand through several tests that this structure is kinda bs and has a **** ton of flaws. I figured I might have to try connecting the inventory with the ammo count. I’m kinda new to Unreal so figuring out the structures is kinda confusing ( I used to work on Unity and weapon switching there was really easy) . Just a quick question, Are there any sort of a static variable kind of a thing in unreal where I can update the variable from another place and it can’t be changed ?

You’re going to have to give me a bit more detail. What are you trying to accomplish with said variable?

It’s fine now man, I figured it out