Hi! I am trying to find out how to make universal Game Over screen button to return to previous screen that played and to restart it.

Currently I have function on my enemy that after collision with player, signals out the ouch method in player.

Enemy on_body_entered signal code:

func _on_sides_checker_body_entered(body):
	body.ouch(position.x)
	$SoundHurt.play()

And on player connected code:

func ouch(var enemyposx):
	set_modulate(Color(1,0.3,0.3,0.5))
	velocity.y = JUMPFORCE * 0.4
	
	if position.x < enemyposx:
		velocity.x = -400
	elif position.x > enemyposx:
		velocity.x = 400
		
	Input.action_release("left")
	Input.action_release("right")
	
	$Timer.start()

Which also starts timer that changes to Game Over screen, like this:

func _on_Timer_timeout():
	get_tree().change_scene("res://GameOver.tscn")

And than on Game Over screen there are two buttons, and this is the complete code for each button:

Main Menu button:

extends Button

func _on_MainMenuButton_pressed():
	get_tree().change_scene("res://TitleMenu.tscn")

& Retry level button:

extends Button

func _physics_process(delta):
	if Input.is_action_just_pressed("jump"):
		get_tree().change_scene("res://Level1.tscn")

func _on_Retry_pressed():
	get_tree().change_scene("res://Level1.tscn")

So my problem is that this way I'll need to create separate Game Over screen for each level (and there are 21 levels in the game) and I'm trying to find the way with one Game Over screen that will on Retry button pressed, always return to the previously played level, not just to the first one.

In case you need more information for answer, feel free to ask and thank you so much in advance for your answers.

Real simple/ugly method, store the level number in a singleton (let's call it globalvars for example), then change your calls to something like:


func _on_MainMenuButton_pressed():
    globalvars.levelnumber = 1
     get_tree().change_scene("res://TitleMenu.tscn")

and

func _on_Retry_pressed():
    get_tree().change_scene("res://Level"+str(globalvars.levelnumber)".tscn")

and whenever the player completes a level, before scene changing call

globalvars.levelnumber +=1   

Hi Bimbam and thank you so much for your answer - it works! :)

I only needed to put a plus sign on that line, so that it writes: get_tree().change_scene("res://Level"+str(globalvars.levelnumber)+".tscn")

and did everything else as you said and it works! Thank you so much!

2 years later