How to store my current position from the Tilemap?

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


I’d like to store in my GlobalP autoloaded script the transform.position of the Tilemap in my main scene, so that i could get back to it exiting some of my other scenes.

Tried that:

onready var player_position = get_node("/root/Node2D/Stage/TileMap").transform.position

Without success…

This code doesn’t work because autoloads are created and ready before the first scene is added to the tree (which might not even contain your tilemap, if you have a main menu before the game for example).

You need to store the position only after the tilemap has been added.

Zylann | 2020-02-20 14:02

Thxs, but how to add the tilemap before?

Syl | 2020-02-20 15:09

If you want to get the node from your singleton, you can’t. Singletons always get created before anything else.
You may want to access your singleton from the scene containing the tilemap instead.

Zylann | 2020-02-20 19:03

You mean, having my var player_position in the singleton, and changing it from the tilemap scene?
Tried with that in my Global singleton:

var player_position = null

And that in my TileMap scene:

func _ready():
	var player_position = $TileMap.transform.position

But i got this error: Invalid get index ‘position’ (on base: ‘Transform2D’)…

Syl | 2020-02-20 19:41

It’s not position, use origin

Zylann | 2020-02-20 19:44

Thxs, but be it with origin or position, my player_position isn’t saved on my global script…

Syl | 2020-02-21 12:43