What I’m gonna talk about is of creating an advanced OOP mechanism, so I’m looking for the most efficient answer, even if it’s hard.
I’m creating a minecraft-like game, and I got most of the mechanics figured out, however this particular one is proving to be a bit challenging. The one that I’m stuck with is what I like to call the Item Behaviour Handler. Bluntly speaking, minecraft has like a ton of items, and all of them have a unique behaviour, based on the method of input, whether it be mouse input or keyboard. Now, the easiest way to solve this is to chuck in a bunch of if/else statements, but that’s unreliable and reduces efficiency.
So my solution:
Basically, I have a Base class named 'Item Behaviour`, and it will have a function that fires everytime the user left-clicks, in this case it raycasts some distance infront of the player and starts breaking the block, and the reason its in the parent class is to avoid rewriting it on every child class, since you can break blocks with any item duh.
Then using inheritance, I can create sub-classes which have a custom type, such as fireball or melee or block or well… you get it.
But then how would I call the certain child class based on the item held?
If you could give me a base idea on how to go about it I would appreciate it.