0 votes

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.


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.

Godot version v3.4.4.stable.official [419e713a2] for Linux
in Engine by (19 points)

1 Answer

0 votes

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...

by (13,112 points)

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.