Skip to content

Commit

Permalink
Add scene-local CoroutineManager feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Inspiaaa committed Sep 6, 2024
1 parent 07af715 commit f0fd500
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions src/CoroutineManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace HCoroutines;
public partial class CoroutineManager : Node
{
public static CoroutineManager Instance { get; private set; }
private static CoroutineManager globalInstance;

public float DeltaTime { get; private set; }
public double DeltaTimeDouble { get; private set; }
Expand All @@ -21,6 +22,34 @@ public partial class CoroutineManager : Node
private DeferredHashSet<CoroutineBase> activePhysicsProcessCoroutines = new();
private HashSet<CoroutineBase> aliveRootCoroutines = new();

public override void _EnterTree()
{
Instance = this;
ProcessMode = ProcessModeEnum.Always;
IsPaused = GetTree().Paused;

if (IsAutoloaded())
{
// This instance is the global (autoloaded) instance that is shared between scenes.
globalInstance = this;
}
}

public override void _ExitTree()
{
if (Instance == this)
{
// Switch back to the global (autoloaded) manager when the scene-local instance is removed (e.g. when
// the current scene is changed).
Instance = globalInstance;
}
}

private bool IsAutoloaded()
{
return GetParent() == GetTree().Root && GetTree().CurrentScene != this;
}

/// <summary>
/// Starts and initializes the given coroutine.
/// </summary>
Expand Down Expand Up @@ -56,13 +85,6 @@ private DeferredHashSet<CoroutineBase> GetUpdatePoolOfCoroutine(CoroutineBase co
};
}

public override void _EnterTree()
{
Instance = this;
ProcessMode = ProcessModeEnum.Always;
IsPaused = GetTree().Paused;
}

public override void _Process(double delta)
{
DeltaTime = (float)delta;
Expand Down

0 comments on commit f0fd500

Please sign in to comment.