get_child() is not able to fetch my Tween?

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

I was following a tutorial on creating cutscenes using a Camera2D and Tween for a topdown 2D game.

I wrote the code posted below for the root node of my Scene, using the tutorial.
I have a Tween node as a child of my Camera.
However, for some reason, Godot does not seem to be able to actually fetch the “Tween” node using the get_child() method.

Code:

extends Node2D

onready var camera = $Camera

func _ready():	
	moveCamera(Vector2(400, -250), 2.0)
	
func moveCamera(targetPos : Vector2, speed : float):
	var originalPos = camera.global_position
    # Error is here:
	camera.get_child("Tween").interpolate_property(camera, "global_position", originalPos, targetPos, (targetPos - originalPos).length() / speed)

Error: Invalid type in function 'get_child' in base 'Camera2D'. Cannot convert argument 1 from String to int.

I’m pretty much stumped on how to fix this. It looks like it’s trying to convert the name of the Tween node into an integer, but I’m not sure why.

:bust_in_silhouette: Reply From: jgodfrey

As the error indicates, get_child() expects an integer argument, not a string. Looking at the docs, you’ll see that get_child() expects a node index, not a node name. Also in the description of get_child(), there’s a note that states:

To access a child node via its name, use get_node.

So, since you’re trying to access the node by its name, use get_node().

The docs are your friend in this case…

Or, you could also reference that node via $Camera/Tween

jgodfrey | 2022-04-25 20:21