Godot Version
4.6
Question
Hello!!
I have a score for when a player presses arrows on rthym (its a rthym game), and i just added the second arrow (the grey arrow for when youre supposed to press the key i mean, think FNF/dance dance revolution). It was working perfectly before, but now whenever the second arrow gets pressed, it subtracts one, and then when the first arrow gets pressed, it goes to the score its supposed to be. Is there a reason why? How do I fix this?
First arrow and second arrow scripts are functionally the same as well, obviously connected to their respective areas/collision shapes though. Im very confused.
#arrow one
extends StaticBody2D
@onready var mainnode = $".."
var score = 0
@onready var score_label = $"../CanvasLayer/Label"
@export var body_entered = false
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'):
set_score(score + 1)
#arrow 2
extends StaticBody2D
@onready var mainnode = $".."
var score = 0
@onready var score_label = $"../CanvasLayer/Label"
@export var body_entered1 = false
func set_score(new_score: int) -> void:
score = new_score
score_label.text = "Score: " + str(score)
func _process(delta: float) -> void:
if body_entered1 != false:
if Input.is_action_just_pressed('right_arrow'):
set_score(score + 1)
You should have only one variable that tracks score. Currently you have two - one in each script, each tracking its own score.
3 Likes
Try tracking the score in the score label for now using this script:
class_name ScoreLabel extends Label
var score: int = 0:
set(value):
score = value
text = "Score: " + str(score)
func _process(delta: float) -> void:
if body_entered != false:
if Input.is_action_just_pressed('left_arrow'):
set_score(score + 1)
Then change the script attached to the two buttons to this:
#arrow one
extends StaticBody2D
@onready var mainnode = $".."
@onready var score_label = $"../CanvasLayer/Label"
@export var body_entered = false
func _process(delta: float) -> void:
if body_entered != false:
if Input.is_action_just_pressed('left_arrow'):
score_label.score += 1
#arrow 2
extends StaticBody2D
@onready var mainnode = $".."
@onready var score_label = $"../CanvasLayer/Label"
@export var body_entered1 = false
func _process(delta: float) -> void:
if body_entered1 != false:
if Input.is_action_just_pressed('right_arrow'):
score_label.score += 1
1 Like
You could combine the scripts to make one arrow.gd by exporting the differences between them, so far it seems to only be the action required
@export var action_name: StringName # assign to left_arrow or right_arrow in inspector
# later ...
if Input.is_action_just_pressed(action_name):
Despite using the same script they would still have a score variable per-node; but now you could use static var score to ensure the score is used across every node with the same script.
2 Likes
Thank you!!! Thisworked :DDD
The real questions is - do you understand why it worked?
2 Likes