Script not working

Godot Version

Godot 4

Question

Hi Godot users of the world!

I am having trouble with this script

@onready var label: Label = $Label
@onready var label_2: Label = $Label2
@onready var label_3: Label = $Label3
@onready var label_4: Label = $Label4
@onready var label_5: Label = $Label5


var all_wrong = false
var button1pressed = false
var button2pressed = false
var button3pressed = false
var button4pressed = false
var button5pressed = false
var value1
var value2
var value3
var value4
var value5
var lockopened = false
var value1num = "6"
var value2num = "4"
var value3num = "5"
var value4num = "4"
var value5num = "9"
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
	pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
	if (value1 == value1num and value2 == value2num and value3 == value3num and value4 == value4num and value5 == value5num):
		lockopened = true
	else: 
		all_wrong = true
	if (value1 != value1num and all_wrong == true):
		label.text = "_"
		button1pressed = false 
	if (value2 != value2num and all_wrong == true): 
		label_2.text = "_"
		button2pressed = false 
	if (value3 != value3num and all_wrong == true): 
		label_3.text = "_" 
		button3pressed = false
	if (value4 != value4num and all_wrong == true):
		label_4.text = "_"
		button4pressed = false 
	if (value5 != value5num and all_wrong == true):
		label_5.text = "_" 
		button5pressed = false```



It should set all the labels back to "_" only when all the numbers are wrong, but it doesn't do it.
the only way I found to set them back is to use as condition only "valuex != valuexnum" but it then makes the code easy to bruteforce.

Are you trying to do an input number pad and have the user input a PIN number of sorts?

I am not sure what the relationship is between the labels and the button1pressed type variables, or how the user is actually inputting these values. Could you add a little more detail on what you are trying to achieve?

You do not seem to be initializing the values you are comparing against.

If you run this code with debugging turned on, where does it break?

Personally, I would replace your first if statement with a series of boolean assignments. Something like this:

all_wrong = value1 == value1num
all_wrong &= value2 == value2num
...

That style makes it easier to debug where a specific conditional statement is breaking.

You got it right.
I am doing it so that when the PIN scene is interacted with, the user is able to press numbers on the keyboard to input the pin, I used to have the buttonpressed booleans to make it so that they got pressed in order. I think they went unused, I don’t kinda remember and I don’t see them in the script, it’s been months.

why all_wrong = value1 == value1num??
all_wrong it’s a Boolean, while the values are ints.
can you explain yourself better?

With respect, the values you are comparing are not ints. value1 doesn’t have an assigned type and value1num is a string.

As for this:

all_wrong = value1 == value1num

all_wrong is a boolean. So is the value returned by the equality comparison. It might make a bit more sense written like this:

all_wrong = (value1 == value1num)

It might be worth noting here that the LineEdit control has a secret property; turn that on and it will obscure the input for pin/password without you having to explicitly code it.

2 Likes

Cool! Thanks I didn’t think of it.

Yeah sorry about that, I wasn’t feeling great at the moment and didn’t pay attention to what I said. It makes sense this way, I’ll look into it, but I’m starting to think about dismantling the whole project and reprogramming it, there is many code that probably has to be rewritten.