Godot Version
3
I want to have a leaderboard at my web game (there was only 1 tutorial and very different from my game, so I think I made some errors)
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
var leaderboard_name = "BestTime"
signal death_signal
signal player_died_signal
func _ready() -> void:
print("Game started")
deathsound.connect("finished", self,"audio_finished")
highscore = load_highscore()
if Bridge.platform.language == "ru":
print("Setting highscore label to Russian")
$Highscore.text = "ΡΠ΅ΠΊΠΎΡΠ΄: " + str(highscore)
if Bridge.platform.language == "en":
print("Setting highscore label to Russian")
$Highscore.text = "highscore: " + str(highscore)
$Timer1.text = str(int(time_alive))
connect("death_signal", self, "_on_Player_death")
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:
var set_score_options = Bridge.SetScoreYandexOptions.new(highscore, leaderboard_name)
Bridge.leaderboard.set_score(set_score_options)
Bridge.storage.set("score", highscore, funcref(self, "_on_storage_set_complited"))
if Bridge.platform.language == "ru":
print("Setting highscore label to Russian")
$Highscore.text = "ΡΠ΅ΠΊΠΎΡΠ΄: " + str(highscore)
if Bridge.platform.language == "en":
print("Setting highscore label to Russian")
$Highscore.text = "highscore: " + str(highscore)
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()
print("Player died with score: ", current_score)
if current_score > highscore:
# Update the high score if the current score is higher
highscore = current_score
print("Updating highscore from ", highscore, " to ", current_score)
save_highscore(highscore)
if Bridge.platform.language == "ru":
print("Setting highscore label to Russian")
$Highscore.text = "ΡΠ΅ΠΊΠΎΡΠ΄: " + str(highscore)
if Bridge.platform.language == "en":
print("Setting highscore label to Russian")
$Highscore.text = "highscore: " + str(highscore)
else:
print("No new highscore. Current highscore: ", 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")
emit_signal("death_signal")
$AnimationPlayer.play_fade_out()
yield(get_tree().create_timer(3), "timeout")
if AdManager.can_show_ad:
Bridge.advertisement.show_interstitial()
else:
get_tree().change_scene("res://Scenes/Menu.tscn")
func audio_finished() -> void:
print("Audio finished")
func save_highscore(highscore: float) -> void:
print("Saving highscore: ", highscore)
var file = File.new()
var err = file.open("user://savefile.dat", File.WRITE)
if err == OK:
file.store_float(highscore)
file.close()
print("Highscore saved successfully: ", highscore)
else:
print("Error saving highscore: ", err)
func _on_visibility_state_changed(state):
if state == "hidden":
BgMusic.set_stream_paused(true)
else:
BgMusic.set_stream_paused(false)
func load_highscore(default_value: float = 0.0) -> float:
var file = File.new()
if file.file_exists("user://savefile.dat"):
var err = file.open("user://savefile.dat", File.READ)
if err == OK:
var content = file.get_float()
file.close()
print("Loaded highscore: ", content)
return content
else:
print("Error loading highscore: ", err)
else:
print("Save file does not exist, returning default value.")
return default_value
func stop_timer():
running = false