Need help with first game!

Godot Version

3

Hi,I have exported my game from godot 3 to web and there are some bugs
1)my high score isn’t saving anything.(i think i found the problem)
2)Localization bug- I need to make my game in 2 language in russian and english so i make it but it dont work correctly.
3)Ad manager bug-Interstitial ad dont work correctly.
So guys if anyone can help me i will be very gratefull.

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
var leaderboard_name = "bestscore"

func _on_interstitial_state_changed(state):
	if state == "closed" or state == "failed":
		BgMusic.set_stream_paused(false)
		get_tree().change_scene("res://Scenes/Menu.tscn")
	elif state == "opened":
		BgMusic.set_stream_paused(true)
	else:
		BgMusic.set_stream_paused(false)
		
signal death_signal

func _ready() -> void:
	print("Game started")
	if Bridge.platform.language == "ru":
		$Highscore.text = "рекорд: " + str(highscore)
	if Bridge.platform.language == "en":
		$Highscore.text = "highscore: " + str(highscore)	
	
	if not Bridge.advertisement.is_connected("interstitial_state_changed", self, "_on_interstitial_state_changed"):
		Bridge.advertisement.connect("interstitial_state_changed", self, "_on_interstitial_state_changed")
	deathsound.connect("finished", self,"audio_finished")
	Bridge.game.connect("visibility_state_changed", self, "_on_visibility_state_changed")
	highscore = load_highscore()
	$Highscore.text = "рекорд: " + str(highscore)
	$Timer1.text = str(int(time_alive))
	connect("death_signal", self, "_on_Player_death")
	set_process(true)

func _on_visibility_state_changed(state):
	if state == "hidden":
		BgMusic.set_stream_paused(true)
	else:
		BgMusic.set_stream_paused(false)	

	
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()
	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)
		$Highscore.text = "рекорд: " + 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")
	Bridge.advertisement.show_interstitial()
	
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 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		





menu gd

extends Control

onready var gamename = $Label2
onready var space = $Label

func _ready() -> void:
	Bridge.advertisement.set_minimum_delay_between_interstitial(60)
	#if Bridge.platform.language == "ru":
		#gamename = "Побег Шарика"
		#space = "нажмите пробел чтобы начать"
	if Bridge.platform.language == "en":
		gamename = "Balloon escape"
		space = "press space to start"
			
func _process(_delta):
	if Input.is_action_just_pressed("space"):
		var result = get_tree().change_scene("res://Scenes/Game.tscn")
		if result != OK:
			print("Error changing scene: ", result)

If you post error messages (in case there are any) and describe the results you expect and which you get, it is more likely someone can and will help you.

For example try printing out same values if you get no error messages and tell us what it prints.

I wouldn’t get the highscore from the timer text:
var current_score = $Timer1.text.to_int()
It is better to work with numbers if you already have them somewhere.

Why don’t you use time_alive directly as highscore if that is the only way to score points?

      if time_alive > highscore:
           highscore = time_alive

I want to spawn another label after death in the bottom thats why and to know i have fixed the high score problem so if you can, help me with localization .

Here you reference your label nodes I guess?

onready var gamename = $Label2
onready var space = $Label

Here you overwrite them to be a string:

		gamename = "Balloon escape"
		space = "press space to start"

It should be gamename.text = "Ballon escape" I guess.

EDIT: I’m not familiar with Godot3. I’m not sure what changed in 4 so I must guess. :wink:

1 Like

okay, i will try

It really worked thank you very much!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.