Post by Admin on Jul 16, 2016 4:39:49 GMT
Maxim Zaks @mzaks Nov 29 2015 19:47
@derkork there is no built-in support. The ideal case would be that you have some generic MonoBehaviour which would know what kind of entity it should create, without keeping references to an entity.
For example in project I am working on, we have a HUDButtonBehaviour, we add this behaviour to everything on the hud that should be able to work as a button. Now we also specify an enum which reflects all Button Actions that you can have.
When we add HUDButtonBehaviour to a game object we specify which ButtonAction it should trigger.
Internally in HUDButtonBehaviour we create an entity which carries ButtonActionComponent, so we don't need any reference from GameObject to an entity. We actually have a sender property on ButtonActionComponent in case we want to know which game object was touched. However this is already not ideal. Best case scenario you are able to decouple logical state completely from its visual representation.
One important note: if you want to have a reference from GameObject to an entity you have to retain this entity and than release it when the game object will be destroyed.
github.com/sschmid/Entitas-CSharp/blob/develop/Entitas/Entitas/Entity.cs#L269
Retain and Release methods are introduced because we have an entity object pool for performance reasons.
@derkork there is no built-in support. The ideal case would be that you have some generic MonoBehaviour which would know what kind of entity it should create, without keeping references to an entity.
For example in project I am working on, we have a HUDButtonBehaviour, we add this behaviour to everything on the hud that should be able to work as a button. Now we also specify an enum which reflects all Button Actions that you can have.
When we add HUDButtonBehaviour to a game object we specify which ButtonAction it should trigger.
Internally in HUDButtonBehaviour we create an entity which carries ButtonActionComponent, so we don't need any reference from GameObject to an entity. We actually have a sender property on ButtonActionComponent in case we want to know which game object was touched. However this is already not ideal. Best case scenario you are able to decouple logical state completely from its visual representation.
One important note: if you want to have a reference from GameObject to an entity you have to retain this entity and than release it when the game object will be destroyed.
github.com/sschmid/Entitas-CSharp/blob/develop/Entitas/Entitas/Entity.cs#L269
Retain and Release methods are introduced because we have an entity object pool for performance reasons.