This
The this
variable is an implicit context variable that refers to the current entity.
this
refers to the object of the current function,
except in a few cases when a function has changed the value of this
for its subblock.
Conventionally, this
defines the lifespan of everything that is being created in the current block.
pub fn ship.Spaceship() { // `this` is `ship`
// Same as `ship.PolygonSprite(...)` because `this` is `ship` in this block
PolygonSprite(shape=Circle(5)) // will live as long as the `ship`
Spawn projectile { // `this` is `projectile` in this block
// Same as `projectile.PolygonSprite(...)`
PolygonSprite(shape=Circle(5)) // will live as long as the `projectile`
Subspawn effect { // `this` is `effect` in this block
// Same as `effect.PolygonSprite(...)`
PolygonSprite(shape=Circle(5)) // will live as long as the `effect`
await Tick(5s) // `effect` despawns after 5 seconds, taking its sprite with it
Despawn
}
}
}
this
as an object parameter
Many functions in Easel take this
as their object parameter.
For example, see the documentation for PolygonSprite begins with this.PolygonSprite
.
That means the PolygonSprite
will be attached to this
, and will live as long as this
does.
this
from context
Because Easel attempts to find all object parameters from context,
most of the time, you do not need to explicitly specify this
as the object parameter.
It is simply implied from the context.
pub fn ship.Spaceship() { // `this` is `ship`
// The following 3 lines of code do the same thing
PolygonSprite(shape=Circle(5))
ship.PolygonSprite(shape=Circle(5))
this.PolygonSprite(shape=Circle(5))
}
By convention, you should omit this
as the object parameter unless you have a good reason to include it.
Just write PolygonSprite
, rather than this.PolygonSprite
.
Subblocks with use this
The value of this
normally comes from the object parameter of the function,
but it can be changed for a subblock using use this =
in the subblock declaration.
For example see the documentation for Spawn which says use this = entity
in its function signature.
Conventionally, if a function spawns a new entity, it will change the value of this
to the spawned entity
for the subblock to make it easy for you to set up the new entity.