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.