Help with executing a generic weapon

I’m making a framework for multiple, swappable weapontypes, i want the input manager to execute the perform function of a weapon. the weapons are divided into their own classes inheriting class Weapon which inherits monobehaviour

So, what I’m trying to do:
From player input manager, OnInput executes a function in playercontroller monobehaviour, which then runs the “perform” function for an arbitrary weapon class which is a monobehaviour

The bad way i’m currently doing it, let’s say the player has a sword:
Input happens, in inputmanager →

private void WeaponMainhandPerform(InputAction.CallbackContext context) {
              GetComponent<IHPlayerController>().mainhandWeapon.GetWeapon();
}

playercontroller runs GetWeapon function from assigned scriptableObject Sword(WeaponConfig), the WeaponConfig SO has an enum with all the weapontypes (Sword,Knife,Etc). Sword(WeaponConfig) has been assigned the value for Sword. So to run the correct weapon perform, GetWeapon uses this ugly switch:

public void GetWeapon() {
            switch (weaponType) {
                case WeaponTypes.Sword:
                    prefab.GetComponent<Sword>().PerformWeapon();
                    break;
                case WeaponTypes.Knife:
                    prefab.GetComponent<Knife>().PerformWeapon();
                    break;
            }
}

So currently it flows like this PlayerInput : Monobehaviour → PlayerController : Monobehaviour → WeaponSO : ScriptableObject → (Prefab) Sword : Weapon : Monobehaviour

What’s a better way to do this? Both the sword and knife class inherits from the class Weapon which in turn inherits from monobehaviour. I’m not a very experienced programmer, so not sure what’s best to use in this case. Virtual override? Interface? Polymorphism? (haven’t used these before just that maybe they could be useful here) Any suggestions appreciated. If i could just replace the switch with something that gets the correct weapon class and function it would be perfect, or if i should just refactor in some way.

Perhaps interfaces?

Using Interfaces in Unity3D:

https://discussions.unity.com/t/797745/2

https://discussions.unity.com/t/807699/2

Check Youtube for other tutorials about interfaces and working in Unity3D. It’s a pretty powerful combination.

I actually did look into interfaces but was put off when i read this result when googling about them
http://hightalestudios.com/2017/03/friends-dont-let-friends-use-interfaces-on-monobehaviour-objects/
But i’m using a scriptableObject as middleman not monobehaviour, i don’t know i’m fresh and impressionable lol. I’ll look into trying with an interface.

From my reading, it sounds like he used interfaces for something that didn’t bring him any value and then wrote a long click-baity “Friends don’t let friends…” article about it. To his credit he updated it with current interface getter abilities.

You know what they say about the “internet of lies.” :slight_smile:

Try his way, try interfaces, try lots of things. It’s really the only reasonable way to arrive at an informed decision.

An interface or inherited base class will reduce the switch to one line, or remove the GetWeapon function completely.

You could also store the perform functions in a dictionary and call it like so:

Dictionary<WeaponTypes, Action> WeaponFunctions;

WeaponFunctions[weaponType]();