Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | YusukeHashi |
Hi guys. I am trying to make my first simple game (similar to the Simon Says but with 9 buttons) and I got stuck at the part where I have to manually give some input to the program.
On the function “func _on_button_pressed():” if I just return a random number (for example 4) the program will just run as normal, but if I try to add some input by myself I will run into an error [ invalid operands ‘int’ and ‘object’ in operator ‘==’ ]
Could you help me understand why is that happening?
Thanks in advance
extends GridContainer
export(ButtonGroup) var group
var random_number = 0
var start_game = true
var beatSeq = Array()
var userSeq = Array()
var buttonPressed = 0
func _ready():
for i in group.get_buttons():
i.connect("pressed", self, "_on_button_pressed")
while start_game:
random_number = random_number_generator()
beatSeq.append(random_number)
print(beatSeq)
var buttonPressed = _on_button_pressed()
print("UserPressed is " + str(buttonPressed))
userSeq.append(buttonPressed)
var result = check_seq(beatSeq, userSeq)
if beatSeq.size() > 3:
start_game = false
print(beatSeq)
print(userSeq)
func _on_button_pressed():
for i in group.get_buttons():
if i.is_pressed():
if i.get_name() == "a1":
print("a1")
return 0
elif i.get_name() == "a2":
print("a2")
return 1
elif i.get_name() == "a3":
print("a3")
return 2
elif i.get_name() == "b1":
print("b1")
return 3
elif i.get_name() == "b2":
print("b2")
return 4
elif i.get_name() == "b3":
print("b3")
return 5
elif i.get_name() == "c1":
print("c1")
return 6
elif i.get_name() == "c2":
print("c2")
return 7
elif i.get_name() == "c3":
print("c3")
return 8
# yield()
return 4
func random_number_generator():
var rndNum = 0
randomize()
for n in range(0, 1):
rndNum = randi() % 9
return rndNum
func check_seq(sequence, attempt):
var result = false
var k = 0
for i in sequence.size():
if sequence[i] == attempt[i]:
print("CORRECT")
result = true
k += 1
else:
print("WRONG")
result = false
continue
return result
At what line/point is the error invalid operands 'int' and 'object' in operator '=='
being returned?
Ertain | 2022-07-18 17:17
I am getting that happening inside the “func check_seq()” at this point:
if sequence[i] == attempt[i]:
I am trying to figure it out by myself but it is difficult to comprehend it.
When I hover over the sequence[i]
I get sequence:[8]
, and when I hover over the attempt[i]
I get attempt:[[EncodedObjectAsID:32330]]
.
Does that tell you anything?
YusukeHashi | 2022-07-18 17:29
Add a break point at the line that is causing the issue. Then look at the variable values in the memory. The error is saying your comparing an integer to an object, so looking at the variables’ values at the error line will tell you which variable has an invalid value. That should help you find the source of the issue.
godot_dev_ | 2022-07-18 19:02
I have no idea what could be causing it. It is my second week with Godot and I don’t have much experience coding.
On the other hand, running this code and looking at the output is giving me a hard time to figure out what is happening.
For example, sometimes, when I run the code it gives me something like this:
[5]
UserPressed is Null
WRONG
[5, 2]
UserPressed is Null
WRONG
WRONG
[5, 2, 7]
UserPressed is Null
WRONG
WRONG
WRONG
[5, 2, 7, 8]
UserPressed is Null
WRONG
WRONG
WRONG
WRONG
[5, 2, 7, 8]
[4, 4, 4, 4]
Where it shows that userPressed is Null
but the at the end it gives me [4,4,4,4]
which is the number I gave it.
Maybe my fault for starting a project that is too complex for me.
YusukeHashi | 2022-07-18 19:47