C
C#5mo ago
IceBlocker

Sell me on interfaces (and components if unity)

Hello! Sooo, for games I suppose the most common interface to think about would be something like health or damageable, or smth alike. However suppose that every object that could be damaged (let's say characters, obstacles, towers??? buildings?? idk), already inherit a base class "entityBase". Why not just add the functions those classes would have anyway because of interfaces to entityBase?
15 Replies
sibber
sibber5mo ago
in this case i dont see any reason not to i dont know about game development, but one example of interfaces is logging where you have a generic abstraction for the logger, and then anyone can define their own implementation but from my experience its hard to get conviced about things like these until you see them in real world applications
Jimmacle
Jimmacle5mo ago
using inheritance for different entity types in games gets messy quickly which is why components are more common there will inevitably be a point where you have an entity that logically inherits from 2 or more other entities except that's impossible to actually do and now you're stuck
IceBlocker
IceBlockerOP5mo ago
yeah, might need to see good examples
becquerel
becquerel5mo ago
sometimes you want to write a function that only operates on IDamageable types
IceBlocker
IceBlockerOP5mo ago
at that point couldn't you make it part of the base class?
becquerel
becquerel5mo ago
and doesn't want to concern itself with all the other junk in the EntityBase class functions should only have access to the minimal amount of stuff they need to do their job 'throw everything in one big base class that does everything' is an antipattern, because it leads to unclear separation of concerns it's the quote about OOP where you want a banana, but instead you get the monkey holding the banana and the whole jungle as well
IceBlocker
IceBlockerOP5mo ago
that makes sense, yeah, I can see why
Jimmacle
Jimmacle5mo ago
composable objects are generally more flexible than trying to build a class hierarchy because you can pick and choose which pieces you need instead of having to take on a whole hierarchy of behaviors
IceBlocker
IceBlockerOP5mo ago
You mentioned an example about an interface IDamageable, what if I have two towers, one damageable and one that isn't, can't see how I would do that with interfaces without making a different tower that doesnt inherit from it
Jimmacle
Jimmacle5mo ago
exactly
IceBlocker
IceBlockerOP5mo ago
I could see simply making two Tower objects and giving a Damageable component to one of them Still, when iterating for stuff to damage, Damageable would only care about the actual damage doing stuff, not targetting stuff So that would also be a component? a Targeteable component?
Jimmacle
Jimmacle5mo ago
yeah, you'd have different components for different independent behavors you'd want an entity to have then you can mix and match however you want
becquerel
becquerel5mo ago
and typically you can declare an interface as being a superset of other interfaces, which makes life a lot more convenient so you could have an IReadyForCombat interface which itself derives from the IDamageable and ITargetable and IAttacker interfaces making it shorthand for listing out all three of the individual ones
IceBlocker
IceBlockerOP5mo ago
well, I must say thanks everyone for the help!
Cattywampus
Cattywampus5mo ago
and if it's Unity (as your title says), you can pass things by references rather than copy by value in Unity and have them serialized via ISerializeReference :blobthumbsup:
Want results from more Discord servers?
Add your server