How assign a remoteTransform2D remote_path in code

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

I have a player character and I also have a camera in every level scene; and I am trying to use a remoteTransform node to get the camera to follow the player. I can set the camera with the inspector, but I would like to do this in code so that I don’t have to do the same repetitive task on every level.

What I have so far is this:

I have my Player scene with a RemoteTransform2d node as a child of a kinematic body (which represents the player), the name of the RemoateTransform2D node is ‘RemoteCamera’.

Then I have lots of level scenes, every single one of which has a Camera2D node called ‘PlayerCamera’.

Now, on my player character, I have the following code:

func _ready():
var PlayerCam = get_tree().get_root().find_node("PlayerCamera",true,false)
$RemoteCamera.remote_path = PlayerCam 

But when I run the code, I get the error message:
Invalid set index ‘remote_path’ (on base:‘RemoteTransform2D’) with vlaue of type ‘Camera2D’.

I am confused by this, when I drag the camera node from each level into the Remote Path slot in the inspector, it all works fine. Is that one different from setting the remote_path in code?

So many thanks in advance :slight_smile:

:bust_in_silhouette: Reply From: jgodfrey

If you hover over the Remote Path property in the inspector, you’ll see that it takes a NodePath to the remote node - which is a string, not an object. So, you want to assign the string-based path to the other node, relative to the location of the RemoteTransform2D in the scene tree.

But, agreed, it’s confusing based on how an “inspector-assigned” node is shown in the inspector…