Skip to main content

Roles

What variable name should you use for your entities?

We recommend you name entities after the main role they play in the game, rather than naming them after their specific type.

Let's say you have a game where you keep barn animals - cows, pigs, chickens, etc. When you create the functions which define the behavior of these animals, you will have to decide what variable name to use. You could do this:

pub fn cow.Cow(...) { ... }
pub fn pig.Pig(...) { ... }
pub fn chicken.Chicken(...) { ... }

But this makes it difficult to share code between the different animals. We recommend you name them based on their primary role in the game. For example:

pub fn animal.Cow(...) { ... }
pub fn animal.Pig(...) { ... }
pub fn animal.Chicken(...) { ... }

Why naming matters

Because we have defined all animals as animal, now anything that needs an animal can find it automatically from Context. This makes it easier to reuse code. Here are some examples of some functions signatures and other declarations which could now accept any animal from context:

pub fn animal.HungerSystem() { ... }
pub fn this.WalkToTarget([animal]) { ... }
pub fn CalculateHayRequired([animal]) { ... }

pub prop animal.IsSleeping = false
pub field animal.HungerLevel = 100
pub signal animal.Hurt = 100

Roles in your game

You should end up with only a short list of roles that your entities play, even though each role has many variants. For example, you might have the following roles:

  • animal: Cow, Pig, Chicken, etc.
  • food: Hay, Corn, Slop, etc.
  • dialog: Game over dialog, upgrade dialog, shop dialog, etc.
  • scene: The Farmhouse, The Barn, The Field, etc.

You will also notice there are a few roles that Easel's built-in functions use. It is best to stick with them and use them for your own entities:

  • body: Any entity that has a physical body in the game, such as an asteroid or ship.
  • owner: Player 1, Player 2, Player 3, etc.
  • camera: Main camera, minimap camera, cutscene camera, etc.

Multiple roles, one entity

Some entities will have multiple roles. You can use a use statement to cast an entity into additional roles.

Let's say you have a Goldfish which is both an animal and food. You might declare it like this:

pub fn animal.Goldfish(...) {
use food=animal

// ...

HungerSystem // takes `animal` from context
FoodAmount = 10 // takes `food` from context
}

The use food=animal statement allows us to use the Goldfish as both an animal and a food automatically from context.

Another common example of this is the body role, which is used for any entity that has a physical body in the game. A Cow is both an animal and has a body, and so we add a use body=animal statement to cast it into the body role:

pub fn animal.Cow(...) {
use body=animal

Body(pos=)
ImageSprite(...) // takes `body` from context
HungerSystem // takes `animal` from context
}

This is the primary reason for a use statement in Easel - to cast an entity into multiple roles within a context.