Switching scenes and spawing at correct position

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

Hi there,

I am currently trying to make a 2D RPG pixel game. I am an artist trying to learn programming. Im getting there i think :")

I have made a scene changer.
Once the player enters the area, a signal is sent, that body has entered and it transitions to the scene i have assigned.
My Scene_changer code below:
.

extends Area2D

export (String, FILE, “*.tscn”) var scene_target = “”

func _ready():

$Sprite.visible = true
pass

func _on_scene_changer_body_entered( body ):

get_tree().change_scene(scene_target)
get_parent().queue_free()

.

That scene that i have assigned, has its own scene changers and each one goes to a different scene.
.
.
You can equate this to a player being in a city, then exiting a city that goes on a main map, that has different cities to choose from and to go to.

OR

Entering and exiting a house.

example from the game Shovel Knight:

.
.
.
.
The problem that i cant find the answer to is:

In every city that i have, there are 4 exits, North, South, East and West.
When the character exits through the North side , to the main map, the character’s position, is not on top of that main map city point that it came out of. And the same thing can be said for when it returns to the city it came out from, it does not enter from the North side. Which is understandable as it is not coded in.

How would one go and make it so that it enters through the exit and vise versa and not at a random point.

.
.

Ive been on this problem for the last 2 weeks, ive search online alot and cant seem to find an answer.

Thank you in advance for helping me.

:bust_in_silhouette: Reply From: Diet Estus

Without knowing the exact details of your project, I can only offer the following technique.

You can have a Singleton scene (also called AutoLoad) that keeps track of which city the player is in and which exit he uses. A singleton is a global node that is always present in your project. You can use it to store information that you want to survive from scene to scene.

You can also have a dictionary there that has exit names as keys and apppropriate destination positions as values.

When you call your code to change the level/scene, you can use the current exit and the dictionary to position the player on the new map at the correct position.

Thank you very much for the reply :).

That sounds like a good idea, I will look into this Singleton scene Autoload thing.
I think this might be my only option.

I am going to leave this question for a couple of days to see if there are more ways to solve this one problem.

Again thank you very much.

marios | 2018-04-07 17:41

No problem. I use variants of this method in a lot of my projects. It works quite well.

Diet Estus | 2018-04-07 21:36