- Home /
Call function based off enum state?
So im working on a game that has the player use a variety of items, all tied to the same 3 keys (i.e. if itemKey1 has the greatsword state, then pressing itemKey1 will use the greatsword). at the moment i have an input handler class that stores the state and checks for input, it then calls a function in my "UseItem" class, and in that, it checks, in a series of if statements, what item is selected, then calls a different function depending.
public enum ItemType
{
rapier, greatsword, bow
}
public class InputHandler : MonoBehaviour
{
[Header("INPUTS")]
public static KeyCode itemKey1 = KeyCode.J;
public static KeyCode itemKey2 = KeyCode.K;
public static KeyCode itemKey3 = KeyCode.L;
public static KeyCode runKey = KeyCode.LeftShift;
[Header("ENUMS")]
public ItemType item1;
public ItemType item2;
public ItemType item3;
[Header("SCRIPT REFERENCES")]
UseItem use;
private void Start()
{
use = FindObjectOfType<UseItem>();
}
private void Update()
{
InputItem();
}
void InputItem()
{
if (Input.GetKeyDown(itemKey1))
{
use.InputUse(item1);
}
if (Input.GetKeyDown(itemKey2))
{
use.InputUse(item2);
}
if (Input.GetKeyDown(itemKey3))
{
use.InputUse(item3);
}
}
}
public class UseItem : MonoBehaviour
{
RapierAttack rapier;
GreatswordAttack greatsword;
PlayerBow bow;
private void Start()
{
rapier = GetComponent<RapierAttack>();
greatsword = GetComponent<GreatswordAttack>();
bow = GetComponent<PlayerBow>();
}
public void InputUse(ItemType item)
{
if(item == ItemType.rapier)
{
if(rapier.canUse)
rapier.Use();
}
else if(item == ItemType.greatsword)
{
if (greatsword.canUse)
greatsword.Use();
}else if(item == ItemType.bow)
{
bow.Use();
}
}
}
This all works, but my concern is that it isnt very expandable. i would like to call one function that is overidden by the specific Use() function that is applicable. Ive heard about abstract classes, but i dont really understand them. would scriptable objects be of any use? Sorry for the really long question, any help is welcome!
Answer by Captain_Pineapple · Jan 01 at 09:35 PM
Hello there,
this use case is the exact reason what Object Oriented Programming is for. You idea that your current approach with cause issues down the road is exactly correct. What you want to do here is the following:
Change your current UseItem
class into this format:
public class UsableItem : MonoBehaviour
{
public virtual void UseItem()
{
throw new System.NotImplementedException("This function must not be called. It must be overridden.");
}
}
//example for your Bow:
public class Bow : UsableItem
{
public override void UseItem()
{
//do whatever you need to do to use your bow
}
}
//example for a Sword:
public class Sword : UsableItem
{
public override void UseItem()
{
//do whatever you need to do to use your sword
}
}
Both the Bow
and Sword
class can now be stored in a UsableItem
reference. In addition they each call their respective UseItem
function when being called upon. Based on this you could just store a direct reference to an usable Item in your InputControl script:
public class InputControl : MonoBehaviour
{
[Header("INPUTS")]
public static KeyCode[] slotKeys = {
KeyCode.J,
KeyCode.K,
KeyCode.L
};
public UsableItem[] ItemSlots = new UsableItem[slotKeys.Length];
public void Update()
{
for(int i=0;i<slotKeys.Length;i++)
{
if(Input.GetKeyDown(slotKeys[i]))
{
if (ItemSlots[i] != null)
ItemSlots[i].UseItem();
}
}
}
}
This way you have an easy way to extend your slots by simply adding another key. You can additionally add more weapon behaviours in a really easy way.
Let me know if that helped or if something was unclear.
Thank you for the response!
where am I assigning the right ItemType to each key? How does the script know what item is active when i press KeyCode.J for instance.
I applied your code in my scripts and the UseItem() function is being called properly, but nothing happens from there
nvm.... I just forgot to assign the scripts in the editor :P
one more question, the way i previously had it set up was in such a way that i could assign items to a key using the enum state.
i could change what key does what in real time, which is an important feature. Is there any way i could do this with this new system?
well yes that is kind of not really possible with this version.
You can simply assign a different item in a slot. Even at runtime. You will always call the UseItem
function of this specific item instance.
If you have a specific need for this current feature then you can keep your enum, add a state None in the Enum and check if any Item(1/2/3) is != None. If it is then call the use function of this item. (Not really sure if that is such a good idea though...) With this i'd see no good way to make your code easy to maintain as you'd always have to do some more things to add a new item.
Your answer
Follow this Question
Related Questions
enum in generic subclass shows as integer 1 Answer
Need assistance with SubType in custom class (UnityScript or C#) 2 Answers
Item type script using classes isn't working 1 Answer
manipulate fields of class 1 Answer
Finite State Machine 1 Answer