[Question to Expert BPers] Finding the Next Index in an Bool Array that is "True" - For Weapon Switching/Unlock System? Bit of a Challenge

Hey Folks, Some context before the question.

This is probably for more advanced Blueprint scripters and programmers.

Context: I’m trying to make a weapon switching system based on the mouse scroll wheel input, As the player scrolls up or down, it increments or decrements respectively either plus or minus 1 byte, this then checks if the player has unlocked the weapon to begin with against an array index, if true then sets the weapon, if false sets to next true index in array.

  • I have an Enumeration for the Weapons
    set up with the Weapons listed.

  • I have an array of bools (that’s
    supposed to check unlocked weapons against the Enum).

How do I increment and decrease through ONLY unlocked weapons, whilst skipping locked weapons?

For example- if in the Enum 0=Pistol, 1=Shotgun, 2=MP5, 4=RPG and only the Pistol and RPG is Unlocked (set to true in separate Boolean array). How can I scroll between these two only? Which would be switching between the 0 and 3 index. -These values would then ultimately be used to drive the WeaponEnum setter, to which I can then use an Enum Switch to drive the weapon firing functionality.

I hope that explains my predicament, Any help would be absolutely appreciated! Thank you.

I would probably make a struct of your enum and bool value, then make an array of those to iterate through more easily.

Otherwise I think you can do something like this;

Basically increment an int, while flipping through your enum, to find your start point (since enum index isn’t set). Then flip through our unlocked bool array based on that starting point, until we find the next unlocked one, and then set our weapon enum to the new value.

This only does the increment, you should be able to modify it easily enough to decrement as well.

Thank you very much for replying, I could definitely use this method too, but there are always one million different ways to achieve something in UE4.

I managed to figure it out by purely using Enums, An Enum Array (Holding the unlocked weapons) on the Character and an Enum switch when Using the weapons.

For Example, When the player purchases a weapon, It simply adds the new Enum by name to the player’s “Unlocked” enum array. I can then simply scroll through the array using input from the mouse wheel to switch between the unlocked weapons!

Thanks again! I really appreciate it.