Asynchronous execution

Since it’s not possible to execute coroutines on scriptable objects, you can use async/await. See the following code examples:

// use this namespace
using System.Threading.Tasks;

[DataObjectDropdown(true, sceneComponentType: typeof(GameObject))]
public SceneComponent targetGameObject;
public float movementSpeed = 10;

public override void ExecuteNode()
{
     // start the async method
     Move();
}   

// Async method
async void Move()
{
     // Get the scene game object reference
     var _obj = targetGameObject.GetReference<GameObject>(this);
     
     while (true)
     {
          // move the object
          _obj.transform.position += _obj.transform.forward * Time.deltaTime * movementSpeed;
          
          // similar to Unity's yield return null to wait for one frame            
          await Task.Yield();
     }  
}

Databrain.UnityAsync

Logic also implements the useful UnityAsync system which implements some useful helpers. Use it by implementing the namespace:

It is recommended that you use Databrain.UnityAsync to prevent some unexpected behaviour like code execution in the editor etc.

using Databrain.UnityAsync

Then instead of using:

await Task.Yield();

like in the code example above, you can use:

await Await.NextUpdate();

or if you want to wait for some seconds:

await Await.Seconds(3);

Last updated