Skip to main content

Awaiting

Often in games, it is useful to wait for something to occur before performing some action. For example, a powerful spell might need to wait until it is fully charged up before being cast.

The await keyword is used to call one or more functions declared with an await fn specifier. It is used to wait for something to happen before continuing.

await fn WaitThenGiveMeANumber() -> value {
// wait for 10 ticks before returning
await Tick(10)
return 123
}

fn Example() {
let x = await WaitThenGiveMeANumber
}

What can be awaited?

await can be used to wait for any await fn to return. Sometimes, these are declared explicitly using the await fn, but sometimes they may be implicit. This is an exhaustive list of everything that can be awaited upon in Easel:

Awaiting multiple expressions

Multiple expressions can be awaited using the await keyword by separating them with commas. The await will return as soon as the first expression returns. All other expressions will be terminated. The return value of the await will be the return value of the first expression to return, but only if that expression returns a single value. Multiple return values are not supported in this form.

pub await fn WaitForACarrot() -> vegetable {
await Tick(1s)
return "Carrot"
}
pub await fn WaitForAPotato() -> vegetable {
await Tick(2s)
return "Potato"
}
pub fn Example() {
let vegetable = await WaitForACarrot, WaitForAPotato
Transmission { "You received a " + vegetable + "!" }
}
// Outputs "You received a Carrot!" after 1 second

Concurrent awaiting

A behavior is a concurrently executing block of code. It is so common to create concurrent behaviors that await on something that Easel has a shorthand for this. See the once, on and with blocks for more information.