My score isnt updating my win condition

Godot Version

4.6

Question

Hello!!! SO i have a win condition to either win or lose if you have a certain score, but everytime i try to run it, I always lose, even when Im way above the score i said as what was needed. Im pretty sure this is because maybe the ‘score’ is only updating the label and isnt actually a number, but i dont know how to fix that. Any help would be appreciated. THanks!!

This is in my main node for the score:

var score = 0

func set_score(new_score: int) -> void:
	score = new_score
	score_label.text = "Score: " + str(score)


this is the label for the score:

extends Label

var score: int = 0:
	set(value):
		score = value
		text = "Score: " + str(score)

example of how the score gets added, only one put in here because i have four different arrows for this rythm game, and theyre all the same. (which is changing my label to be +1 everytime but again, dont think its changing the actual number :,D)

func set_score(new_score: int) -> void:
	score = new_score
	score_label.text = "Score: " + str(score)
	
	
func _process(delta: float) -> void:
	if body_entered != false:
		if Input.is_action_just_pressed('left_arrow'):
			score_label.score += 1

and heres what was supposed to work, but is only giving me losses:

func _process(delta: float) -> void:
	if Globals.game_end:
		if score > 29:
			get_tree().change_scene_to_file("res://Scenes/office.tscn")
			Globals.won = true
			print("win!")
		else:
			get_tree().change_scene_to_file("res://Scenes/office.tscn")
			print("lost :(")

You can just call set_score instead of setting the text of the label, like I changed you code above. I think that should work.

I just did this yesterday with my game jam game I’m developing, Rick O’Shea.

You can see the label in the top right with a funky font (it says 134).

Here’s the label code:

extends Label


func _ready() -> void:
	Game.new_score.connect(_on_new_score)


func _on_new_score(score: int) -> void:
	text = str(score)

Then the score is stored in my Autoload, called Game:

signal new_score(value: int)

var score: int = 0:
	set(value):
		score = value
		new_score.emit(value)

When a bullet hits the Target, it calculates the score and adds it:

class_name Target extends StaticBody3D

@export var score_multiplier: int = 1

func hit(ricochet_amount: int) -> void:
	Game.score += ricochet_amount * score_multiplier

You can do the same thing.

  1. Add this to your Globals:
signal new_score(value: int)

var score: int = 0:
	set(value):
		score = value
		new_score.emit(value)
  1. Change your Label to this:
extends Label


func _ready() -> void:
	Globals.new_score.connect(_on_new_score)


func _on_new_score(score: int) -> void:
	text = "Score: " + str(score)
  1. Get rid the code in your Main node. You shouldn’t need it. If you do, use this:
func set_score(new_score: int) -> void:
	Globals.score = new_score
  1. Change your nodes that add score to this:
func _process(delta: float) -> void:
	if body_entered != false:
		if Input.is_action_just_pressed('left_arrow'):
			Globals.score += 1

I removed the set_score() function because I think it’s causing you problems.

  1. Replace your game end code with this:
func _process(delta: float) -> void:
	if Globals.game_end:
		if Globals.score > 29:
			get_tree().change_scene_to_file("res://Scenes/office.tscn")
			Globals.won = true
			print("win!")
		else:
			get_tree().change_scene_to_file("res://Scenes/office.tscn")
			print("lost :(")

That should get you there, or close to it.

You have two different score variables, it seems like you only add to the score_label.score

Then go on to check what I assume is the main’s score

You should remove the main script’s var score and replace it’s use with score_label.score


It would be better to use signals, try to avoid checking a value every frame such as if Globals.game_end.