Post by Admin on Jul 16, 2016 3:01:02 GMT
Andreas Wilcox @svdvorak Aug 18 2015 18:41
@bcatcho That looks like a very nice solution for spawning from code, I might borrow it for my own use. Though my problem was initially how to spawn Entitas entities based on scene gameobjects, I solved it the following way. I wouldn't say it's a great solution, just something I tried out and it has worked well so far.
What I want is for there to be an entity created with specific components for a game object so the following class is attached to the game object in the scene. I use Vexe to add all the different components in the editor (since Unity doesn't support interfaces in the editor).
To then create the actual entities I have a start system that goes through the scene, finds all ComponentContainers and creates entities with the specified components.
Note that I've modified the code generator to add the ComponentToId-function in ComponentIds so that I can actually add existing components to an entity (you can find the code here). As mentioned before by the others, it's not very efficient to add components directly and searching through the scene is also expensive but seeing as it is run once at startup and I don't have that many ComponentContainers I find that it's ok. I might change my mind as the game expands, I'm open for suggestions though. Note that after start the components on the game objects are "dead", they won't actually change the entities. It's fine though I'm thinking of finding a way to jump from game object to entity in the debug system.
I'll admit though that it's not especially clean since you need to add all the components needed, even ones you probably don't even want to configure. For example, I need to add position and rotation components even though I don't care what values they have. Having a configuration behavior like @bcatcho gives a cleaner interface though it requires one for each type. Hopes this helps anyone =)
@bcatcho That looks like a very nice solution for spawning from code, I might borrow it for my own use. Though my problem was initially how to spawn Entitas entities based on scene gameobjects, I solved it the following way. I wouldn't say it's a great solution, just something I tried out and it has worked well so far.
What I want is for there to be an entity created with specific components for a game object so the following class is attached to the game object in the scene. I use Vexe to add all the different components in the editor (since Unity doesn't support interfaces in the editor).
public class ComponentContainer : MonoBehaviour
{
public List<IComponent> Components;
}
To then create the actual entities I have a start system that goes through the scene, finds all ComponentContainers and creates entities with the specified components.
public class LinkViewsStartSystem : IStartSystem, ISetPool
{
private Pool _pool;
public void SetPool(Pool pool)
{
_pool = pool;
}
public void Start()
{
var componentContainers = Object.FindObjectsOfType<ComponentContainer>();
foreach (var componentContainer in componentContainers)
{
CreateEntityFor(componentContainer);
}
}
private void CreateEntityFor(ComponentContainer componentContainer)
{
var entity = _pool.CreateEntity();
foreach (var component in componentContainer.Components)
{
entity.AddComponent(ComponentIds.ComponentToId(component), component);
}
}
}
Note that I've modified the code generator to add the ComponentToId-function in ComponentIds so that I can actually add existing components to an entity (you can find the code here). As mentioned before by the others, it's not very efficient to add components directly and searching through the scene is also expensive but seeing as it is run once at startup and I don't have that many ComponentContainers I find that it's ok. I might change my mind as the game expands, I'm open for suggestions though. Note that after start the components on the game objects are "dead", they won't actually change the entities. It's fine though I'm thinking of finding a way to jump from game object to entity in the debug system.
I'll admit though that it's not especially clean since you need to add all the components needed, even ones you probably don't even want to configure. For example, I need to add position and rotation components even though I don't care what values they have. Having a configuration behavior like @bcatcho gives a cleaner interface though it requires one for each type. Hopes this helps anyone =)