Can't go back to saved scene more than once

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

I am trying to make a combat system similar to Pokemon. When you get into the Area2D that triggers fights by random chance succeeds. Problem was that whenever I got out of the battle player went back to the first state of the level not where it was before the battle began. So I managed to code going back to where you were before the battle BUT if when I get out of the battle and wait for another it just gives blackscreen instead of the battle scene when the random battle triggers again.
Here are the codes please help I been trying to fix this for more than 3 days.

Global.gd code:

extends Node
var saved_scene
func switch_scene(temp_scene):
	# save scene and remove it from the tree
	saved_scene = get_tree().get_current_scene()
	get_tree().get_root().remove_child(saved_scene)
	# instance and add temporary scene as current scene
	var new_scene = load(temp_scene).instance()
	get_tree().get_root().add_child(new_scene)
	get_tree().set_current_scene(new_scene)

func load_scene():
	if saved_scene != null:
		get_tree().get_current_scene().queue_free()
		get_tree().get_root().add_child(saved_scene) 

Player code about this problem:

onready var BattlePlace = get_node ("/root/World/BattlePlace")
onready var player = get_node("/root/World/Player")
var TimeRestart = false
const JUMPHEIGHT = -300
onready var glob = get_node("/root/Global")
onready var diyalog = get_node("/root/World/Dialogue")
func _physics_process(delta):
	var inbody = BattlePlace.overlaps_body(player)
	if inbody == true && TimeRestart == false:
		$BattleChanceTimer.start()
		for i in range(3):
			randomize()
			var guess1 = int(rand_range(1,1000))
			randomize()
			var guess2 =  int(rand_range(1,1000))
			if guess1 == guess2:
				glob.switch_scene("res://BattleArena.tscn")
			else:
				pass

code for Area2D that triggers the battles:

onready var glob = get_node("/root/Global")
# Declare member variables here. Examples:
# var a = 2
# var b = "text"


# Called when the node enters the scene tree for the first time.
func _ready():
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
#func _process(delta):
#	pass


func _on_ButtonRun_pressed():
	var esc1 = int(rand_range(1,4))
	randomize()
	var esc2 = int(rand_range(1,4))
	if esc1 == esc2:
		glob.load_scene()
	else:
		pass

I have a feeling this problem might be because it opens the same arena and not a duplicate of it which it should. But every attempt I tried failed miserably.

xxxtlgxxx | 2020-04-12 12:19

Also I believe the engine doesn’t crash but just shows black screen instead of the arena

xxxtlgxxx | 2020-04-12 12:19

:bust_in_silhouette: Reply From: xxxtlgxxx

Alright, I managed to fix it. Problem was camera. It was in a diffrent place when it instanced again.

hi im also experiencing similar problems, how did you manage fix the camera issue?

nucifere | 2020-04-20 15:05

I didn’t have a camera attached to the player in the second scene(temporary battle scene) So I attached a camera to player and set it to current. And it fixed the issue. But now I have much bigger issues and I think I will need to find a diffrent way of this temporary scene change thing because I run into errors whatever i do. PackedScene might be a much better method.

xxxtlgxxx | 2020-04-20 15:18