Problems to verify if the same number was clicked

Get help using Construct 2

Post » Sat Feb 01, 2014 7:19 pm


I'm having some trouble to find the error of this script and so I decide to ask for help. The goal of the script is to check that the correct number was clicked. When picks a number, the audio tells the same number and I click on the same number, but the answer appears as wrong. Someone could check for me? Thank you.

B
74
S
13
G
4
Posts: 1,005
Reputation: 8,058

Post » Sat Feb 01, 2014 10:30 pm

Is numbers.value a text variable?
B
55
S
29
G
19
Posts: 1,520
Reputation: 25,650

Post » Sun Feb 02, 2014 12:20 am

@ramones,

number.value is a number variable
B
74
S
13
G
4
Posts: 1,005
Reputation: 8,058

Post » Sun Feb 02, 2014 1:59 am

@RenatoB, there are a few things in your code that seem a little funny to me, but I tested some of them out and they seem to work (but just aren't quite the way I would have done it).

I believe the main problem is that the CheckAnswer function is not working with the same "numbers" instance as the one you touch. When you touch one, it is the only instance "picked", but when you call the CheckAnswer function it will randomly pick any instance to work with. So, You should pass in the numbers.UID into the CheckAnswer function, and then in there "pick" that unique ID.

One other potential problem is in your "On Start of layout" section, where you "Pick a random numbers instance". You repeat that loop 20 times, but theoretically it could pick the same numbers instance all 20 times. Out of 20 times through the loop, you will have no way of knowing how many instances got picked more than once, and how many never got picked. If you have 20 instances of numbers, and want to set each one to a random animation frame, then replace the "Repeat count times" and "Pick a random instance" with "For each numbers". (the part where you choose a random index and delete it out of the array seems fine as far as I can tell).
B
97
S
34
G
20
Posts: 438
Reputation: 17,790

Post » Sun Feb 02, 2014 10:29 am

[QUOTE=AllanR] @RenatoB, there are a few things in your code that seem a little funny to me, but I tested some of them out and they seem to work (but just aren't quite the way I would have done it).[/QUOTE]

I'm a newbie programmer trying to accomplish some personal projects. When I compare my scripts with others that were written by more experienced programmers, I see that are very different. But how in the end everything works, I keep following this line.

[QUOTE=AllanR]I believe the main problem is that the CheckAnswer function is not working with the same "numbers" instance as the one you touch. When you touch one, it is the only instance "picked", but when you call the CheckAnswer function it will randomly pick any instance to work with. So, You should pass in the numbers.UID into the CheckAnswer function, and then in there "pick" that unique ID.[/QUOTE]

I'll check it. What I find strange is that the texts fields that I include for testing, shows the same number in both: the number of the draw as well as what was clicked. I thought it was something wrong, the text fields would show that.

[QUOTE=AllanR]One other potential problem is in your "On Start of layout" section, where you "Pick a random numbers instance". You repeat that loop 20 times, but theoretically it could pick the same numbers instance all 20 times. Out of 20 times through the loop, you will have no way of knowing how many instances got picked more than once, and how many never got picked. If you have 20 instances of numbers, and want to set each one to a random animation frame, then replace the "Repeat count times" and "Pick a random instance" with "For each numbers". (the part where you choose a random index and delete it out of the array seems fine as far as I can tell).[/QUOTE]

In fact the numbers are not repeated, only the "0" but it's intentional.

RenatoB2014-02-02 10:31:04
B
74
S
13
G
4
Posts: 1,005
Reputation: 8,058

Post » Sun Feb 02, 2014 12:37 pm

[QUOTE=RenatoB] @ramones,

number.value is a number variable[/QUOTE]

Well you're comparing soundNumber (a text variable) to number.value (a number variable) in events 12/13. You'd need to compare similar types either by making both variables text or number, or by comparing
[code]soundNumber = str(number.value)[/code]
or
[code]int(soundNumber) = number.value[/code]
B
55
S
29
G
19
Posts: 1,520
Reputation: 25,650

Post » Sun Feb 02, 2014 12:59 pm

Hi @ramones. After I read your post I changed the soundNumber variable type to number too. Now both variables are number variables. I even created a local variable with the same name and removed the global one, but still not working. Now the problem inverted: all number that I clicked is right.
B
74
S
13
G
4
Posts: 1,005
Reputation: 8,058

Post » Sun Feb 02, 2014 1:52 pm

Instead of trying to solve the problem I decided to follow the @AllanR suggestion and made comparisons between UIDs and everything worked. Thank you for help me.RenatoB2014-02-02 14:33:21
B
74
S
13
G
4
Posts: 1,005
Reputation: 8,058

Post » Sun Feb 02, 2014 3:32 pm

@RenatoB, the reason you were seeing the correct number when you clicked on it was because you were displaying the number in the text field before you called the CheckAnswer. At that point you did have the correct instance, but then in the function it has no idea what has happened outside of the function so it just used any random instance.

The comparing a string variable to an integer would have also been a problem... anyway, glad you got it working!   :)
B
97
S
34
G
20
Posts: 438
Reputation: 17,790

Post » Sun Feb 02, 2014 4:39 pm

@Allan: I got it. Living and learning. Thanks for the tips.
RenatoB2014-02-02 16:39:40
B
74
S
13
G
4
Posts: 1,005
Reputation: 8,058

Next

Return to How do I....?

Who is online

Users browsing this forum: MathNook and 48 guests