Yes $Timer1.text
okay now there is no error but still dont save my high score even after putting in game.gd.
extends Node2D
const SPAWN_INTERVAL = 0.4 # Adjust spawn interval as needed
var spawn_timer = 0.0
var can_spawn_bullets = true
onready var deathsound = $Pop
var time_alive: float = 0.0
var running: bool = true
var highscore = 0
func _ready() -> void:
deathsound.connect("finished", self,"audio_finished")
highscore = load_highscore()
$Highscore.text = str(highscore)
$Player.connect("death_signal", self, "_on_Player_death")
$Timer1.text = str(int(time_alive))
# Start processing every frame
set_process(true)
func _process(delta):
spawn_timer += delta
if spawn_timer >= SPAWN_INTERVAL:
spawn_bullet()
spawn_timer = 0.0
if running:
# Increment the time_alive by the time passed since the last frame
time_alive += delta
# Update the Label's text to show the elapsed time in seconds
$Timer1.text = str(int(time_alive))
# Check if high score needs to be updated
if time_alive > highscore:
$Highscore.text = str(int(time_alive))
signal fade_out
func spawn_bullet():
if not can_spawn_bullets:
return
var bullet_scene = preload("res://Scenes/Bullet.tscn")
var bullet_instance = bullet_scene.instance()
var viewport_width = get_viewport().size.x
bullet_instance.position.x = rand_range(0, viewport_width)
connect("fade_out", bullet_instance, "fade_out")
get_tree().root.add_child(bullet_instance)
func _on_Player_death():
# Get the current score from Timer1 label
var current_score = $Timer1.text.to_int()
if current_score > highscore:
# Update the high score if the current score is higher
highscore = current_score
save_highscore(highscore)
$Highscore.text = str(highscore)
func playerDied(pop_effect_instance) -> void:
print("Player died")
if not deathsound.playing:
deathsound.play()
can_spawn_bullets = false # Stop spawning bullets
add_child(pop_effect_instance)
emit_signal("fade_out")
$AnimationPlayer.play_fade_out()
yield(get_tree().create_timer(3), "timeout")
get_tree().change_scene("res://Scenes/Menu.tscn")
func audio_finished() -> void:
print("Audio finished")
func save_highscore(highscore: float) -> void:
var file = File.new()
var err = file.open("user://savefile.dat", File.WRITE)
if err == OK:
file.store_float(highscore)
file.close()
else:
push_warning(err)
func load_highscore(default_value: float = 0.0) -> float:
var file = File.new()
var err = file.open("user://savefile.dat", File.READ)
if err == OK:
return file.get_float()
else:
# Could not open, might not exist
return default_value;
func stop_timer():
running = false
and i deleted all my Timer1 gd script and dont changed my highscore script
extends Label
func _physics_process(delta):
var timer_text = get_node("../Timer1").text
self.text = "High Score: " + timer_text
okay yes the problem was my high score script but now it saves but not correctly and i dont now how to fix it
@gertkeno as you can see in 2 screenshot my score was 5 and it must be 5 but when i start it was 4.
This seems like the issue, it’s always replacing the highscore with the timer text. Is there any reason to do that since the game script will edit the highscore label at the correct time?
Thanks for replying but i already make it the problem was in the signals so i added them but of course without your saving mechanism it wouldn’t work,thanks!
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.