Better Way? Case & Break/String To Int

Is there a better way than using case and break for this. Everything works great how it is but in my mind i would like expand on my knowledge to better my coding skills. My end goal is to be able to select what gun i want from the inspector via a String and not an Int (My Animator uses SetInterger) so i would have to convert a String to an Int and that’s not something i know how to do proficiently as of yet.

Any help would as always be greatly appreciated.

public class AIAnimation : MonoBehaviour
{
    private aiMovement aiM;
    private Animator anim;
    public int weaponType;
    public GameObject[] activeWeapons;

    void Start () {
        aiM = this.transform.parent.GetComponent<aiMovement>();
        anim = GetComponent<Animator>();
        weaponSelector (weaponType);
    }
  
    void weaponSelector(int weaponType)
    {
        switch (weaponType)
        {
        case 1:
            weaponDeactive();
            activeWeapons[weaponType].SetActive(!activeWeapons[weaponType].activeSelf);
            anim.SetInteger("WeaponType_int", weaponType);
            break;
        case 2:
            weaponDeactive();
            activeWeapons[weaponType].SetActive(!activeWeapons[weaponType].activeSelf);
            anim.SetInteger("WeaponType_int", weaponType);
            break;
        case 3:
            weaponDeactive();
            activeWeapons[weaponType].SetActive(!activeWeapons[weaponType].activeSelf);
            anim.SetInteger("WeaponType_int", weaponType);
            break;
        case 4:
            weaponDeactive();
            activeWeapons[weaponType].SetActive(!activeWeapons[weaponType].activeSelf);
            anim.SetInteger("WeaponType_int", weaponType);
            break;
        case 5:
            weaponDeactive();
            activeWeapons[weaponType].SetActive(!activeWeapons[weaponType].activeSelf);
            anim.SetInteger("WeaponType_int", weaponType);
            break;
        case 6:
            weaponDeactive();
            activeWeapons[weaponType].SetActive(!activeWeapons[weaponType].activeSelf);
            anim.SetInteger("WeaponType_int", weaponType);
            break;
        case 7:
            weaponDeactive();
            activeWeapons[weaponType].SetActive(!activeWeapons[weaponType].activeSelf);
            anim.SetInteger("WeaponType_int", weaponType);
            break;
        case 8:
            weaponDeactive();
            activeWeapons[weaponType].SetActive(!activeWeapons[weaponType].activeSelf);
            anim.SetInteger("WeaponType_int", weaponType);
            break;
        case 9:
            weaponDeactive();
            activeWeapons[weaponType].SetActive(!activeWeapons[weaponType].activeSelf);
            anim.SetInteger("WeaponType_int", weaponType);
            break;
        case 10:
            weaponDeactive();
            activeWeapons[weaponType].SetActive(!activeWeapons[weaponType].activeSelf);
            anim.SetInteger("WeaponType_int", weaponType);
            break;
          
        }
    }
  
    public void weaponDeactive(){
      
        foreach (GameObject go in activeWeapons)
            go.SetActive (false);
    }
}

There’s no reason for the switch/case since they all do exactly the same thing. You can get rid of all that and just use the three lines of code starting with “weaponDeactive()”. As for using a string, no, that’s not something you ever want to do if you can avoid it. If the intention is to use something more readable than a number, use an enum instead.

–Eric

Use them

1 Like

Thankyou Eric

I have read many of your great answers here on the forum and you have helped me tremendously along the way so thank you greatly.

Somehow i think i try to over complicate things due to not understanding everything as well as i should.
Regarding the String to Int, the intention was readability so thankyou again for the enum reference.

Thanks LordOfDuck for the link :slight_smile:

I seem to be getting an error of Type to Int. Now im presuming thats due to the change i made of int weaponType to enum weaponType.

my question is now how do i now call my weaponSelector(int weaponType) via Start();

public class AIAnimation : MonoBehaviour
{
    private aiMovement aiM;
    private Animator anim;
    public enum weaponType {NoWeapon,Pistol,AssultRifle01,AssultRifle02,Shotgun,SniperRifle,Rifle,SubMachineGun,RPG,MiniGun,Grenades};
    public GameObject[] activeWeapons;


    void Start () {
        aiM = this.transform.parent.GetComponent<aiMovement>();
        anim = GetComponent<Animator>();
        weaponSelector (weaponType); //(Error) Microsoft says the values of enum are set to int
    }

    void FixedUpdate(){
        Animotion();
    }

    void weaponSelector(int weaponType)
    {
        weaponDeactive();
        activeWeapons[weaponType].SetActive(!activeWeapons[weaponType].activeSelf);
        anim.SetInteger("WeaponType_int", weaponType);
    }

    public void weaponDeactive(){
   
        foreach (GameObject go in activeWeapons)
            go.SetActive (false);
    }
}

You can cast it to an int, since your function specifically takes an int rather than an enum.

–Eric

Or you could move your WeaponType outside of the class, and change the method to take a WeaponType as a parameter.

You’re still going to have to cast it eventually.

–Eric

1 Like

Eric, I might end up just going back to public int weaponType since im having the hardest time figuring out how to cast the enum as hole rather than

int weapon = (int)weaponType.Pistol
int weapon = (int)weaponType.AssultRifle01

Am i even going about this in the right manner? Is it not possible to cast all the variables of the Enum to an Int, since i only need one variable of the Enum at a time that will be selected via the inspector.

I wish this stuff came easier to me but i am a very visual person. Could i get possibly get an example of what you mentioned above so i can give it a try and forgive me for the spam of questions.

Thankyou Kindly.

Just do the (int) thing when you want to use the enum as an int.

–Eric