Should getComponents<T>/etc also retrieve subclasses?
See original GitHub issueI need this behaviour as I’m adding an entity system that processes all components B, C and D that subclass component A, which provides a standard interface for the system. It would be great if I could just call getComponents() and get all of those matching components, but the current behaviour gets the exact class via the “is” keyword.
I’m conscious of the fact that this could easily break existing games that assume it doesn’t work this way. If that’s a concern we could add an optional “includeSubclasses” flag that switches the behaviour on, with off being the default.
At the moment I’ve hacked in a getComponentsAll() method such that my system can do this itself, but I don’t see why we couldn’t bake this behaviour into Entity/ComponentList. Is there any reason I shouldn’t proceed with a PR for this?
The check used would probably be as follows:
component.GetType().IsSubclassOf( typeof( MyComponent ) ) || component.GetType() == typeof( MyComponent)
Issue Analytics
- State:
- Created 6 years ago
- Comments:7 (6 by maintainers)
Matcher definitely doesn’t work with subclasses, but that’s a slightly different usecase to the one I wanted to solve, which seems to be solved already as I must’ve done something dumb like put the wrong class name in the <>.
@0xFireball Yep, it is. Should’ve closed this issue after my last comment, my bad. 😃