Godot Version
4.5
Question
I’m programming a game where i have a corridor with some terminals, when you press a button in front of the selected terminal in the pic it creates another corridor with terminals too.
The problem is that when i add a PackedScene to the terminal, to instantiate another corridor, Godot shows a message about recursion that the corridor can’t instance a corridor. How can avoid this error or another way to instantiate the corridor from the terminals that are inside of the corridor scene?
Hi,
A scene cannot contains an instance of itself as that would indeed lead to an infinite recursion issue. If you’re doing something like this, the error message you’re getting is normal and you should do your instantiation inside another node that will not create any recursion.
It should work at runtime though, in case that was the question. I just tried with a very simple script and I can instantiate a new instance of my scene inside of an existing one with no problem.
If that does not help, can you share the message Godot is showing?
1 Like
Thank for your response, the terminal has an array of resources. This resources has a property of the packed scene to instance (can be different type of corridor), when i asign this resources to the array and then try to populate the property with the scene godot says that i cant add that scene to avoid recursion.
The idea was to clicking on the terminal, the terminal destroys and another corridor appear with another terminal. For me this is a controled recursion but looks like is not for godot, even if i dont instance the scene insde the corridor and i do to the parent.
Unrelated to your recursion problem, you are going to run into performance problems with your CSGBoxes if you instantiate many at runtime. If you select your CSGCombiner3D node, you will see a button in the toolbar in the 3D window that allows you to create Mesh. Press that button and use the created mesh for your corridors in-game.
If you want to avoid the recursion problem, just store the corridor as its own scene and store the path to the scene in your corridor. Then you can create an instance using the path in your code and there will be no recursion error. But I still recommend you follow @sixrobin 's advice.
1 Like
Thanks for your hint but im using CSG only for blocking.
As you say i have the corridor scene saved and asigned to the resource in the array of the corrydor of possible rooms to instance, but that doesnt work, godot return an empty list if it detects its own scene inside the array.
If it’s not an array of Strings then you’re missing what I suggested. Store the file path as a String, not a reference to the corridor.
1 Like
Okay, i understand now, i will try that. Can be the problem that when its a reference it autoloads the scene?
Exactly. In fact the problem is the reference IS a reference, it’s literally the same object twice in the same spot in memory.
1 Like