Skip to main content

Systems

In Easel, a system is any function that imbues an entity with ongoing functionality. It does this by adding behaviors, components and/or state to the entity. Systems are not a feature of the programming language, they are simply a reusable pattern used by Easel programmers to organize their code.

A common example of a system is a HealthSystem, which tracks the health of an entity and destroys it when it reaches 0. This is a simple example of a HealthSystem:

pub prop unit.Health

pub fn unit.HealthSystem() {
Health = 100

with Health {
if Health <= 0 {
Expire
break
}
}
}

Because the with block adds a behavior to the entity, the code inside the with block will continue to run for the entire lifetime of the entity. That is what makes this function a system.

What makes a system different from a normal function?

A system is different from a regular function because it adds ongoing behaviors that continue to run the entity after the function has finished executing. Most of the time, a system will last the entire lifetime of the entity, but it doesn't have to.

For example, a ArriveFromBottomSystem might make your spaceship arrive in a big flash of light from the bottom of the screen, but once it is done arriving, it should stop running and delete itself.

Systems and entities

A system can add any combination of behaviors, components and state to an entity.

For example, a MagnetSystem might be used to make powerups automatically move towards any nearby players. It might add:

  • A PolygonCollider component to detect nearby players.
  • A Attractor prop to store the current player it is moving towards. There may be two player nearby and it needs to choose one of them.
  • A on BeforePhysics behavior which moves the powerup closer to the Attractor every tick.

Care must be taken to make sure two systems cannot overwrite each other's state or behaviors. For example, the MagnetSystem should give the PolygonCollider an Id, i.e. PolygonCollider<magnet>, otherwise it might overwrite the PolygonCollider added by another system.

Deleting systems

In some cases, you want to be able to delete a system after it has been added to an entity. To do this, implement a delete fn that removes everything that the system originally added to the entity. For example:

pub prop unit.Health

pub fn unit.HealthSystem() {
Health = 100

behavior<dieWhenZero> with Health {
if Health <= 0 {
Expire
break
}
}
}

pub delete fn unit.HealthSystem() {
delete behavior<dieWhenZero>
}

In most cases, you don't need to do this. Most systems last the full lifetime of the entity. If you want them to last a shorter time, you can Subspawn a child entity, and expire the child entity earlier to end the system.