WEB game leaderboard problem, please help

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

Does it work? or what is the problem?

No its not woriking in my exported game there is no leaderboard. I will send you now


there is nothing in my top player

My plugin leaderboard.

I think for this to work you have to implement this method in the AdManager:

func set_highscore(name: String, score: int)
    var options
    match Bridge.platform.id:
        "yandex":
            options = {
                "leaderboardName": name,
                "score": score
            }
    
            Bridge.leaderboard.set_score(options, funcref(self, "_on_set_score_completed"))
    
func _on_set_score_completed(success):
    print(success)

Then in your _on_Player_death()-method you have to call it with the appropiate name and score:

if current_score > highscore:
    AdManager.set_high_score(playername, current_score)
1 Like

Is this func set_highdcore in c#? Just interesting

Ah i think it has to be

func set_highscore(name: String, score: int)
    var options
    match Bridge.platform.id:
        "yandex":
            options = {
                "leaderboardName": name,
                "score": score
            }
    
            Bridge.leaderboard.set_score(options, Callable(self, "_on_set_score_completed"))
    
func _on_set_score_completed(success):
    print(success)
1 Like

I will try now

what is playername ?

Depends on you, do you want the name from the yandex-platform or do you have your own name in game?

no i dont make authorization to set name

Do you want to use the yandex-username?

in reality i dont care what name i need i just want to work this leaderboard :sweat_smile:

Well you need a name to display on the leaderboard and its your choice what name to display. You can also give everyone the same name


i have this Leaderboard technical name BestScore
and
[en] Displayed leaderboard name Max time
and same for russian [ru]…

You can just do

if current_score > highscore:
    AdManager.set_high_score("Player", current_score)
1 Like

and i have changed leaderboard name to this

func set_highscore(name: String, score: int):
    var options
    match Bridge.platform.id:
        "yandex":
            options = {
                "BestTime": name,
                "score": score
            }
    
            Bridge.leaderboard.set_score(options, funcref(self, "_on_set_score_completed"))

Im not sure if that works, but you can try it out

1 Like

okay then i will try as you said


same maybe need to beat my record ? to appear there or after moderation ?

it only updates when the condition current_score > highscore == true