Accessing a script from another node with Instanced Childs

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

Hi,

I have a platform game with ladders. In my main Scene/Level I have populated various copies of my ladders around my game level. I have added them with the ‘Instance Child Scene’ button in the Scene Tree.

Each Ladder has an Area2D, Sprite and a CollisonShape2D.

I use a signal on the Ladder’s to tell the Player when he is overlapping the Ladder.
It’s in my Player script I’m starting to see an issue.

Currently in the Player’s script I’m getting the Ladders position to re-center the Player on the said Ladder.

onready var  Ladders = get_node("../Ladder")

func _on_Ladder_on_ladder() -> void:
   self.global_position.x = Ladders.ladder_position.x

The Scene Tree:
enter image description here

This works totally fine, however when I add another Ladder to the Scene Tree; Ladder2, Ladder3 etc. I can’t use the get_node() option as it’s unique to just one Ladder in the get_node path.

So this isn’t going to work for more than one ladder.

What other options are there?

You should avoid hardcoding the reference to the ladder. Instead have the ladder be provided when you signal that the player entered the ladder. That way your signal handling function that deals with ladder-player collision will also receive the ladder the player collided with

godot_dev_ | 2022-07-12 17:16

:bust_in_silhouette: Reply From: USBashka

You can add variables to your signals. Like this:

signal on_ladder(ladders)

And when the signal emits, reference the current ladder:

emit_signal("on_ladder", self)

And then

func _on_Ladder_on_ladder(ladders) -> void:
    self.global_position.x = ladders.ladder_position.x

More info: Using signals — Godot Engine (stable) documentation in English

Hi,

Thanks for the info, that was just what I was looking for.

1 step nearer :wink:

JayH | 2022-07-13 10:01