- Home /
ECS architecture & the state machine
I've recently been learning ECS and getting my head around DoD methodology, one question I have is around a State Machine replacement.
Imagine I have a realtime hack 'n' slash with a series of AI-driven monsters all running around independently. Now imagine the player opens up an inventory - with the desired effect of pausing the monsters behaviour. How would one architect this in ECS?
Do we give the monsters behaviour component a "paused" attribute and iterate through all of the monsters in the game setting it? Or...?
Answer by RendrWyre · Jan 09, 2020 at 10:38 PM
@GeorgeDoodlebug You might want to do your own research because I might have misunderstood some of the ECS stuff..... but, AFAIK;
Archetypes are used to group entities that share the same components. Unity does this automatically (and at runtime) by grouping entities with the same components into an archetype. So, for example:
Fish Archetype
- Renderer Component
- Mesh Component
- Fish Health Component
- Pause Component
Vampire Archetype
- Renderer Component
- Mesh Component
- Vampire Health Component
- Pause Component
Although they are different archetypes they share similar components - namely a "pause" component. Now, this component should only hold the pure data needed to pause the monster. For example, an isPaused boolean or something. You might have a Pause Monster System that actually handles the pausing.
Now, onto the core of ECS... iteration. The "Pause Monster System" would iterate through each entity that has a "Pause Component" and write to that component (ie - set isPaused to true). It's basically like saying "find all of the entities with a Pause Component and then do something".
Unity has been publishing a lot of ECS/DOTS stuff from copenhagen 2019. Their Pong video (https://www.youtube.com/watch?v=a9AUXNFBWt4) is a good place to start if you're still struggling with the theory/how it works.
just as further clarification
Entities are like gameobjects. They represent "things" in your scene.
Components hold pure data.
Systems perform some kind of action on component data. For example, writing a new movement speed to an entity's movement component. In this case, a Movement System would write to a Movement Component of a single or multiple Entities.
Answer by ben-rasooli · Aug 12, 2021 at 01:26 AM
The other two answers are right. If you wanna do it in a more structured and manageable way, you can read my recent article that is designed for your exact scenario. In short, you put your systems in a ComponentSystemGroup and then control their execution there.
Answer by CaseyHofland · Jul 21, 2020 at 11:01 AM
One approach is to disable every system that is doing runtime stuff (or only movement stuff if you're lazy like me)
private StepPhysicsWorld stepPhysicsWorld;
private IncreaseVelocityOverTimeSystem increaseVelocityOverTimeSystem;
protected override void OnCreate()
{
stepPhysicsWorld = World.GetOrCreateSystem<StepPhysicsWorld>();
increaseVelocityOverTimeSystem = World.GetOrCreateSystem<IncreaseVelocityOverTimeSystem>();
}
protected override void OnUpdate() // From SystemBase
{
if(Input.GetKeyDown(KeyCode.P))
{
stepPhysicsWorld.Enabled = !stepPhysicsWorld.Enabled;
increaseVelocityOverTimeSystem.Enabled = !increaseVelocityOverTimeSystem.Enabled;
}
}
But I concur, it is not ideal and if there is a better way I'd gladly hear.
Answer by andrew-lukasik · Jul 21, 2020 at 01:04 PM
Paused
is a game UI state and not a monster state - as opposed to freezed
, petrified
or unconscious
). So it doesn't belong to IComponentData.
What you can do instead is to make relevant systems stop scheduling jobs while game is paused. This would probably include systems such as: monster locomotion, monster animation, decision making, decision execution (attacking, working etc) and also those changing values over time (health, hunger etc)
public class MyPausableSystem : SystemBase {
protected override void OnUpdate ()
{
if( gameUiStateSource.isPaused ) return;
job1.Schedule();
job2.Schedule();
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612221318im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Using interfaces in C# 4 Answers
Component based architecture Unit, AI, Health, Attack 0 Answers
Unity UI Capability 0 Answers
Physics architecture. When first step after collision happens? 0 Answers
Skill Behaviours Implementation 0 Answers