Help filtering node types from array

Godot Version

Godot 4

Question

I have an array like this:

[player:<CharacterBody3D#30685529293>, WorldEnvironment:<WorldEnvironment#31037850919>, SpotLight3D:<SpotLight3D#31054628136>, barrier:<StaticBody3D#31071405353>, watermelon:<Node3D#31121737004>]

How do i filter the array to return all the watermelons, to have smthn like this:

[watermelon:<Node3D#31121737004>]

I also tried making a function to do this but it’s just returning […]
(by “…” i mean nothing. i just had to put “…” because of formatting)

_get_nodes_in_array(get_node("watermelon"), get_tree().root.get_children()[0].get_children())


func _get_nodes_in_array(type, arr):
	var nodes = []
	for i in arr.size():
		if arr[i] == type:
			nodes.append(arr[i])
	return nodes

im new to godot so i might ask alot of followup questions

Change from the ‘==’ operator to the ‘is’ operator to compare variable/node types:

func _get_nodes_in_array(type, arr):
	var nodes = []
	for i in arr.size():
		if arr[i] is type:
			nodes.append(arr[i])
	return nodes

im getting
Parser Error: Local parameter “type” cannot be used as a type.
i even tried changing type to ntype and is still didnt work

how would i get a node’s type, example:
the node name might be watermelon2 but the type would be watermelon or smthn

mvrm, i did what this guy did more or less https://www.youtube.com/watch?v=3WkkVKhw3HM

that is a guide for node js: what is node js and it’s benefits

that isn’t node js it is a godot tutorial.

really?
sorry! I just wanted to help

1 Like

no your good

1 Like