Ahmed1
April 15, 2025, 3:55pm
1
Godot Version
Godot v4,3
Question
Hi!
What I want is, when I get the game over scene and press play again, I want the player to restore his health to default.
Right now, when I press the play again button.
I can see the player in-game for a (second)
Then immediately return to the game over scene again!
Because the player has no health left
What should I do?
I get the game over scene right after the player lost all the hp
player script
const SPEED = 400
const JUMP_VELOCITY = -750.0
func HP():
$ProgressBar.value = GlobalPlayer.HP
if GlobalPlayer.HP ==0:
get_tree().change_scene_to_file("res://GameOver.tscn")
func _physics_process(delta: float) -> void:
HP()
play button script
extends Button
func _pressed():
get_tree().change_scene_to_file("res://main.tscn")
There are many ways to accomplish what you’re after, with one simple way being to reset the GlobalPlayer object’s HP to the default in your _pressed()
function immediately before changing the scene to main.tscn.
2 Likes
Ahmed1
April 15, 2025, 4:21pm
3
I just tried this now but the problem still the same
extends Button
var HP = 31
var default_HP := 31
var current_HP := default_HP
func _pressed():
get_tree().change_scene_to_file("res://main.tscn")
func _ready():
reset_HP()
func reset_HP():
current_HP = default_HP
_ready()
only gets called when the object gets added to the scene; once it’s in the scene, _ready()
isn’t called again.
As @soapspangledgames says, I’d suggest:
func _pressed():
get_tree().change_scene_to_file("res://main.tscn")
current_HP = default_HP
In your code, why do you have both HP
and current_HP
?
You might also consider making default_HP
a const
.
1 Like
Ahmed1
April 15, 2025, 5:15pm
5
like this?
extends Button
const default_HP := 31
var current_HP = default_HP
func _pressed():
get_tree().change_scene_to_file("res://main.tscn")
current_HP = default_HP
func reset_HP():
current_HP = default_HP
1 Like
Ahmed1
April 15, 2025, 5:42pm
6
didn’t work problem still the same
You’ve made a new var current_HP
, it is not related whatsoever to your global HP
Use GlobalPlayer.HP = default_HP
2 Likes