When is it appropriate to use inheritance?

Hey guys, I want to see if I understand things correctly.

Imagine you are making a survival game. You have many actors in the level that may be interacted with by the player.

To query what player is interacting with we have number of options:

  • check its object name
  • check its class or parent class
  • check for a tag

As far as I can tell, the end result is the same? We have just used some sort of info about the actor to identify it.

A case for inheritance may be that, perhaps all actors of a certain type can share some functions? E.G. when you pick up an apple or a banana, both might call an “Create EAT FOOD dialogue option” function.

Otherwise, are all these different methods to identify actors the same?

Would a smart method for actor interaction be like this?

player overlaps another actor, then send interface message to that actor. If the actor is implementing interface, it will proceed accordingly.

Actually, upon further thought, I think a better approach might be that all these things are the same class, and each instance just gets its specific data from a data table.

So, Food Item might be a class. Each instance selects a data table row to figure out what its graphic representation is, its calorie content, eating sound, etc.

THEN, if some specific food item type requires special behavior, I can just have a flag for that in the data table, and that could either activate some additional function or add a component?

p.s. I am using a specific example in hopes to make question easier to read and understand, but it’s hypothetical example. I am asking in general programming terms what might be best approach, just because to me it seems like most of the methods cover the same problems.

I think your problem maybe coming from trying to figure out how the player should interact with each type of object. The point of inheritance and programming compartmentalization, is that the object decides how to react with the player, not the other way around.