Godot Version
Godot 4.3
Question
My game increases the score by 1 each time the player manages to catch 1 falling object. It’s also supposed to display the current score and the high score in the death scene. After some work, the scores are displaying just not correctly.
My issue is that each time my game restarts, and the player starts catching falling objects, my previous high score is being overwritten by the current score despite the current score not being higher than the high score.
Example:
Game 1 score = 6
high score = 6
Game 2 If the player catches nothing.
score = 0
high score = 6
Game 3 If the player catches 1 falling object.
score = 1
high score = 1
It seems like my highscore function is struggling to save the previous high score ? I suspect there’s some logical issue in my code that I’m missing.
score.gd
var score = 0
var highscore = 0
@onready var score_label = $Score
func add_point():
score += 1
score_label.text = "Score:" + str(score)
save_score()
func add_highscore():
highscore += 1
save_highscore()
update_highscore()
func save_score():
var file = FileAccess.open("user://score.save", FileAccess.WRITE)
file.store_32(score)
file.close()
func save_highscore():
var highscore_file = FileAccess.open("user://high_score.save", FileAccess.WRITE)
highscore_file.store_32(highscore)
highscore_file.close()
func update_highscore():
if score > highscore:
highscore = score
save_highscore()
death_scene.gd:
extends Control
@onready var death_score = $Score2
@onready var high_score = $HighScore2
func _ready():
loading_score()
loading_highscore()
# Called when the node enters the scene tree for the first time.
func loading_score():
if FileAccess.file_exists("user://score.save") :
var file = FileAccess.open("user://score.save",FileAccess.READ)
var score = file.get_32()
file.close()
death_score.text = str(score)
func loading_highscore():
if FileAccess.file_exists("user://high_score.save"):
var highscore_file = FileAccess.open("user://high_score.save",FileAccess.READ)
var highscore = highscore_file.get_32()
high_score.text = str(highscore)
var currentscore_file = FileAccess.open("user://score.save", FileAccess.READ)
var current_score = currentscore_file.get_32()
currentscore_file.close()
if highscore < current_score:
highscore = current_score
var new_highscore = FileAccess.open("user://high_score.save", FileAccess.WRITE)
new_highscore.store_32(current_score)
high_score.text = str(current_score)
new_highscore.close()
falling objects script:
extends Area2D
func _physics_process(delta: float) -> void:
position.y += 100 * delta
@onready var score = $"../score"
func _on_body_entered(body: Node2D) -> void:
if body.name == "pink_cookie_man":
score.add_point()
score.add_highscore()
queue_free()
games.gd
extends Node2D
@onready var new_game_score = $score
@onready var new_game_highscore = $score
func _ready():
$cookie_normal_Timer.wait_time = 0.5
$cookie_normal_Timer.timeout.connect(_on_timer_timeout)
newgame_score()
newgame_highscore()
func _on_timer_timeout():
randomize()
var cookie_normal = preload("res://scenes/cookie_normal.tscn").instantiate()
var mint_choco = preload("res://scenes/cookie_mint_choco.tscn").instantiate()
var strawberry_choco = preload("res://scenes/cookie_strawberry_choco.tscn").instantiate()
var sprinkles_frosting = preload("res://scenes/cookie_sprinkles.tscn").instantiate()
var jam_cookie = preload("res://scenes/cookie_jam.tscn").instantiate()
var dynamite = preload("res://scenes/dynamite.tscn").instantiate()
cookie_normal.position.y = -534
cookie_normal.position.x = randf_range(-1152,1152)
mint_choco.position.y = -534
mint_choco.position.x = randf_range(-1152,1152)
strawberry_choco.position.y = -534
strawberry_choco.position.x = randf_range(-1152,1152)
sprinkles_frosting.position.y = -534
sprinkles_frosting.position.x = randf_range(-1152,1152)
jam_cookie.position.y = -534
jam_cookie.position.x = randf_range(-1152,1152)
dynamite.position.y = -534
dynamite.position.x = randf_range(-1152,1152)
add_child(cookie_normal)
add_child(mint_choco)
add_child(strawberry_choco)
add_child(sprinkles_frosting)
add_child(jam_cookie)
add_child(dynamite)
func newgame_score():
if FileAccess.file_exists("user://score.save"):
var new_gamescore = FileAccess.open("user://score.save",FileAccess.WRITE)
new_gamescore.store_32(0)
new_gamescore.close()
func newgame_highscore():
if FileAccess.file_exists("user://high_score.save"):
var score_file = FileAccess.open("user://high_score.save", FileAccess.READ)
score_file.get_32()
new_game_highscore.highscore = score_file.get_32()
score.gd is a script inside score which is under the game node. The death scene is on a separate scene that isn’t under the game node.
I tried adding the newgame_highscore function in the games.gd hoping it would set the highscore to the previous game’s score preventing it from being overwritten by the current score, but the same issue remained.