Part 3: Unity ECS — operating on Entities

Tomasz Piowczyk
2 min readJun 9, 2018

--

How to do something with these entities?

Part 1: Unity ECS — briefly about ecs
Part 2: Unity ECS — project design
Part 3: Unity ECS — operations on Entities
Part 4: Unity ECS — ECS and Jobs

The Basics

EntityArchetype — what is it and why you need this.

An EntityArchetype is a unique array of ComponentType. EntityManager uses EntityArchetypes to group all Entities using the same ComponentTypes in chunks. (docs: THERE)

I’ll not cover the chunks idea since this series of article is mainly targeted for beginners.

What will we need in the beginning — initialization

Just before we create entities, we need EntityArchetype — let’s create them.

Spawn some entities — finally..

But nothing happen — Let’s take a look at systems

Ah.. now it’s much better, something happen. Let’s hurt our entities, so they can reach our Sky.

Ok, but they are still alive..

We’ve got PostUpdateCommands, why not just use EntityManager since it manages entities? Do you remember what did say in our Part 1?

But actually, you don’t want to add/remove components via EntityManager. You’d rather do it after update to not break the group (you’ll get an error about accessing deallocated nativearray), so you want to use PostUpdateCommands (EntityCommandBuffer).

What is EntityCommandBuffer and why we need this?

It’s pretty good explained in documentation THERE

All structural changes have hard sync points. CreateEntity, Instantiate, Destroy, AddComponent, RemoveComponent, SetSharedComponentData all have a hard sync point. Meaning all jobs scheduled through JobComponentSystem will be completed before creating the Entity, for example. This happens automatically. So for instance: calling EntityManager.CreateEntity in the middle of the frame might result in a large stall waiting for all previously scheduled jobs in the World to complete. See EntityCommandBuffer for more on avoiding sync points when creating Entities during gameplay.

There is mentioned JobComponentSystem that I haven’t cover yet, but don’t worry, it’ll be in next part! EntityManager invalidates existing, injected arrays and component groups, so it’ll mess up our Updates.
Unlike in JobComponentSystems, PostUpdateCommand(EntityCommandBuffer) in ComponentSystem is set and available automatically.
This EntityCommandBuffer allows us to queue up changes to be performed so that they can take effect later in the main thread.
Now you’re interested in using jobs in ECS for sure. I’ll write next part about that.

Is it all? No, are you tired? There is just one more part:
Part 4: Unity ECS - ECS and Jobs

Give me some feedback in comment section, don’t worry I won’t hate you if you show me some “anomaly” in my article. If you enjoy my article — like it and follow me. It’ll motivate me to write more articles. See you.

Originally published at https://connect.unity.com/p/part-3-unity-ecs-operating-on-entities

--

--

Tomasz Piowczyk

Game enthusiasts. Open to improvements. Share passion. Programming lover.