How would I save the position in a RPG

Godot Version 4.2.1

Question

So I am currently making an RPG in Godot and lets say we have a starting room that has a bed and other stuff. The scene starts by the player laying on the bed and you have to move yourself out. Now, lets say if you keep going down it switches to another scene that is a hallway. But if they for some reason had to go back in the room how would I make it so the player starts at the bottom so it can look like he came from there?

Maybe you should use in the room scene a position2d node, it will store the position of the entrance. Then when you move to this room/scene, you set the player position = to the position2d node position. There lots of ways of doing this.

You can store the position in an autoload singleton. Then it would be accessible from anywhere, even if the rooms in your game are handled in different scenes.

If there were to be multiple entrances to a room, I would I work with that?

How would I do that?

There are many ways to approach this problem, but you could try something simple like this:

  • Make an autoloaded script that handles scene transitions. Let’s call the script Global.
  • Make a list of possible spawn points for each scene. For example, you could add a Node2D and name it SpawnPoints to every scene. Then add Marker2D nodes as children of the SpawnPoints node. Make sure the root node of the scene has a script with a reference to SpawnPoints.
@onready var spawn_points: Node2D = $SpawnPoints
  • Make a door scene and export variables for the destination. This could be the path of the target scene and the index of the spawn point.
@export var target_scene := ""
@export var spawn_point := 0
  • Connect a signal from the door to Global with the exported variables as parameters.
signal door_used

func _ready():
    door_used.connect(Global.change_scene, CONNECT_DEFERRED)

func door_activated():
    door_used.emit(target_scene, spawn_point)
  • In the Global script, change the scene and spawn player to a spawn point with the given index.
func change_scene(scene_path, spawn_point):
    get_tree().current_scene.free()
    var packed_scene = load(scene_path)
    var new_scene = packed_scene.instantiate()
    get_tree().get_root().add_child(new_scene)
    get_tree().current_scene = new_scene

    var player = PLAYER_SCENE.instantiate()
    current_scene.add_child(player)
    player.position = new_scene.spawn_points.get_child(spawn_point).position