When trying to add a node to the tree from a ldtk post import script, something weird that I don’t understand happens. Minimal reproducible example is more at the bottom. This is the script:
@tool
var tube_scene = preload("res://scenes/tube/tube_entity.tscn")
func post_import(entity_layer: LDTKEntityLayer) -> LDTKEntityLayer:
print("Importing entities on " + str(entity_layer))
var definition: Dictionary = entity_layer.definition
var entities: Array = entity_layer.entities
var i := 0
for entity in entities:
if entity.identifier == "Tube":
var node := tube_scene.instantiate()
node.name = "Tube_2 " + str(i)
print("Adding new node " + node.name)
print_node_tree(node)
entity_layer.add_child(node)
node.owner = entity_layer
i += 1
#node.get_child(0).init(entity.fields.Point)
pass
return entity_layer
What’s the problem you wonder? First, let’s take a look at my tube scene (lt’s just a couple of nodes)
Alright, now, when I instantiate that using the previous script (it runs automatically when I make changes to the LDTK scene), it happens this:
And this is pretty annoying. As you could see in the sube scene screenshot. Path is a unique name node. Changing its name breaks everything. Why does it change tho? There isn’t any other node called that way at the same nesting level. But for some reason, when saving the scene, the warning tells otherwise…
Something is wrong in the code, I can’t really figure it out yet because I’m not sure what you’re trying to do, but it looks like you’re being recursive, or you’re nesting infinitely. By adding a tube to the entities, and by trying to create a new tube for every entity with the Tube identifier, you’re also adding new tubes for the tubes you’ve just added. Until for some reason Godot notices you’re doing something recursive and stops the loop or it just stops by chance.
At a certain point Godot notices you’re duplicating stuff and tells you it’s going to rename the new object because there are already objects with that name.
I mean, you loop the entities and add a new Tube entity for every Tube entity… but the loop is not over yet because the set you’re looping is constantly growing, so it will keep iterating for the new Tube entities you just added. Am I making myself clear?
In LDTK you have entities you can “parse” in the import script. Right now I only have one Tube entityh. The loop in the script only runs one iteration. And there I just instantiate a tube that gets added as a child of the Entities node (entity_layer in the code). It’s not recursive because that loop doesn’t take into account the tubes I add, the entities definition is dictionary that is already defined when passed to that function by LDTK. Even if I remove the loop and I instantiate a single tube, the problem still remains
But if instead of adding the child in that script I do it in runtime, it works fine.
What is weirder, if you run the project and check the remote tab, the node is displayed as Tube_2 0/Path instead of Tube_2 0/Path2
So… I have clear that it’s not a recursion problem, but I don’t know what it could be neither…
I think I got it.
It’s because you’re using a unique name for the node (%)
So whenever you instantiate a second one you’ll get the error that there is an object with that name already, and will try to rename it so they can be unique. It seems like you already have the “Path” in the node/scene marked in green in my screenshot, am I right? so when you instantiate the second one you get a conflict because 2 objects are using the same unique name “Path”.
Solution: don’t use unique names in the Tube scene.
Same thing happens to me when I try to create 2 nodes called “Path”. I set the first one as Unique (%) and when I rename the second one, Godot automatically changes it to Path2.
Apparently this triggers when you ADD the node to the scene, this is why you still see “Path” when you do print_node_tree(node), because it’s in memory but it’s not added anywhere, so it’s not generating conflicts yet. It’s when you add it to the scene that it gives you the error and renames the node.
That doesn’t make sense. Unique names are local to its scene, that means, as long as in the same nesting level there isn’t another node named the same there shouldn’t be conflict. Otherwise unitque names would be useless. if you manually add two tube scenes to the tree you’ll see each one of them has its own “Path” node and Godot doesn’t rename it. This issue only happens when called from the LDTK post import script and it’s weird because it doesn’t rename the root node of the scene but rather the first child
Hmm… I see. Well, I don’t know then. I haven’t used LDTK so I don’t know the inner workings. But keep in mind that automatic rename might be something to do with the Unique name. Try not using a unique name, just to see if the error goes away.
Also, what does the test1 scene have? does it have a “Path” object that could generate conflicts with the Tube.Path? Because, as you mentioned, if you create other Tubes, there’s no problem. The problem comes when it’s added to THAT tree, so there’s something going on in that tree.
On the other hand, could you NOT add it to the yellow tree? Could you just add it directly to the test-1scene Node or to another node outside that tree?
Not using unique name doesn’t solve it, that’s not the problem, test1-scene in the provided project just have “test1” which is yellow because that’s the level imported from LDTK. No, it doesn’t hve a “Path” object and even if it had one, it shouldn’t give a conflict. I’m not spawning a path objectt named path inside my tube node at any moment, that’s the thing.
The only important thing about LDTK here, is that when you import a level, it can run post import scripts. I use one of those to read the entity data from LDTK and load the actual entities that I will use in my game. And the weird thing is that instantiating the node there gives me this issue, but any other kind of instantiation outside of that node doesn’t.
If you have some doubt I encourage you to check the provided project, the setup is as minimal as it can be and you can check for yourself anything you think that could be causing it. I will continua trying to figure out too what’s happening but as of now I have no idea because this is very specific
What happens if you do print_node_tree to the whole scene? Or at least to “test1”
Just to see if there’s a residual “Path” node hidden anywhere we are not seeing?
I’m still confident there’s a “Path” object hidden somewhere in that yellow tree that’s conflicting with the one you’re trying to add.
The error message is pretty clear and every time I got it, even if I wasn’t using LDTK, it was for that reason (I stopped using unique names altogether, I found them to cause more trouble than what they solve)
There is no need to do that as that would be the same as viewing the remote view, and as I stated before, in the remove view the node is “Path” instead of “Path2” which doesn’t make sense… Why does a node changes from local to remote view? Anyways, here is the screenshot so you can check: