Getting the position of another scenes node

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

I’m currently working on a 2D platformer with ladders. I’m hoping to centre the Player on the ladder on the X axis for him to climb.

The ladder is an Area2D and emits a signal to say that the Player has entered it.

In the Player script, I wish to centre the Player to the ladder, however I don’t have a reference to the Ladder.

I wish to use something like this:

player.position.x = ladder.position.x

I have a main script on Level_01, but I realise that as the game eventually will have more levels things might break if I hard reference the Ladder (least I think so).

What’s the best way to reference the Ladder’s position?

If possible, could you supply some code to make it easier to understand?


:bust_in_silhouette: Reply From: godot_dev_

In your _ready function of your ladder, you could connect your ladder to the Area2D signals that you use to detect when the player enters, and handle the signal in your ladder script . The ladder signal handler function could re-emit the signal (e.g, “player_entered_ladder”) and it could send a reference to itself. That way, your Level_01 script could connect to the ladder’s signal and the Level_01 script will then have access to any ladder (if you add more ladders) that signals. This would support adding multiple ladders.

Thanks for your suggestion, however I’m still not clear how to use Signals and get a postion of another node.

From what I understand, to get a reference to another node in another scene, you would usually use somthing like:


To call a function from another script:


But I have no idea how to use this method either to get the ladders X position so I can use it in my Player’s script.

JayH | 2022-07-07 18:41

Below is example code to demonstrate my suggestion (I haven’t tested it, but it should give off the idea):

Assuming you have a scene tree structured as follows

  • stage (Node2D)
    • ladders (Node2D)
      • ladder1
      • ladder2

      • laddern
    • player

    #your ladder script

    signal player_entered_ladder

    func _ready():

    var area = get_node("/path/to/Area2D/child")

    #called when player enters ladder
    func _on_player_entered_ladder(playerArea):
    #we give ‘self’ so emit_signal function to notify any node (in your case the player) that
    #is interested in what ladder was entered

    #your player script

    func _on_entered_ladder(ladder):
    player.position.x = ladder.position.x

    #your stage script

    func _ready():

    var player = get_node("player")
    #we connect the player to the ladders
    #by iterating over all ladders (you can add ladders without needing to change the script)
    #in the scene as children of the stage/ladders node
    var ladders = get_node("ladders")
    for child in ladders.get_children():
    	#we found a ladder?
    	if child is preload(""):
    		#connect the player to the ladder signals, by having "_on_entered_ladder" function
    		#called every time player enters ladder

godot_dev_ | 2022-07-13 15:41