Skip to main content

This

The this variable is an implicit context variable that refers to the current entity.

this refers to the subject 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() {
// Same as `ship.PolygonSprite(...)` because `this` is `ship` in this block
PolygonSprite(shape=Circle(5)) // will live as long as the `ship`

Spawn projectile {
// Same as `projectile.PolygonSprite(...)` because `this` is `projectile` in this block
PolygonSprite(shape=Circle(5)) // will live as long as the `projectile`

Subspawn effect {
// Same as `effect.PolygonSprite(...)` because `this` is `effect` in this block
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
}
}
}

If a function changes the value of this for its subblock, it will be noted in the function signature. For example see the documentation for Spawn which says this = entity in its function signature.

Functions should not normally change this. Functions should only change this when they are spawning a new entity and their subblock is intended to define the main purpose of the entity.