I am trying to come up with a nice system to easily add new scene transitions, and make sure that the player is located in the right position when the scene has loaded.
This is how far i have come with a demo:
Think of 2 entrances, which will teleport you to 2 different scenes:
https://imgur.com/a/pz4kb
Currently i tried to create 2x area2d nodes, with a collision area. One named TeleportSource, and one named TeleportDestination.
I create an instance of TeleportSource and add it in front of the entrance. I then create an instance on the new Scene i am going to, and create an instance of TeleportDestination.
Gdscript for Source:
extends Area2D
export (PackedScene) var SceneDestination
func _on_TeleportSource_body_entered( body ):
print("entered")
get_tree().change_scene(SceneDestination.resource_path)
Then on Destination i have:
extends Area2D
export (PackedScene) onready var PlayerInstance
export (NodePath) onready var TeleportDestination
func _ready():
var Player = PlayerInstance.instance()
add_child(Player)
Player.position = get_node(TeleportDestination).position
print ("Teleporter is: ", get_node(TeleportDestination).position)
print ("Player is at: ", Player.position)
So now i can freely enter the first room, and go back out again without any issues.
The problem exists when i have 2 entrances like on the picture, as there is no way for the engine to know which room i am coming from.
The question is then:
Is my approach totally wrong?
Or should i create a variable on each scene, that is then sent to a singleton, to let my script know where i came from?
Any other approaches?