Access a Nodes Button class

Godot Version

Godot 4.3

Question

I am using Node.get_children() to to get all of a Nodes children. But the function returns Nodes (as Array[Node]). Some of these Nodes are Buttons. These are the ones I actually want, so i wrote a function that iterates through the returned Array[Node] and should return a filled Array[ Button].

The Problem is, I have no way of recognising whether one of the Nodes is a Button or not.

Here’s my code:

var children = button_parent.get_children()
var buttons = get_btn_from_nodes(children)

func get_btn_from_nodes(nodes):
	var res : Array[Button]
	for node in nodes:
       #this is where the magic should happen
       #if :
	      res.append(node)
	return res

I am stuck and please need help, how can i access the Button from script?
The fact i havent found anything similar means its either super basic or just not possible, in which case it would be nice if you could point me in the right direction.

Solution:

i have to apologize, I somehow expected that TextureButton
inherits from Button and could therefore be found with If child in Button: which, as i now know, was false. Button and TextureButton both inherit from BaseButton.
Thank your for your help

Try

func get_btn_from_nodes(nodes):
	var res : Array[Button]
	for node in nodes:
                  #this is where the magic should happen
                  #if :
		if node is Button:
			res.append(node)
	return res
4 Likes

hi, thanks for your answer and sorry for my late reply, i was a bit busy.

sadly your solution does not work, the function returns an empty array

func buttons_pls() -> Array[Button]:
	var Children : Array[Node] = self.get_children()
	var MyButtons : Array[Button]
	for Child in Children:
		if Child is Button:
			MyButtons.append(child)
	return MyButtons

Have a go with this one

1 Like

hey, i found the mistake i made. thanks for working on my problem:)

1 Like

no worries mate :slight_smile:

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.