Skip to main content

AltaCoroutine

A class in Alta.Coroutines, the global namespace.

A wrapper around a Unity coroutine that tracks its state, catches exceptions and supports cancellation and awaiting. Runs on the given MonoBehaviour, or on the global AltaCoroutineHandler singleton if none is given.

Constructors​

MemberWhat it is
AltaCoroutine()Creates an AltaCoroutine with default values.

Fields​

4 private and internal fields
MemberWhat it is
private CancellationToken cancellationToken checked after each step; when cancelled the coroutine calls Stop.
private static List<Action> delayCallsCalls queued by DelayCall for the end of this frame.
private static List<Action> delayCallsSwapSecond list swapped with delayCalls while queued calls run, so calls added meanwhile wait for the next frame.
private static Logger loggerNLog logger for this class.

Properties​

MemberWhat it is
Coroutine Coroutine { get; private set; }The underlying Unity coroutine once started.
static bool IsInitialized { get; }Static: true once the global AltaCoroutineHandler exists.
Action<Exception> OnError { get; set; }Called with the exception if the coroutine throws. When unset, the error is logged instead.
Action OnFinished { get; set; }Called when the enumerator runs to the end.
Action OnStopped { get; set; }Called when the coroutine is stopped or its cancellation token fires.
AltaCoroutineState State { get; protected set; }Running, finished, stopped or error.
protected IEnumerator Enumerator { get; set; }The enumerator the coroutine steps through.
2 private and internal properties
MemberWhat it is
private static AltaCoroutineHandler AltaCoroutineHandler { get; }The AltaCoroutineHandler singleton that runs coroutines with no target object.
private MonoBehaviour TargetObject { get; set; }The behaviour the Unity coroutine runs on; defaults to the handler singleton. If it is destroyed, the coroutine stops with it.

Methods​

MemberWhat it is
static AltaCoroutine Create(IEnumerator enumerator, MonoBehaviour targetObject = null, CancellationToken cancellation = null)Static: makes a coroutine without starting it. Call Start on the result later.
static void DelayCall(Action call)Static: runs an action at the end of the current frame (all such calls are batched), or after a number of seconds with the delay overload.
static void DelayCall(float delaySeconds, Action call)Static: runs an action at the end of the current frame (all such calls are batched), or after a number of seconds with the delay overload.
static void DuoDelayCall(Action call)Static: runs an action after the end of this frame and the next fixed update.
Task<bool> GetTask()Returns a task that completes true when finished, false when stopped, or faults on an error. Adding it also replaces the default error log.
static AltaCoroutine Start(IEnumerator enumerator, MonoBehaviour targetObject = null, CancellationToken cancellation = null)Static version creates and starts a coroutine. The instance version starts it, warning if it is already running.
virtual AltaCoroutine Start()Static version creates and starts a coroutine. The instance version starts it, warning if it is already running.
virtual void Stop()Stops a running coroutine and calls OnStopped.
4 private and internal methods
MemberWhat it is
private IEnumerator CoroutineHandler(IEnumerator enumerator) (iterator)Wraps the enumerator: steps it, catches exceptions (calling OnError or logging), stops on cancellation and calls OnFinished at the end.
private static IEnumerator Delay() (iterator)Coroutine bodies for DelayCall: one runs all queued calls at the end of the frame, the other waits the given seconds then calls back.
private static IEnumerator Delay(float delaySeconds, Action callback) (iterator)Coroutine bodies for DelayCall: one runs all queued calls at the end of the frame, the other waits the given seconds then calls back.
private static IEnumerator DuoDelay(Action callback) (iterator)Coroutine body for DuoDelayCall: waits for the end of the frame and the next fixed update, then calls back.

Used in​

Members of other types that take, return or hold a AltaCoroutine:

EffectHandler.unitsToRegen, RecipePlacer.coroutineHandler, ChunkSync.running, RepeatCommandModule.running, CookingPot.checkingForConversionTemperature, FruitNinja.spawningRoutine, EquipmentCustomizationRoom.playerLeftBoundaryChecker, EquipmentCustomizationRoom.poseCheckingDelay, HeatSourceBase.extinguishCoroutine, HeatSourceBase.lightingCoroutine, ActiveNest.coroutine, AnimatedFloatTransitionAiHook.coroutine, StabilizePickupJoint.recreateJointRoutine, PourableContainer.pouringCoroutine, TouchScreenButtonVisual.growRoutine, INetworkEntity.StartCoroutine, NetworkColliderGroup.onConnectDelay, NetworkEntity.StartCoroutine, HapticWhileHeld.haptic, CombatState.getOutOfCombatCoroutine, DeadState.animationCoroutine, DeadState.handleDeathCoroutine, DownedState.deathCountdownRoutine, ShowNames.NameBoardHandler.delayToActivate, ToggleEnemyHealthBars.rotationUpdater, DiscreteStatBarVisual.pingCoroutine, StatBarSimple.changeAnimation, AutoContainerLid.relidCoroutine, BowString.activeCoroutine, BowStringPullbackInputMethod.timeoutCoroutine, BowStringPullbackInputMethod.waitForTime, ButtonCounter.downButtonPressed, ButtonCounter.upButtonPressed, ButtonCounter.waitingForChangeToFinish, CapPickup.checkingPop, CharacterRigPoser.posingAnimation, ColliderGroup.IgnoreCollisionsForDuration, CurveOutput.coroutine, DestroyByTime.destroyCoroutine, DragTrigger.dragUpdater, EquipmentRoomAnimator.boundaryAnimation, EruptAreaAttack.attacking, Fuse.fuseCoroutine, GarbageCollectTimer.runGcCollect, InGameInspector.displayingMessage, ItemShooter.coroutine, MaterialToggler.running, MRK_ActivationLever.columnAnimation, NetworkingUtilities.StartEntityCoroutine, ParticleMeshReplacePooledObject.releaseRoutine, PickupDockingChecker.currentCheckingCoroutine, PickupInteractionScaler.scalingAnimation, PlayerMessageDisplay.messageDisplayCoroutine, PlayerScreenFader.currentFade, PlayerScreenFader.CustomFade, PlayerScreenFader.FadeBackIn, PlayerScreenFader.FadeToBlack, PlayerScreenFader.SetFadedAndFadeBackIn, PlayerSpecialEffectsController.StartDeathAnimation, RendererMaterializerHandler.materializationAnimation and 7 more.