async
- Last UpdatedApr 24, 2024
- 1 minute read
The async keyword is used to specify that a class method is asynchronous. An async method runs synchronously until it encounters a wait expression, after which the method is suspended until the wait condition is verified. Control then returns to the caller of the method. If the async method doesn't include a wait operator, it is executed synchronously.
Async methods can be used to implement loops synchronized to the game loop.
Async methods can be called using Runtime::Environment.StartAsync(asyncMethodName).
Code example
The following example shows how to declare and call an async method.
class TestWait
{
public static int waitValue;
public static bool waitVar()
{
return waitValue==0;
}
public async static void test()
{
Runtime::Environment.Print("--wait started”);
wait(waitVar()); //wait until waitValue value changes
Runtime::Environment.Print("--wait ended”);
}
public static void testStart() //when testStart is called, it calls the async method test()
{
Runtime::Environment.Print("--startedTestStart");
Runtime::Environment.StartAsync(test());
Runtime::Environment.Print("--endedTestStart");
}
}
//Output
//--startedTestStart:
//--endedTestStart
//--wait startedwait
//--wait ended (when waitValue changes its value
)