Changing values on a duplicated node child

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By grymjack

I am copying a node that is a texturebox with two child labels. How do I change the text value for the child labels on that copied node?

	temp_node = get_node(node_path)
	my_copy = temp_node.duplicate()
    ??? my_copy.childlabel.text = 'blah blah'
:bust_in_silhouette: Reply From: jgodfrey

Assuming one of the labels is named Label, something like this should work:

temp_node = get_node(node_path)
my_copy = temp_node.duplicate()
add_child(my_copy)
my_copy.get_node("Label").text = 'blah blah'

Thank you. That did the trick

grymjack | 2023-02-15 01:54

I’m hoping you can help me with this. Your reply helped me with the text label issue, but I am having a similar problem. I am trying to create a flowchart on the fly. I’ve attached a screenshot of it in operation. As you can see, this chart, and many of the other tech trees, will go past the bottom of the screen. I can’t really make it any smaller as the text will get too hard to read. Even making the ‘pipes’ smaller will not help in some cases. I used a code chunk, you placed in a previous post from a couple years ago, to try and embed the chart in a vertical scroll container. However I just can’t seem to get the boxes to appear on the screen. I included the modified code from your earlier post below, as well as the code I am currently using to create the chart you see on the screenshot. I have also included a screenshot from the IDE if that will help. Please let me know what stupid mistake I am doing here. -Thanks

modified code from your earlier posted example

onready var container = $ScrollContainer
onready var vbox = $ScrollContainer/VBoxContainer
onready var scrollbar = $ScrollContainer.get_v_scrollbar()


func box_test():
	for i in range(100):
		var temp_node = get_node("ChartPieces/YellowBox/YellowBox")
		var my_copy = temp_node.duplicate()


		addItem(my_copy, (200 + (i * 20)))
		yield(get_tree().create_timer(.1), "timeout")


func addItem(node, y_pos):
	vbox.add_child(node)

	vbox.get_node("YellowBox").visible = true
	vbox.get_node("YellowBox").set_position(Vector2(200, y_pos))


	yield(get_tree(), "idle_frame")
	container.scroll_vertical = scrollbar.max_value

my code that performs the same function as seen in the screenshot

func create_node_copy_array(node_path, copy_number):
	# initialize variables
	var temp_node
	var my_copy
	var node_list = []

	# loop for creating box copies
	for loop in range(0,copy_number):
		temp_node = get_node(node_path)
		my_copy = temp_node.duplicate()
		node_list.append(my_copy)

	# returning array of nodes
	return node_list

func tech_display_box(x_pos, y_pos, iterations, flip_flag):
	# initializing variables
	var temp_node
	var x_pos_adj
	var y_pos_adj

	# loop for displaying box copies
	for loop in range(0, iterations):
		# testing for flip
		if (flip_flag == true):
			# left leaning stack
			x_pos_adj = (x_pos - (loop * 3))
		else:
			# right leaning stack
			x_pos_adj = (x_pos + (loop * 3))

		# y position shift is the same no matter the side
		y_pos_adj = (y_pos - (loop * 3))
		
		# extracting node from array and positioning
		temp_node = box_node_array[loop]
		add_child(temp_node)
		temp_node.visible = true
		temp_node.set_position(Vector2(x_pos_adj,y_pos_adj))

grymjack | 2023-02-16 13:06

I haven’t looked at any of your posted code, but I just took my originally posted code (linked by you above) and changed it to insert 100 TextureRects instead of 100 Buttons. It seems to work as exactly as expected:

extends CanvasLayer

onready var container = $ScrollContainer
onready var vbox = $ScrollContainer/VBoxContainer
onready var scrollbar = $ScrollContainer.get_v_scrollbar()
onready var rect = $TextureRect

func _ready():
	for i in range(100):
		var r = rect.duplicate()
		addItem(r)
		yield(get_tree().create_timer(.1), "timeout")

func addItem(r):
	vbox.add_child(r)
	yield(get_tree(), "idle_frame")
	container.scroll_vertical = scrollbar.max_value

How does that differ from what you’re trying to do?

jgodfrey | 2023-02-16 19:46

Thanks for taking the time to answer. When I created a brand new rect with the same yellow box texture, it worked. When I tried rect2 for my original texture, it still didn’t work. I tried dragging the original up to the top level, didn’t work. Created rect3 from scratch, renamed it, moved it back down, it worked. It looks like the Expand checkbox was throwing everything off? I tried to scale the texture after the add_child(r), didn’t work. Is syntax below correct? Do I need to go back to LibreDraw and make all my graphics the exact size I need? Thanks again for the help.

onready var rect = $TextureRect
onready var rect2 = $ChartPieces/YellowBox/YellowBox
onready var rect3 = $YellowBox

I tried to scale after the fact, but no change in the size. Is the syntax correct?

vbox.get_node("TextureRect").rect_scale = Vector2(.5, .5)

grymjack | 2023-02-16 21:19

What version of Godot are you using? Also, is the project available anywhere to take a look at. That’d certainly be easier than sifting through code snippets in a forum… :slight_smile:

jgodfrey | 2023-02-16 21:27

Nothing in github/gitlab yet. Building many different client pieces, will be integrating them soon. Depending on how you feel about getting tarballs from internet randos, I could email you the project files for this piece?

grymjack | 2023-02-16 21:41

BTW: v3.5.1.stable.official [6fed1ffa3]

grymjack | 2023-02-16 21:49

Sure, happy to take a look if you can provide me with a simple way to see the problem in live code. I don’t want to drop a real email address in a public forum, but here’s a disposable one if you want to send me a d/l link. Or, it’ll supposedly accept attachments too, but I don’t know what the limitations are:

raok4z+bcgu5qpaetumo@sharklasers.com

And, I’m still not sure what version of Godot you’re using. That could be quite relevant here…

jgodfrey | 2023-02-16 21:52

Ah, just saw your Godot version post. :slight_smile:

jgodfrey | 2023-02-16 21:54

sent a DL link to provided address

grymjack | 2023-02-16 22:52

So, a little bit of input here on the project you provided… I can get this to work roughly like you want (I think), by:

  • Set the layout of the existingScrollContainer to full rect
  • Add a basic container child control (Panel or ViewportContainer seem to work)
  • Set the rect_min_size of that container to something large enough to hold your entire flowchart (I used x800y2000 for example)
  • In the 4 places in your code where you call add_child() change that to add the child to the child control mentioned above (so, $ScrollContainer/Panel.add_child() for example)

I think that should provide the ability to vertically scroll the entire flowchart. That said, I’m really no expert in Godot’s UI system so there may very well be better ways to do this…

Also, rather than having all of your “building blocks” off-screen in the same scene, I’d recommend that you store each of them as a separate scene and instance them into the final scene as needed.

jgodfrey | 2023-02-20 16:21