Setting player position from different exits

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

Hello, I’m new to GoDot and I’m making my first 2D rpg game. I set up a scene change script that runs when you collide with 2D collision bodies set on map edges and doors. At this point, I have a player instance placed on every map so every time you change locations the player is in the spot I set in the editor.

I want to switch to loading the player in a global scene that is autoloaded, and have the transition collisions decide the coordinates where the player will be placed upon changing scenes. What would be a simple way of making this happen?

p.s. I am a noob programmer, so any reference to good tutorials is appreciated.

:bust_in_silhouette: Reply From: jgodfrey

One idea. In each scene, place a spawn point where the player should start. That could be easily represented by dropping a Position2D node in each scene at the appropriate location.

Then, when you load the new scene, simply create an instance of your Player scene and position it at the location of the Postion2D.

I could see that working fairly easy, thank you. Do you know how I could place the player as a child of my ysort node in each scene?

des8989 | 2020-09-01 22:28

Really, it depends on how your scenes are organized. The only trick will be in finding the YSort node in each scene. Is the that node at a known level in the hierarchy in each scene?

Assuming you can get a reference to the YSort node and your spawn point (and, I’m sure that can be done), it’s really just something like:

player.position = $Position2D.position
$YSort.add_child(player)

jgodfrey | 2020-09-01 22:51

If the nodes can’t easily be found via standard get_node() calls, you could always put them in groups as necessary (say, a ysort group and a spawn group). Then, you could easily find them via the SceneTree’s get_nodes_in_group() function.

jgodfrey | 2020-09-01 23:01