Topic was automatically imported from the old Question2Answer platform.
Asked By
Superkrypet
Im making an RPG with two houses in the same scene and i want to be able to enter them easily. But how do i make so that my player comes out of the right house when he lefts that scene and comes out of the left house when he lefts that scene because right now i can only place one starting position for my player
Create a Singleton, store the players position in the world before entering a house as a variable in that Singleton and load it when leaving the house again.
Here’s a rough sketch:
Global.gd (that’s the singleton)
extends Node
var last_world_position
EntryDoor.gd
extends Area2D
var house_scene_path = "res://<PathToYourHouseScene>.tscn"
func on_Area2D_body_entered(body):
if body.is_in_group("Player Characters"):
Global.last_world_position = body.global_position
get_tree().change_scene(house_scene_path)
ExitDoor.gd
extends Area2D
var world_scene_path = "res://<PathToYourWorldScene>.tscn"
func on_Area2D_body_entered(body):
if body.is_in_group("Player Characters"):
get_tree().change_scene(world_scene_path)
World.gd
func _ready():
if Global.last_world_position:
get_node("Player").global_position = Global.last_world_position
This is briliant! thanks a lot man. But just so that im sure i understand what is it that the Globol.gd cript is going to be attached to
Superkrypet | 2020-05-29 12:55
The Global.gd script is attached to a Node (that’s why it says extends Node in the beginning). You don’t have to create a scene for that though, Godot will do that on its own. Just add it as a AutoLoad (check out the link I provided above).
When you run the game, that node will be added as a child of the root-node of the scene tree (as opposed to the root-node of the current scene), so when we later switch the current scene it won’t be deleted and can store any information we want to keep. Other than that it’s a normal node in the scene tree. As it is a direct child of root of the scene tree it is easy to access it from anywhere using it’s name.