problems with run away button and transitions

Godot Version

Godot 4.6

Question

I trying to make a battle transition where when the player enters the Area2D node, it transtions into a the battle scene, and when the player clicks the run away button, there is an 85% chance the play can run away. I managed to get the transitions to work, but when I transition into the battle the player can still move around in the world. I tried to add this line of code ‘get_tree().paused = true’ but this would stop the enemy spawning in and the buttons stopped working. For my run away button I initially had it change the scene to the main game scene, but this would reset the player position and world. Is there a way to stop the player from moving, and then to allow it to move again from the same position in the world. If you need anything more just ask.

#My Battle transtion code#
extend Area2D
var battle = preload("res://Battle_scene/Battle.scn")
var battle_ui_scene = preload("res://battle_Animation_ui.tscn")
var Player = preload("res://player.tscn")

func _on_body_entered(body):
	if body.is_in_group("Player"):
		var BattleUI = battle_ui_scene.instantiate()
		get_tree().current_scene.add_child(BattleUI)
		
		var animation = BattleUI.get_node("AnimationPlayer")
		
		animation.play("TransitionIn")
		await get_tree().create_timer(1.5).timeout
		
		var battleTemporary = battle.instantiate()
		get_tree().current_scene.add_child(battleTemporary)
		animation.play("TransitionOut")
		#get_tree().paused = true #THIS STOPPED THE ENEMY FROM SPAWNING AND PREVENTS BUTTON FROM WORKING#
		queue_free()
		
		

#My BattleUI code just for run button#
extends Control
var Inventory = preload("res://player_inventory.tscn")
var battle_ui_scene = preload("res://battle_Animation_ui.tscn")
var game_world = preload("res://game_world.scn")
var battle = preload("res://Battle_scene/Battle.scn")

func _on_run_button_pressed() -> void:
	var fleeProb = randi_range(0,100)
	if fleeProb <= 85:
		var battle_ui_instance = battle_ui_scene.instantiate()
		get_tree().current_scene.add_child(battle_ui_instance)
		var animation = battle_ui_instance.get_node("AnimationPlayer")
		animation.play("TransitionOut")
		await get_tree().create_timer(1.5).timeout
		get_tree().paused = false
		get_tree().change_scene_to_file("res://main_game.tscn") #THIS RESETS THE WORLD#
		battle_ui_instance.queue_free()

Just add this to _on_body_entered():

body.set_physics_process(false)

Then when you want the player to move again, set it to true.

BTW, you do not need to detect if body is in group “Player” if you put the player on its own layer and only have the Area2D mask set to that same layer.

Thank you. How would I change the scene from the Battle Scene to the Main Game scene, without resetting player position like i did in my code?

TBH, you have a lot of weird, non-standard things going on in your code. Why are some of your scenes binary files with the .scn extension? That’s something that should only be used on export (which happens automatically to .tscn files).

I can’t tell you why your player is having that problem, but my guess is because in your scenes you are re-creating it. Instead of doing that, pass it as an argument and reparent it to the existing scene.

Thank you this worked. The reason i had the .scn extension was because Godot was giving me a warning when saving saying the file was too large and to change the scene to a .scn

1 Like