Hi, I would like to know how I can make a script that loads another scene and then puts you to the correct position. I have this script that loads to another scene on body overlap. Here is the script:

extends Area2D

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

func _physics_process(delta):
	var bodies = get_overlapping_bodies()
	print (bodies)
	for body in bodies:
		if body.name == "Player":
			get_tree().change_scene(next_world)

I would like to do the same as this: export(String, FILE, "*.tscn") var next_world but then with the position. I am new to Godot/gdscripts so please don't talk in difficult terms plz. Thanks. -Davey

The problem you are going to encounter is after you call get_tree().change_scene, you do not have a good way to get a reference to the player node, not in the script that changes the scene at least. Without the ability to reference the player, and potentially the position you want to move the player to, it will be difficult to set the player's position from the Area2D node.

What I would suggest is adding the following code to your player script:

export (NodePath) var path_to_starting_position = null
var _starting_position
func _ready():
	if (path_to_starting_position != null):
		print ("Player Script: Placing player at starting position set in editor...")
		_starting_position = get_node(path_to_starting_position)
		global_position = _starting_position.global_position
	else:
		print ("Player Script: No starting position set...")

Then setting the position you want the player to start at in the inspector for each scene.

If you want the player to start at a different position based on which entrance/exit used, then the issues becomes a bit more difficult and there are multiple different ways to go about it. One way you could achieve this is to use a singleton to store the name of the desired destination in the newly loaded scene, and then have the player script retrieve the position from a list defined somewhere in the scene itself.

Hopefully this helps! :smile: (Side note: Welcome to the forums!)

I made a similar thing, so I added destination coordinates:

export(String, FILE, "*.tscn") var room_name
export(Vector2) var coordinates = Vector2(100, 100) 

and when player hits the door, this happens:

Global.goto_scene(room_name, coordinates)

so instead of using change_scene, I have custom "goto_scene" function.

I'm using Global.tscn as my base scene for the game. it has Player character, and some ui elements.. this is autoloaded by Godot, and is always there no matter what level is loaded. so it let's me keep all stats, inventories etc with Player when switching levels. (https://docs.godotengine.org/en/3.1/getting_started/step_by_step/singletons_autoload.html)

so, the level loading is implemented in Global.tscn's script file. it's basicly just this example for documentation: https://docs.godotengine.org/en/3.1/tutorials/io/background_loading.html#example

but I added destination variable, and then changed goto_scene and set_new_scene functions to include the coordinates... so changes look a bit like this:


var destination = Vector2(0, 0)
      
func goto_scene(path, coordinates): 
    destination = coordinates  # take down the coordinates for later
    loader = ResourceLoader.load_interactive(path)
    [... rest of the function like in example ...]
    

func set_new_scene(scene_resource):
    set_process(false)
    current_scene = scene_resource.instance()
    get_node("/root").add_child(current_scene)
    get_node("Player").global_position = destination   # move player to new position

it might look much trouble for simple thing, but once you are using this type of loading, it let's you do all sort of nice things.. like loading screen, or like I have forexample, different fade in/fade outs or other level switching transitions..

.b

I'm pretty new to Godot and as you said this might look much trouble for a simple thing, It does. I can't really understand the tutorials on the Godot websites because there unclear for me and my English. Can you maybe explain it in an easier way if there is an easier way. Thanks :) -Davey

3 years later