0 votes

Hey All,

Right now I use an Area2D node with the following code to allow me to switch between scenes whenever the player collides with the Area2D node


extends Area2D

export(String, FILE, "*.tscn") var next_world_scene

func physicsprocess(delta):
var bodies = get


for body in bodies:
    if body.name == "Player":

This all works great but the big issue I have is that for my hub world, there are multiple scenes to transition to so when you go about different scenes, you don't leave the same way you came out of.

Basically imagine playing Link To The Past, but every time you left a dungeon to go to Hyrule Field, you always spawned at the center of the map rather than by the exit you just came out of.

I'm going to assume AutoLoads is going to be my best bet but not sure the best way to go about this. Any help would be sincerely appreciated.

Take care,

in Engine by (14 points)

1 Answer

+1 vote

Right, you can use an autoload for this. On each Area2D you can store the position that you want the player to appear at in the next scene. When the player enters the Area2D, send that data to the autoload. Then, after the scene change, you can set the player's position using that data.

by (6,296 points)

Appreciated it exulun,

So when my player collides with the Area2D, it emits the "level_complete" signal so what should I have in my autoload to store? My brain is pretty jumbled trying to think of a smart solution but right now I'm thinking have in the autoload code something like this

var destination

func ready():
destination = get

Again, can't stress enough that I've been over thinking this but once this is setup that will be a huge step in quality for my game. Would love to hear your thoughts.

Thank you!

Well, you could just access the autoload directly and set the destination variable when the player hits the Area2D. Like,

Global.destination = Vector2(some position here)

So on my autoload script would I have all the different Vector2 positions stored in the autoload? Trying to visualize how I should go about this. Appreciate your help.

Not necessarily. You could store only the current Vector2 position that you want the player to appear at in the next scene and change that whenever the scene is changed.

So my exit script would have this


extends Area2D

func physicsprocess(delta):
var bodies = get
for body in bodies:
if body.name == "Player" or "GolfCartPlayer" or "CYOAPlayer" or "PlayerAutoRun" or "PlayerSkateBoard" or "PogostickPlayer" or "Submarine":
Global.destination = Vector2(25, 50)


Is Global the name of my autoload? How would I transfer that data over to the autoload?

Well first off, there's no need to check for overlapping bodies every frame. Just check whenever a new bodies enters using signals.

Second, that if statement doesn't work. You will need to write body.name == "string" for every string you want to compare it to. Otherwise it will only convert the string itself to a bool, and every non-empty string evaluates to true, so that if statement will always evaluate to true. Consider putting all the names into an array and using the in keyword, using collision layers, or groups.

Third, yes Global is the name of your autoload. Since you can access those directly you've already transferred the data over by setting the destination variable.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.