using Godot; using System.Collections.Generic; namespace Rokojori { [Tool][GlobalClass] public partial class LoadScene : SequenceAction { [Export( PropertyHint.File )] public string scenePath; [Export] public Node target; [ExportGroup("Transforming")] [Export] public Node3D poseParent; [Export] public Vector3 offsetTranslation = Vector3.Zero; [Export] public Quaternion offsetRotation = Quaternion.Identity; [Export] public Action onLoaded; Node _loadedNode; public Node GetLoadedNode() { return _loadedNode; } bool _loading = false; int _id = -1; List _cached = new List(); protected 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(); _loadedNode = node; target.AddChild( node ); node.Owner = target.Owner; if ( node is Node3D n3D ) { if ( poseParent != null ) { Pose.From( poseParent ).Set( n3D ); } n3D.GlobalPosition = n3D.GlobalPosition + offsetTranslation; n3D.SetGlobalQuaternion( n3D.GlobalQuaternion() * offsetRotation ); } Action.Trigger( onLoaded ); _loadedNode = null; DispatchEnd( _id ); _id = -1; _cached.ForEach( ( c )=> { target.AddChild( packedScene.Instantiate() ); Action.Trigger( onLoaded ); DispatchEnd( c ); } ); _cached.Clear(); } } }