When creating an enemy information display, I need to get all components on an entity. Now the [java]ed.getEntity(EntityId arg0, Class … arg1)[/java] returns only the components I ask for right?
Could I go with a component interface, to get all components implementing a certain interface (say ‘IEnemyComponent’) ?
I’d like to follow best practice, so I’m asking preactively instead of discovering later on that I’ve done it all wrong
Systems should ask for the components that they will deal with. Asking for all components is a sign of a major design problem (there could be hundreds of components that are irrelevant to the system asking). So you may need to explain more.
If a system knows how to deal with health, speed, damage (kind of unlikely) then it should ask for those.
To me “enemy” seems like a completely human concept. It would have to be explained why a system cares what “enemy” is… and then yes, it probably deserves a component.
…still, I’m looking very skeptically at the whole idea.
Systems should ask for the components that they will deal with
I agree. All my systems to handle game logic only uses 1 or 2 components.
The ‘system’ that is troubling me here is not a system to handle game logic per say. I simply want to display all available information about a unit, to the player. If he selects his own units, all information is displayed, if he selects an enemy, only a subset is displayed.
Also, I know I have to keep track of which components to display to the player, somehow, because not all components are ment to be known by the player. But the issue is still valid.
@asser.fahrenholz said:
Also, I know I have to keep track of which components to display to the player, somehow, because not all components are ment to be known by the player. But the issue is still valid.
Hmmm… you just described why it’s not valid and then say it’s valid.
You want to display certain components to the user… so you display those components. Better to display only the components you wish to display and not the 100 odd random components you might add later.
So I don’t see how the issue is still valid since there are no valid use cases presented to “get all components for an entity”.
@pspeed said:
Hmmm... you just described why it's not valid and then say it's valid.
You want to display certain components to the user… so you display those components. Better to display only the components you wish to display and not the 100 odd random components you might add later.
So I don’t see how the issue is still valid since there are no valid use cases presented to “get all components for an entity”.
Okay, so as I see it, I have to ‘hard-code’ a list of components that may or may not be on the entity I select.
That is, if I choose not to go with Empire Phoenix’s suggestion.
Just to clarify https://code.google.com/p/reflections/
allows you exactly what you requested in the beginning,
Getting all classes wich inherit from an interface.
[java]
final Reflections reflections = new Reflections(“de.visiongamestudios”);
final Set processedComponents = reflections.getSubTypesOf(IVisiblecomponent.class);
//all implementing
final Set<Class<? extends ProcessedInterface>> casted = processedComponents;
for (final Class<? extends ProcessedInterface> subType : casted) {
if (subType.isInterface()) {
//traverse interfaces that extend the interface
this.traverseInterface(subType, reflections); //-> basically do the same ask reflections for the subtypes of this new subInterface, and repeat recursivly
}
//add to visible list
}
[/java]
@Empire Phoenix said:
Just to clarify
https://code.google.com/p/reflections/
allows you exactly what you requested in the beginning,
Getting all classes wich inherit from an interface.
[java]
final Reflections reflections = new Reflections(“de.visiongamestudios”);
final Set processedComponents = reflections.getSubTypesOf(IVisiblecomponent.class);
//all implementing
final Set<Class<? extends ProcessedInterface>> casted = processedComponents;
for (final Class<? extends ProcessedInterface> subType : casted) {
if (subType.isInterface()) {
//traverse interfaces that extend the interface
this.traverseInterface(subType, reflections); //-> basically do the same ask reflections for the subtypes of this new subInterface, and repeat recursivly
}
//add to visible list
}
[/java]
Just to clarify, if you use extension and interfaces for your components you do something severely wrong.
Did you read the thread even? As then you would understand where this comes from.
When building dynamically a gui for a subset of components
one could either hardcode them
or tag them with an interface and let code generate that list.
There are a few other similar use cases btw,
eg flagging all components that should be network synchronized in a networked ES, flag all components that need to be persistable (eg table creation,schema validation ect in background)