Toggling / cycling through weapons

This is a pretty cool part of our functionality - being able to change which weapon is active. There was some different logic we used in this code, did you have any questions about it?

why are we not using this :
var weapons = FindObjectsOfType();
foreach (Weapon weapon in weapons) { }

It would certainly work but it’s very inefficient. Also, if we later add a function that the player doesn’t have all the weapons at hand at the start of the game but can collect them one after the other, it would be really troublesome to disable all of them (and effectively put them into hiding so the player wouldn’t be able to find them (provided they just lie around in the open).

What I did instead (although I commented out in favour of Rick’s implementation, at least for now) is:
foreach( Weapon weapon in GetComponentsInChildren<Weapon>()) { /*...*/ }

I haven’t run it like this but it should select all objects inside the “Weapons” container object that atually are weapons (thus eliminating the issue of having to be careful not to drop a non-weapon object into it), and additionally will automatically access them as Weapon objects, so any other Weapon specific methods one might come up with would be accessible…

1 Like

How to implement this part using New Input System?
The only way i made is like:

private void ProcessKeyInput()
    {
        if (Keyboard.current.digit1Key.wasPressedThisFrame){
            currentWeapon = 0;
        } else 
        if (Keyboard.current.digit2Key.wasPressedThisFrame){
            currentWeapon = 1;
        }
    }

But how to make it in the right way?
How to make it smth like:

void OnWeaponChange(ExactKeyIPressed code){
        if(code == digit1Key) {
            // do smth...
        } else 
        if(code == digit2Key) {
            // do smth else...
        }
    }

In ProcessScrollWheel when scrolling up the weapons cycle from top to bottom in the hierarchy and on scroll down visa versa but when scrolling down after reaching the 1st weapon which is pistol in my case the next switch doest bring out any weapons and leaves player without anything to use, The same thing was happening while scrollling up but changing -1 to -2 in the code fixed it but thats not the case for while scrolling down
Hierarchy Fps Temp

void ProcessScrollWheel()
    {
        if (Input.GetAxis("Mouse ScrollWheel") > 0)
        {
            if (currentWeapon > transform.childCount -2)
            {
                currentWeapon = 0;
            }
            else
            {
                currentWeapon++;
            }
        }

        if (Input.GetAxis("Mouse ScrollWheel") < 0)
        {
            if (currentWeapon < 0)
            {
                currentWeapon = transform.childCount -1;
            }
            else
            {
                currentWeapon--;
            }
        }
    }

when I try to change "if (currentWeapon < 0) {currentWeapon = transform.childCount -1;} "
-1 from -2 my last weapon “Bullpup” weapon doest get cycled and when theres nothing being subtracted the empty weapon state happens twice