ℹ️ Select 'Choose Exercise', or randomize 'Next Random Exercise' in selected language.

Choose Exercise:
Timer 00:00
WPM --
Score --
Acc --
Correct chars --

GDScript Scene Tree Node Finder

GDScript

Goal -- WPM

Ready
Exercise Algorithm Area
1func find_node_recursive(root_node: Node, target_name: String) -> Node:
2# Check if the current node is the target
3if root_node.name == target_name:
4return root_node
5
6# Iterate through children recursively
7for child in root_node.get_children():
8var found_node = find_node_recursive(child, target_name)
9# If found in a subtree, return it immediately
10if found_node != null:
11return found_node
12
13# Node not found in this subtree
14return null
15
16
17func find_node_in_tree(root_node: Node, target_name: String) -> Node:
18# Handle cases where the root node itself is null or invalid
19if root_node == null:
20printerr("Error: Root node is null.")
21return null
22
23# Handle empty target name, which is invalid for node names
24if target_name.empty():
25printerr("Error: Target name cannot be empty.")
26return null
27
28# Start the recursive search
29return find_node_recursive(root_node, target_name)
Algorithm description viewbox

GDScript Scene Tree Node Finder

Algorithm description:

This GDScript code implements a recursive function to search for a specific node within a Godot Engine scene tree by its name. It starts from a given root node and explores its children and their descendants until the target node is found or the entire tree is traversed. This is commonly used in game development to locate UI elements, character components, or other game objects dynamically.

Algorithm explanation:

The `find_node_recursive` function performs a depth-first search (DFS) on the scene tree. The base case for the recursion is when the current node's name matches the `target_name`, in which case the node is returned. If not, it iterates through the current node's children, recursively calling itself on each child. If a recursive call returns a non-null node, it means the target was found in that subtree, and the function immediately returns it, preventing further unnecessary searching. The `find_node_in_tree` function acts as a public interface, adding initial validation for the root node and target name before initiating the recursive search. This approach ensures that the search terminates as soon as the node is found, optimizing performance. The time complexity is O(N) in the worst case, where N is the total number of nodes in the subtree being searched, as each node might be visited once. The space complexity is O(H) due to the recursion depth, where H is the maximum height of the scene tree.

Pseudocode:

function find_node_recursive(currentNode, targetName):
  if currentNode.name equals targetName:
    return currentNode
  for each child in currentNode.children:
    found = find_node_recursive(child, targetName)
    if found is not null:
      return found
  return null

function find_node_in_tree(rootNode, targetName):
  if rootNode is null:
    print error
    return null
  if targetName is empty:
    print error
    return null
  return find_node_recursive(rootNode, targetName)