KROYSDOCS
HomeWhat's newv0.1.0

Coroutine

A running routine returned by EntityScript.StartCoroutine. Keep it to stop that coroutine with StopCoroutine or to wait for it by checking IsRunning.

Kindclass
NamespaceKroys.EngineScripting
Members1
SourceCoroutine.cs
EXAMPLE

In a script

A complete script that uses Coroutine; copy it into your scripts project.

Coroutines.cs
public sealed class DoorOpener : EntityScript
{
    public float OpenSeconds = 1.5f;
    private Coroutine? moving;

    // Call this from a trigger, a button or another script.
    public void Toggle(bool open)
    {
        // Only one movement at a time: stop the running one before starting the next.
        if (moving is { IsRunning: true })
            StopCoroutine(moving);
        moving = StartCoroutine(Slide(open ? 2.5f : 0.0f));
    }

    private IEnumerator Slide(float targetHeight)
    {
        Transform transform = Transform;   // a handle: writes go straight to the entity
        Vector3 start = transform.Position;
        Vector3 end = new(start.X, targetHeight, start.Z);
        for (float t = 0.0f; t < OpenSeconds; t += Time.DeltaTime)
        {
            transform.Position = Vector3.Lerp(start, end, t / OpenSeconds);
            yield return null;              // continue next frame
        }
        transform.Position = end;
    }
}
REFERENCE

Members

Properties
#public bool IsRunning { get; }
True while the routine has more to do; false once its method returned, reached yield break or was stopped.
property
Signatures
public bool IsRunning { get; }

KroysEngine 0.1.0 documentation, built from the engine repository by Tools/BuildDocsSite.py.