Skip to main content

ActionBase

Important Attention Reminder

Coding Style wiki


ActionBase is the abstract base class of all ActionSystem actions. Custom actions inherit from it and override the OnStart / OnUpdate / OnDone lifecycle methods, controlling the duration with SetDuration (set -1 to end by condition, which requires calling MarkAsDone manually). It also has a built-in sub action mechanism for composing child actions inside an action.

NamespaceOxGKit.ActionSystem
Typepublic abstract class ActionBase
SourceActionBase.cs
using OxGKit.ActionSystem;

Reminder Create a custom Action skeleton quickly via Right-Click Create/OxGKit/Action System/Template Action.cs.

Quick Start

Timed action: set the duration in OnStart; the base class automatically marks the action done (firing OnDone) when the time elapses. Combine with GetTimeElapsedRatio for progress-driven effects.

using OxGKit.ActionSystem;
using UnityEngine;

public class FadeOutAction : ActionBase
{
#region Default Constructor
public FadeOutAction()
{
this.name = nameof(FadeOutAction);
}

public FadeOutAction(int uid) : this()
{
this.uid = uid;
}
#endregion

public CanvasGroup canvasGroup;
public float duration;

/// <summary>
/// [Factory Mode] Create an action instance through a static method
/// </summary>
public static FadeOutAction CreateFadeOutAction(CanvasGroup canvasGroup, float duration)
{
var action = new FadeOutAction();
action.canvasGroup = canvasGroup;
action.duration = duration;
return action;
}

protected override void OnStart()
{
// Set the duration (auto MarkAsDone when elapsed)
this.SetDuration(this.duration);
}

protected override void OnUpdate(float dt)
{
// Fade by elapsed-time ratio (0 to 1)
this.canvasGroup.alpha = 1f - this.GetTimeElapsedRatio();
}

protected override void OnDone()
{
this.canvasGroup.alpha = 0f;
}
}

Condition-based action: SetDuration(-1) means end by condition; you must call MarkAsDone yourself.

using OxGKit.ActionSystem;

public class WaitForServerAction : ActionBase
{
public WaitForServerAction()
{
this.name = nameof(WaitForServerAction);
}

protected override void OnStart()
{
// -1 = end by condition, MarkAsDone must be called manually
this.SetDuration(-1);

// Finish when the condition is met
Server.Request(ok => this.MarkAsDone());
}
}

General Rules

Lifecycle

  1. RunStart: Reset() clears state first (flags, elapsed time, sub actions), the action is marked started, then OnStart fires (set SetDuration and initialize there).
  2. RunUpdate(dt): driven per frame by the runner; accumulates elapsed time. While unfinished, the action is automatically marked done (MarkAsDone) when the duration elapses, otherwise OnUpdate fires; sub actions are updated as well.
  3. MarkAsDone: fires OnDone (once) and marks the action done; only when both the action itself and all sub actions are done is it marked all done (IsAllDone), which the runner uses to decide removal.

Completion Rules

Important An action with SetDuration(-1) never finishes on its own - guarantee that the MarkAsDone() / MarkAllDone() call path always fires (including error paths).

Attention Actions are stateful one-shots: re-running (RunStart) resets internal state, but never share one action instance across two runners simultaneously.


Members

MemberDescription
public int uidUnique action id, usable with RemoveAction(uid).
public string nameAction name (defaults to the class name), used for log identification.

Protected fields (for derived classes)

MemberDescription
protected bool _isStartedStarted flag.
protected bool _isDoneDone flag of the action itself.
protected bool _isAllDoneAll-done flag (including sub actions).
protected float _durationDuration (set via SetDuration).
protected float _timeElapsedAccumulated elapsed time.
protected QueueSet<ActionBase> _queueSubActionsSub action queue (QueueSet is a FIFO queue with unique items).

Lifecycle Overrides

Method Overview

MethodDescription
OnStartFired when the action starts (set duration and initialize).
OnUpdateFired every frame while running (not called once done).
OnDoneFired when MarkAsDone is called (once).

OnStart

protected virtual void OnStart() { }

Fired when the action starts (called by RunStart); typically sets SetDuration and runs initialization logic.

OnUpdate

protected virtual void OnUpdate(float dt) { }

Fired every frame (driven by RunUpdate); no longer called once the action is done.

OnDone

protected virtual void OnDone() { }

Fired when MarkAsDone is called (fires only once); useful for wrap-up work (callbacks, state cleanup).


Run Control

Method Overview

MethodDescription
RunStartStarts the action (resets state and fires OnStart).
RunUpdateUpdates the action (time accumulation and sub action updates).
ResetResets action state (and clears sub actions).

RunStart

public void RunStart()

Starts the action: Reset clears state, the action is marked started, then OnStart fires.

Reminder Normally called by the ActionRunner or by composite actions (SequenceAction, etc.) - you usually do not call it yourself.

RunUpdate

public void RunUpdate(float dt)

Updates the action (driven per frame by the runner): accumulates elapsed time, automatically marks the action done (MarkAsDone) when the duration elapses, otherwise fires OnUpdate; also updates sub actions and marks all done once the action itself and all sub actions are finished.

Reset

public void Reset()

Resets action state (started/done flags, elapsed time) and clears sub actions.


Status Queries

Method Overview

MethodDescription
IsStartedWhether the action has started.
IsDoneWhether the action itself is done.
IsAllDoneWhether everything is done (including sub actions).
GetTimeElapsedGets the elapsed time in seconds.
GetTimeElapsedRatioGets the elapsed-time ratio (0 to 1).

IsStarted

public bool IsStarted()

Whether the action has started (true after RunStart).

IsDone

public bool IsDone()

Whether the action itself is done (sub actions not included).

IsAllDone

public bool IsAllDone()

Whether the action is all done (including sub actions); the runner uses this to decide when to remove the action.

GetTimeElapsed

public float GetTimeElapsed()

Gets the accumulated elapsed time (seconds) since the action started.

GetTimeElapsedRatio

public float GetTimeElapsedRatio()

Gets the elapsed-time ratio (0 to 1, based on the duration set via SetDuration; returns 0 when duration is less than or equal to 0). Suitable for progress-driven effects such as lerping.


Completion Control

Method Overview

MethodDescription
SetDurationSets the duration (-1 = end manually by condition).
MarkAsDoneMarks the action done (fires OnDone).
MarkAllDoneMarks everything done (including sub actions).
CheckAndReduceTimeChecks whether the duration has elapsed (protected).

SetDuration

public void SetDuration(float duration)

Sets the action duration (seconds); the action is automatically marked done (MarkAsDone) when the time elapses. Set -1 to end by condition.

Important An action with SetDuration(-1) never finishes on its own - you must call MarkAsDone yourself (guarantee the callback path always fires, including error paths).

MarkAsDone

public void MarkAsDone()

Marks the action done: fires OnDone (once); if all sub actions are also done, the action is marked all done (IsAllDone).

MarkAllDone

public void MarkAllDone()

Marks the action all done (including every sub action); also called by the runner when removing an action (RemoveAction).

CheckAndReduceTime

protected bool CheckAndReduceTime()

Checks whether the duration has elapsed (always returns false when duration is -1); called automatically by RunUpdate, normally not called manually.


Sub Actions

Protected methods for derived classes to compose sub actions inside an action. Sub actions are updated automatically by the base RunUpdate; the action is marked all done only when the action itself is done and all sub actions are done.

Method Overview

MethodDescription
AddSubActionAdds and starts a sub action.
IsSubActionAllDoneChecks whether all sub actions are done.
UpdateSubActionsUpdates all sub actions.
MarkSubAllDoneMarks all sub actions done.
ClearSubActionsClears sub actions.

AddSubAction

protected ActionBase AddSubAction(ActionBase action)

Adds a sub action (the same instance is never added twice; its OnStart fires immediately on add). Returns the action itself.

IsSubActionAllDone

protected bool IsSubActionAllDone()

Checks whether all sub actions are done (returns true when there are none).

UpdateSubActions

protected void UpdateSubActions(float dt)

Updates all sub actions (called automatically by RunUpdate).

MarkSubAllDone

protected void MarkSubAllDone()

Marks all sub actions as all done (called automatically by MarkAllDone).

ClearSubActions

protected void ClearSubActions()

Clears the sub action queue.