How to store on my singleton my position in TileMap?

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

Greets!

Got this (var player_position = null) on my global script, and this:

func _ready():
    	GlobalP.player_position = $TileMap.transform.origin

on a high node of my TileMap.

I want to get the position when returning to this TileMap from a clicked area2D, but of course that doesn’t work, i just come back to the starting point of the TileMap scene…

:bust_in_silhouette: Reply From: Bernard Cloutier

If I understand correctly, in one scene you have a player somewhere on your tilemap. Then you want to switch to another scene, and when you come back you would like your player to be in the same place he was, right?

The tilemap’s origin is always going to be the same. Why aren’t you storing the player’s position instead? Like this: GlobalP.player_position = $Player.position (I’m assuming your node is called Player and is at the same level as Tilemap. Since you haven’t shared your scene tree, I cannot know for sure that the $Player path will be correct).

You got it right, but i’m still returning to the starting point.

I’ve stored the position your way, but now i need it to position my player according to it when entering the TileMap scene.

Syl | 2020-02-24 19:04

How about this:

in your player script

func _ready():
    if GlobalP.player_position not null:
        self.position = GlobalP.player_position

Just remember, setting GlobalP.player_position only copies the current value of your player position. If you change the player’s position, the value in GlobalP.player_position will not change. So I don’t think you should set GlobalP.player_position in a _ready function, you should set it just before you switch scene.

Bernard Cloutier | 2020-02-24 19:08

Ok, tried to put those lines in my player script, but strangely, got this error:

‘:’ expected at end of line.

When ‘:’ is obviously at the end of the line…

Syl | 2020-02-24 19:21

My bad, this is the correct version:

func _ready():
    if GlobalP.player_position != null:
        self.position = GlobalP.player_position

Bernard Cloutier | 2020-02-24 19:24

Here we are! Cheeeeeeeeeeers! :slight_smile:

Syl | 2020-02-24 19:27