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

80 lines
1.5 KiB
C#

using Godot;
using System.Collections.Generic;
namespace Rokojori
{
[GlobalClass]
public partial class LoadScene : RJSequenceAction
{
[Export]
public string scenePath;
[Export]
public Node target;
[Export]
public RJAction onLoaded;
bool _loading = false;
int _id = -1;
List<int> _cached = new List<int>();
public override void _OnTrigger()
{
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 );
Actions.Trigger( onLoaded );
DispatchEnd( _id );
_id = -1;
_cached.ForEach(
( c )=>
{
target.AddChild( packedScene.Instantiate() );
Actions.Trigger( onLoaded );
DispatchEnd( c );
}
);
_cached.Clear();
}
}
}