Switching between available / unavailable Weapons in a Class Array

I’m struggling about a very simple feature every old school FPS has which is switching between Weapons that have been picked up by the Player (or given at the start of a game).

I have these Variables in my Switching system :

Weapon Index (Integer), which simply is the current Weapon Index number.
Available Weapon Class Array (Class Array), which is an Array of Classes where the Weapons that have not been picked up (through Pickup Blueprints with Set Array Elem adding the proper Weapons in the “Inventory” so to speak) are set to None (21 in total) - and the picked up ones having the corresponding Weapon Blueprints.

What I want to do : Linearly skipping the Classes that are None (Invalid) when Switching, until it finds a Valid Class.

So that, for example, when I have the Weapons 1 and 3 in my Available Weapon Class Array, skip the Index 2 after 1 and all the rest after 3, comming back to Index 0 (which is always Valid).

I already know about the “Is Valid Class” and “Is Valid Index” nodes - but I have no idea what node should I use to loop an increment when switching.

I guess **For Loop **or For Loop with Break look like what I need, but I’m not sure how “Loop Body” works exactly and how to “Complete” the loop.

Hi @YukiTaiyo

Do you mean ‘For Each Loop’ Node?

https://media.discordapp.net/attachments/817437846578659358/817439348978614282/unknown.png?width=1203

Hi,

I’m not sure about the outputs results in For Each Loop and also in the Branch with the Is Valid Class condition.

Also, I forgot to mention, since Index 0 is always valid, when I switch and I have no Weapons in my Class Array (meaning from 1 to 21), nothing should happen, but instead it switches to Index 1 when True in the Branch.

Here, it switches to 1 from 0 even if I didn’t picked up Weapon 1 and does not go back to 0 when I switch again ; and if I have other Weapons after the 1st Weapon, it doesn’t switch to them either, only switch to 1 in loop.

On weapon pickup you spawn the weapon locally. A reference of the weapon is stored in an Actor Object Reference Array.

If you auto switch to new pickup then after you add to the array you get its “Last Index” and set “Current Index” as that value.

Cycling weapons (Next): On switch If (Last Index == Current Index), Next = 0, Else Next = current +1
Cycling weapons (Previous): On switch If (Current Index == 0), Next = last index, Else Next = current - 1

When you get the Next index attach the current weapon to a back socket, hide the actor, unhide the new actor and attach to hand socket. I’d also have a “Current Weapon” variable that gets updated on switch. You can then refer to “Current Weapon” AOR for logic etc.

This approach only works if all weapons are derived from the same Weapon Class (parent).

e.g. Weapons_Class … All universal weapon logic and variables.

Weapons Array (Weapons_Class Object Reference)

the active weapon class array should only contain valid classes anyway. I personally would not be using classes, i would be using a list of weapon actors.

Yeah the class array element will always be valid. The problem I’m seeing (lack of details in OP post) is no array/anything denoting what he does have (weapons) to determine a true condition.

Beyond that, having to loop on every execution to find out what you have is backasswards and inefficient.

Sorry for the lack of details about my entire setup.

In fact my Weapons have to be each in a specific Index because of the shooting system, and this is why I have to check all the Class Array (which is an Array of Actors).

I wonder if I have to make a manual check for all the next weapons from the index of each weapon in hand (If Weapon Index == X & Next Weapon Index Class is Valid / Invalid… else If Next Weapon is Valid / Invalid… etc… increment X times in Weapon Index).

This way ? It’s a pain to make but at least… it seem to work how it should. There’s just a problem for now : If I have Weapons 1, 2 and 3 for example, it switches directly to the last one available.

I would work out a away to know for fact what you have so you can eliminate the loops/nested branches on weapon switch. You should only really need to loop, if at all, when you add or remove a weapon.

More details on your setup would help us figure out an optimal approach. A walk through on the weapon setup and thought process on the weapon switching logic you’ve implemented.

edit … Is this for a Weapon Mastery type game mode? Weapon switch on progression type thing?

Here’s how my Weapon system works in short :

• In my Player Character, I have an empty Child Actor spawning Weapons Blueprints, and it is also child of an another empty actor simulating the hand.

• The Player Character have a “Trigger” (“Firing”) system / Function where " If Weapon Index (current weapon in hand) is X, then use this Weapon Blueprint (cast to) shooting system (attributes) upon firing". If Weapon Index doesn’t match the Weapon Class, the weapon doesn’t spawn and no shooting occurs.

• As I showed, Player Character has a variable holding the Arrays of weapons, set to None by default except Index 0 (fists).

• Then, the Weapon Pickup Blueprints (only intended for pickup) send the proper information to Player Character through “Set Array Elem”, placing a given Weapon Blueprint into a given Index Number (e.g. : AK47 = 5).

• The Player Character holds all the the ammo amount values as the Child Actors get succesively “destroyed” upon Switching weapons.

As I said, it’s a very classic FPS style of switching weapons. Currently, with the manual setup I made, just have to see why it switches to the last available weapon and how to go back to the fists after switching the last possessed weapon.

As I stated in previous posts you need to have a method of knowing at all times what items you do have. This prevents the need to conditionally loop on previous/next switching.

Here’s an example.

In the Pick up Item Parent class you need minimum two vars. Item Name and Item Class. For each new item you’d create a child class, set those two vars.

In the class that will handle the inventory and switching you need 3 vars. Sorted Inventory array, Haves array, and current item. For the sorted array you add an entry for each potential item. String name only, in your preferred sorted order. Do not select/set a class value. These need to stay null.

In the same class…
Begin overlap is for demo purposes only. Use whatever method you want to call Add Item.

TMAP Add “Overwrites” existing entries. If PP7 exists it will simply update the class value and maintain sort order of the TMap. Hence why you need to presort the array (string).

I’ll take a look at your setup idea. I haven’t thought about checking for the Weapons with Strings ; and in fact “a method of knowing at all times what items you do have”. I’m having a hard time to figure out what I’m doing myself in fact. :o

I would normally use an enum/actor object reference TMap for the sorted array. it makes no logical sense destroying and creating actors on weapon switch. That just eats performance. The classics you refer to never destroyed weapons on switch. They simply hide/unhide.

You’ll figure all this out over time.

Just how did you set 2 Variable Types into 1 Variable for Sorted Array ?

Variable Type … at end of row there’s options. single, array etc. Tmap is the last option if I remember correctly.

That’s excatly it, thanks. :wink:

I’ll give updates about the switch setup.