How do make my player spawn back to the same location it was in before the battle

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

I’m making a Pokemon type game and was wondering that after my battle, how do I make my character spawn back to the location of the encounter instead of the main location of the scene which is what happens when I use the change scene function

what part of this are you not sure how to do? Is it saving the position, or making the player spawn at that position or what? What do you have so far?

Millard | 2020-11-20 19:09

:bust_in_silhouette: Reply From: EmanuelOzorio

I’ve tryed to make a pokemon-like some time ago and implemented like this:

I created a scritp called Global and put on autoload.
here is the script:

extends Node

var root
var world_scene

func _ready():
	root = get_tree().get_root()

func change_to_battle(path: String, enemy: Enemie):
	var new_scene_res = load(path)
	var new_scene = new_scene_res.instance()
	new_scene.set_enemy(enemy)
	world_scene = get_tree().get_current_scene()
	root.add_child(new_scene)
	get_tree().set_current_scene(new_scene)
	root.remove_child(world_scene)

func back_to_world():
	world_scene.back_to_world()
	root.remove_child(get_tree().get_current_scene())
	root.add_child(world_scene)
	get_tree().set_current_scene(world_scene)

The back_to_world is just a method that I used to do some things when the player comes back like remove the enemy of the scene.
The arg path contais the path to the battle scene and the arg enemy contain the data of the enemy for battle.

Whent the player collides on enemy just call Global.change_to_battle(...)

P.S.: That code makes the scene of level continue in the memory and consuming memory. For sure that is not the best way, but is the easyest way that i found when i coded the project…