|
|
|
|
Reply From: |
DaddyMonster |
Your save_highscore
and load_highscore
methods both look fine.
You just need to add a check to see if your latest score is higher than your existing highscore
after you die and update your highscore value if so before you save it.
Something like this would work:
func _on_Ground_died():
if score > highscore:
highscore = score
save_highscore()
death_screen.init_game_over_menu(score, highscore)
When I do that, the score and highscore always give a 0 value. even if I scored more than 0.
It’s like the highscore is not registering for some reason.
hommixo | 2022-01-16 19:41
Score is also becoming zero also? After start you mean or after the player dies? Didn’t you say the death_screen
score was working?
Also, is there logic in your set_score()
method? Unless I’m missing something (other than what I mentioned before), the issue isn’t in the code you included.
We need to narrow down the issue. In each function add a `printt(“score”, score, “highscore”, highscore). Try to find where it gets reset to zero.
Try changing var highscore = 5
at the top. See if it stays as that.
Finally, check the load file is being found:
if not save_data.file_exists(SAVE_FILE_PATH):
print("file not found")
return
DaddyMonster | 2022-01-16 20:04
The print("file not found")
doesn’t show so the load file is being found. When I set the highscore var equals to 5, it still shows a 0 which is really weird.
hommixo | 2022-01-16 20:26
Here is my entire World Scene code:
extends Node2D
onready var hud = $HUD
onready var obstacle_spawner = $ToastSpawner
onready var death_screen = $DeathScreen
onready var death_speech = $SpeechBubbleDeath
onready var timer = $DeathTimer
onready var villain = $VillainToast
onready var background_sound = $SoundEffects/BackgroundSound
onready var water_boil = $SoundEffects/WaterBoil
onready var wind = $SoundEffects/Wind
onready var villain_laugh = $SoundEffects/VillainLaugh
const SAVE_FILE_PATH = "user://savedata.save"
var score = 0 setget set_score
var highscore = 5
func _ready():
#background_sound.play()
#water_boil.play()
wind.play()
obstacle_spawner.connect("obstacle_created", self, "_on_obstacle_created")
new_game()
villain.play("default")
func new_game():
self.score = 0
obstacle_spawner.start()
load_highscore()
func player_score():
self.score += 1
func set_score(new_score):
score = new_score
hud.update_score(score)
func _on_obstacle_created(obs):
obs.connect("score", self, "player_score")
#Controls the death scene
func _on_Ground_died():
timer.start()
obstacle_spawner.stop()
death_speech.visible = true
villain.play("laugh")
villain_laugh.play()
func game_over():
if score > highscore:
score = highscore
save_highscore()
death_screen.init_game_over_menu(score, highscore)
func _on_DeathTimer_timeout():
game_over()
func save_highscore():
var save_data = File.new()
save_data.open(SAVE_FILE_PATH, File.WRITE)
save_data.store_var(highscore)
save_data.close()
func load_highscore():
var save_data = File.new()
if not save_data.file_exists(SAVE_FILE_PATH):
print ("file not found")
return
save_data.open(SAVE_FILE_PATH, File.READ)
highscore = save_data.get_var()
save_data.close()
hommixo | 2022-01-16 20:35
You’ve got this the wrong way round:
func game_over():
if score > highscore:
score = highscore
Should be highscore = score
But, just for now make it: highscore = 42
In load_highscore
add a print to see what comes out:
highscore = save_data.get_var()
printt("Highscore loaded:", highscore)
save_data.close()
DaddyMonster | 2022-01-16 20:46
I get:
Highscore loaded: 0
and on the death screen, the highscore value did change to 42.
hommixo | 2022-01-16 20:51
What?! Weird is the word…
Ok, change the save and load from store_var()
to store_float()
- send in 42 and see what comes out. You can open up the file in notepad for good measure to see what’s there if you like.
DaddyMonster | 2022-01-16 21:00
When I do that the highscore becomes equal to Null.
hommixo | 2022-01-16 21:14
I finally got it to work! After changing the code to what you proposed and it didn’t work, I changed it back to what it was before and everything seems to be working perfectly, I don’t know what was causing the issue.
hommixo | 2022-01-16 21:26
Thank you so much for your help, you are a lifesaver!
hommixo | 2022-01-16 21:30
I suspect the file was corrupted. Changing the type probably meant making a new one so changing it back fixed the issue.
Anyway, great that it’s working!
DaddyMonster | 2022-01-16 21:35