rj-action-library/Runtime/Actions/LoadScene.cs

80 lines
1.5 KiB
C#
Raw Normal View History

2024-05-12 17:03:20 +00:00
using Godot;
using System.Collections.Generic;
namespace Rokojori
{
[GlobalClass]
2025-01-08 18:46:17 +00:00
public partial class LoadScene : SequenceAction
2024-05-12 17:03:20 +00:00
{
[Export]
public string scenePath;
[Export]
public Node target;
[Export]
2025-01-08 18:46:17 +00:00
public Action onLoaded;
2024-05-12 17:03:20 +00:00
bool _loading = false;
int _id = -1;
List<int> _cached = new List<int>();
2025-01-08 18:46:17 +00:00
protected override void _OnTrigger()
2024-05-12 17:03:20 +00:00
{
if ( _loading )
{
_cached.Add( DispatchStart() );
return;
}
_loading = true;
_id = DispatchStart();
var errorCode = ResourceLoader.LoadThreadedRequest( scenePath );
if ( Error.Ok != errorCode )
{
_cached.Add( _id );
_id = -1;
_loading = false;
}
}
public override void _Process ( double delta )
{
if ( ! _loading )
{
return;
}
var status = ResourceLoader.LoadThreadedGetStatus( scenePath );
if ( ResourceLoader.ThreadLoadStatus.Loaded != status )
{
return;
}
_loading = false;
var packedScene = (PackedScene) ResourceLoader.LoadThreadedGet( scenePath );
var node = packedScene.Instantiate();
target.AddChild( node );
2025-01-08 18:46:17 +00:00
Action.Trigger( onLoaded );
2024-05-12 17:03:20 +00:00
DispatchEnd( _id );
_id = -1;
_cached.ForEach(
( c )=>
{
target.AddChild( packedScene.Instantiate() );
2025-01-08 18:46:17 +00:00
Action.Trigger( onLoaded );
2024-05-12 17:03:20 +00:00
DispatchEnd( c );
}
);
_cached.Clear();
}
}
}