I have an array of PackedScenes which I select at random. If the scene selected contains a value of 3, then I just add the first PackedScene in the array to the current scene. I want to check the scene's data value before determining whether it's added to the current scene. The only way I could access that data was to instance the PackedScene. I was wondering if It's correct to delete instances that aren't added to the scene as a child? I simplified my code below so it focuses on the question. Thanks in advance.
using Godot;
using System;
public class Example : Node
{
private PackedScene[] _possibleScenes ={
GD.Load<PackedScene>("res://1.tscn"),
GD.Load<PackedScene>("res://2.tscn"),
GD.Load<PackedScene>("res://3.tscn"),
GD.Load<PackedScene>("res://4.tscn"),
GD.Load<PackedScene>("res://5.tscn"),
GD.Load<PackedScene>("res://6.tscn")
};
public override void _Ready() { SpawnScene(); }
private void SpawnScene()
{
GD.Randomize();
uint rand = (uint)(GD.Randi() % _possibleScenes.Length);
Test newScene = (Test)_possibleScenes[rand].Instance();
//if data member in newScene is == 3, instance the first scene
if (newScene.val == 3)
{
newScene.QueueFree(); //delete instance at end of frame
newScene = (Test)_possibleScenes[0].Instance();
}
AddChild(newScene);
}
}