Skip to main content

Subject Parameters

Functions can take a subject parameter, which is a special kind of parameter that comes before the . in a function call.

fn vegetable.FunctionThatTakesASubject() { }
fn Example(carrot) {
carrot.FunctionThatTakesASubject
}

If the subject parameter is not passed in explicitly, it will be found from context. It is conventional to not specify the subject parameter directly and instead let it be found from context.

fn vegetable.FunctionThatTakesASubject() { }
fn Example(carrot) {
Spawn vegetable {
FunctionThatTakesASubject // the subject parameter `vegetable` is found from context
}
}

Automatic 'this' declaration

The subject of a function automatically gets bounded to the this identifier. This is useful as many functions will a subject parameter named this. Conventionally, this always designates the main entity being manipulated within the current block of code.

fn this.DoSomethingCool() { }
fn Example() {
Spawn vegetable {
DoSomethingCool // the subject parameter `this` is assigned `vegetable` from context
}
}

Subjects imply lifespans

The subject parameter is considered the entity that is doing the action carried out by this function. By convention, if the function performs any long-lasting behavior, the subject parameter is the entity that owns the behavior, and so when the subject dies, the behavior should stop.

fn this.DoSomethingCool() { }
fn Example() {
Spawn vegetable {
DoSomethingCool
await Tick(5s)
Expire
// By convention, DoSomethingCool should end now that the projectile has expired
}
}