Export a Dictionary containing child nodes - loss of nodes in the value field
Asked Answered
I

3

0

Hi
I'm trying to build a paperdoll system, constructed from a parent node and sprite2D children of the node representing the layers. I am using @tool so that I can customise the various attributes of the character and save these out as separate scenes for NPCs etc. The parent node has a dictionary containing a reference to it's children. The children are created in code during the parents _ready() function. I use a dictionary for quick access to the "layers" for when I need to swap out textures or parameters of the sprites shaders etc (using resources that I have created for various components). Having been at this for a couple of weeks and happy with the results, my code has stop working! I know its probably something really simple but I think I must have gone code blind because I can't resolve the issue. My dictionary has the keys but now has <empty> for the values.

Here is some example code that replicates the issue:

@tool

extends Node2D

@export var spriteNodes: Dictionary

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	for i in 10:
		var node: Sprite2D = Sprite2D.new()
		node.name = "Node"+str(i)
		add_child(node)
		node.owner = get_tree().edited_scene_root
		spriteNodes[i] = node   

I have also tried creating the nodes from within a child of parent, so they are then visible to the parent, just in case this is a _ready() ordering issue, however the problem persists. Hopefully there is something painfully obvious that I am missing.

Interrex answered 12/3 at 16:42 Comment(0)
F
0

Could be a lot of things:
1 - don't have the dictionary as export. Exports are shared between instantiated scenes. If you have more than one of this, then that must be the problem.
2 - should be for i in range(10) (Idk, it's the syntax in python, maybe godot is different).
3 - your node is named node. This is bad, change it. node is a class, class names should not be used as variable names.
4 - you are changing the... node.owner ??? I'm confused about this part. If you need a node to be children of a specific node, call add_child from that node. For example if you want your node to be a child of the root, you do: get_tree().add_child(mynode)
5 - spriteNodes[i] = node This might be the problem too. You are assigning the class node instead of an actual object.

Fidellas answered 13/3 at 2:19 Comment(0)
I
0

Thanks for trying to answer the question Vidar , however I think you are missing the point of the question. The code is for example only to reproduce the problem in Godot. Some answers to your points:

  1. This problem persists within a single scene, the children of the root node belong to the same scene.
  2. Godot allows x in y forms, however this was only as an example here
  3. This is example code only and case sensitive; however, I agree it is good practice to avoid naming variables after classes, but that isn't the issue here
  4. This is quite standard in Godot for tool scripts, you need to have assigned an owner to enable the nodes to appear in the editor.
  5. See 3, "node" is not a class here; however "Node" would be - I could have called it "aNode" to make it clearer in the example code, but I'm grateful you tried to help
Interrex answered 13/3 at 8:42 Comment(0)
I
0

Just to update, at runtime the dictionary is populated and working as expected, so this may be something to do with how dictionaries function within a tool script within the editor.

Interrex answered 13/3 at 9:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.