Getting the position of an instanced scene after moving it

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

Hi,

I’m making a 2D platformer with ladders. As I begin to build the ladder mechanic i’m running into a few issues.

The Ladder’s root node is an Area2D, with a Sprite and CollisionShape2D. The scene is autoloaded to make it accessable to other scripts. The CollisionShape2D is set to ‘Make unique’ allowing me to scale the ladder’s sprite with it’s collision shape.

I’m centering the Player on the ladder when he collides with the Ladder.

Getting the reference for the location of the Ladder in the Ladder’s script:

var ladder_position = Vector2()

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

In the Player’s script I use the position of the Ladder and apply it to the Player:

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

I’ll need to move the instanced Ladder’s to the relevant location on my level map. But the problem is that the location the Player receives is the one before the Ladder is moved. I don’t know how to get the current position of the Ladder.

Currently when the Player collides with the ladder, it is set to the top left corner of the screen (0,0).

What should I do?

after the ladder moves. Get the position of the ladder.


Here you have taken the first position of the ladder on the scene.

var ladder_position = Vector2()
func _ready() -> void:
     adder_position.x = self.global_position.x

ramazan | 2022-07-11 09:33

Hi,

The ladder’s are stationary, they don’t move. What I meant was there are multiple ladders around the main level scene. some ladders longer, some shorter :wink:

But when the Player enters the any of the Ladder’s Area2D collision and the signal is sent to the Player’s script, the position from the Ladder is the original position before I placed it around the main scene, i.e (0,0).

Only the position of the Ladder where it was originally created is available with my script.

How do I get the Ladder’s current position after I’ve placed it?

JayH | 2022-07-11 09:44

sorry i misunderstood. I use google translation

Note: Area, Area collision works but
Area , Body skirmish would be better.

suggestion :
Use Staticbody on stationary objects.

example:
When the Staticbody (“Ladder”) enters “Player Area” in

Playar Script (signal Area2d )

func _on_Area2D_body_entered(body):
       if body.name == "Ladder" :   // ladder = Statickbody name
              print(body.global_position.x)
  pass

ramazan | 2022-07-11 12:29

:bust_in_silhouette: Reply From: ReudsFratt92

Connect a signal and set collision on a collision layer with player (watch tutorial if need), when player enters the collision shape. Then I would use an autoloaded global script, be it named GlobalScript with abool on_ladder and Vector2 ladderPos.

func _on_collisionShape_entered():  # or it would be something like that
    GlobalScript.on_ladder = true
    GlobalScript.ladder_pos = self.global_position

then check with the player’s script status of these variables. This approach also opens you to easier stats monitoring during the whole game, as the global script would travel along through the scenes afaik.

Sounds good to have a Global Script - I was thinking of implementing one, but I don’t see how it can get the current location of the Ladder compared to what I have.

Someone in another forum said that perhaps I need to update the Ladders position, but I’ve no idea how to do so, or whether that’s the answer.

It feels like I’m missing something with how Godot works. I still don’t know for sure why I’m not getting the current location of the Ladders and only the orignal position when the Ladder scene was created. It’s like I’m missing a step/method somewhere.

JayH | 2022-07-11 12:12

Because you might be simply getting it’s position before you move it. If you want to use _ready() so badly, set the position of your ladder before adding it as a child. _ready(): executes in the first frame, rigth before appearing in the scenetree.

ReudsFratt92 | 2022-07-11 12:26

I don’t wish to use _ready() badly, it’s just the way I know so far.

The below screenshot is the code for the Ladder:

And the Main Scene Level:

JayH | 2022-07-11 12:58

From Screenshots I see that ladder’s position indeed is 0,0

ReudsFratt92 | 2022-07-11 13:13

The Main Root Level Node is selected (Level_01), not the Ladder node :wink:

Here’s it with the Ladder Node selected:

JayH | 2022-07-11 13:42

try parsing self through the signal as a variable. Then, in player’s script, get the position value of the parsed variable, or parse the position itself as Vector2.

ReudsFratt92 | 2022-07-11 21:50

The issue was autoloading the Ladder scene. Although now that is sorted I have issues with accessing multiple ladders, so I’ve created a new post on that subject.

JayH | 2022-07-12 15:23