How to get and store player location at any given time.

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

I need to get and store the location of my player (Kinematicbody2d). This is so I can place the character correctly outside of a door he has exited in a town with multiple buildings. Any help would be greatly appreciated.

:bust_in_silhouette: Reply From: kidscancode

A 2D node’s position is accessed via the position property. T

To store the current location in a variable to access later:

var saved_location = position

To place the player at a certain location, set the property via:

position = Vector2(x, y)

Thanks a ton, that helps a lot!

LucinaMan | 2018-07-21 22:03

Here’s a follow up question, currently the trigger to remember the position is when the sprite collides with an area2D. How can I get a reference to the sprite in the area2D’s code?

LucinaMan | 2018-07-21 22:45

I’m assuming that by “the sprite”, you mean the KinematicBody2D player.

Use the Area2D’s body_entered() signal, which includes a reference to the body that overlapped the area.

func _on_Area2D_body_entered(body):
    # body is the colliding object

kidscancode | 2018-07-21 22:53