0 votes

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);
    }
}
in Engine by (35 points)

Please log in or register to answer this question.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.

Categories