ActionBase
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.
| Namespace | OxGKit.ActionSystem |
| Type | public abstract class ActionBase |
| Source | ActionBase.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
- RunStart:
Reset()clears state first (flags, elapsed time, sub actions), the action is marked started, then OnStart fires (setSetDurationand initialize there). - 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.
- 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
| Member | Description |
|---|---|
public int uid | Unique action id, usable with RemoveAction(uid). |
public string name | Action name (defaults to the class name), used for log identification. |
Protected fields (for derived classes)
| Member | Description |
|---|---|
protected bool _isStarted | Started flag. |
protected bool _isDone | Done flag of the action itself. |
protected bool _isAllDone | All-done flag (including sub actions). |
protected float _duration | Duration (set via SetDuration). |
protected float _timeElapsed | Accumulated elapsed time. |
protected QueueSet<ActionBase> _queueSubActions | Sub action queue (QueueSet is a FIFO queue with unique items). |
Lifecycle Overrides
Method Overview
| Method | Description |
|---|---|
| OnStart | Fired when the action starts (set duration and initialize). |
| OnUpdate | Fired every frame while running (not called once done). |
| OnDone | Fired 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
| Method | Description |
|---|---|
| RunStart | Starts the action (resets state and fires OnStart). |
| RunUpdate | Updates the action (time accumulation and sub action updates). |
| Reset | Resets 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
| Method | Description |
|---|---|
| IsStarted | Whether the action has started. |
| IsDone | Whether the action itself is done. |
| IsAllDone | Whether everything is done (including sub actions). |
| GetTimeElapsed | Gets the elapsed time in seconds. |
| GetTimeElapsedRatio | Gets 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
| Method | Description |
|---|---|
| SetDuration | Sets the duration (-1 = end manually by condition). |
| MarkAsDone | Marks the action done (fires OnDone). |
| MarkAllDone | Marks everything done (including sub actions). |
| CheckAndReduceTime | Checks 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
| Method | Description |
|---|---|
| AddSubAction | Adds and starts a sub action. |
| IsSubActionAllDone | Checks whether all sub actions are done. |
| UpdateSubActions | Updates all sub actions. |
| MarkSubAllDone | Marks all sub actions done. |
| ClearSubActions | Clears 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.