@@ -572,26 +572,43 @@ func add_node(params):
572
572
if debug_mode :
573
573
print ("Pack result: " + str (result ) + " (OK=" + str (OK ) + ")" )
574
574
575
+ var save_error = ERR_CANT_CREATE # Initialize with an error state
576
+
575
577
if result == OK :
578
+ absolute_scene_path = ProjectSettings .globalize_path (full_scene_path ) # Ensure absolute path is defined here (Removed var)
576
579
if debug_mode :
577
580
print ("Saving scene to: " + absolute_scene_path )
578
- var save_error = ResourceSaver .save (packed_scene , absolute_scene_path )
581
+ save_error = ResourceSaver .save (packed_scene , absolute_scene_path ) # Use absolute path for saving
579
582
if debug_mode :
580
583
print ("Save result: " + str (save_error ) + " (OK=" + str (OK ) + ")" )
584
+
581
585
if save_error == OK :
586
+ # Simplified success message for clarity
587
+ print ("Node '" + params .node_name + "' of type '" + params .node_type + "' added successfully" )
588
+ # Add a small delay after successful save before cleanup
582
589
if debug_mode :
583
- var file_check_after = FileAccess .file_exists (absolute_scene_path )
584
- print ("File exists check after save: " + str (file_check_after ))
585
- if file_check_after :
586
- print ("Node '" + params .node_name + "' of type '" + params .node_type + "' added successfully" )
587
- else :
588
- printerr ("File reported as saved but does not exist at: " + absolute_scene_path )
589
- else :
590
- print ("Node '" + params .node_name + "' of type '" + params .node_type + "' added successfully" )
590
+ print ("Waiting briefly after save..." )
591
+ OS .delay_msec (100 ) # 100ms delay
591
592
else :
592
593
printerr ("Failed to save scene: " + str (save_error ))
594
+ # No quit here, proceed to cleanup
593
595
else :
594
596
printerr ("Failed to pack scene: " + str (result ))
597
+ # No quit here, proceed to cleanup
598
+
599
+ # --- Start Cleanup ---
600
+ # Removed packed_scene.free() as PackedScene is RefCounted
601
+ # Reinstate scene_root.free() to prevent RID leak
602
+ if scene_root and is_instance_valid (scene_root ):
603
+ if debug_mode :
604
+ print ("Freeing instantiated scene root node after delay" )
605
+ scene_root .free () # Free the instantiated scene root
606
+ # --- End Cleanup ---
607
+
608
+ # Quit only if there was a critical error during packing or saving
609
+ if result != OK or save_error != OK :
610
+ quit (1 ) # Exit with error code if packing or saving failed
611
+ # Otherwise, the script will naturally exit after _init finishes
595
612
596
613
# Load a sprite into a Sprite2D node
597
614
func load_sprite (params ):
0 commit comments