How do I Get number as an input and store them in array

Get help using Construct 2

Post » Sat Jul 29, 2017 10:43 am

Message: khelaghor can only post plain text URLS until they have 500 rep. 1 URLS modified. Why?
Hi,
I'm struggling with "text box" form control. I want to get numbers as input and store them as variable and then calculate those variables to get an answer. But I can not get the values of the text. There's no such expression exists in construt 2 but I got it in Javascript. Can anybody know what to do. Here's some game instruction so that you can understand what I wanna do. I really appreciate the help. Thanks
https://mega.nz/#F!tEIEyZBB!G4nTrZvAOMCoY3uR8i3fnA
B
9
S
2
Posts: 28
Reputation: 585

Post » Sat Jul 29, 2017 11:27 am

You can use the following expressions to convert strings to numbers (from the manual):

float(x)
Convert the integer or text x to a float (fractional number). If x is text, non-numeric characters are allowed after the number, but not before. For example float("3.1xx") returns 3.1, but float("xx3.1") returns 0.

int(x)
Convert the float or text x to an integer (whole number). If x is text, non-numeric characters are allowed after the number, but not before. For example int("33xx") returns 33, but int("xx33") returns 0.

So if you want to assign the contents of a textbox object to a variable as a number, use the following action:

Set variable to float(Textbox.Text)

...or...

Set variable to int(Textbox.Text)

...depending upon how you want to store the number.

You can then assign that variable to an array index.

Alternatively you can skip the variable assignment and go straight to the array:

Set Array.at(X,Y) to float(Textbox.Text)
B
22
S
9
G
4
Posts: 476
Reputation: 4,327

Post » Sat Jul 29, 2017 11:53 am

Thanks @mekonbekon . I', trying it right now.
B
9
S
2
Posts: 28
Reputation: 585

Post » Sat Jul 29, 2017 12:04 pm

I want to limit the input of the user from (1-4). Could you please tell me how can I do that?
B
9
S
2
Posts: 28
Reputation: 585

Post » Sat Jul 29, 2017 12:14 pm

Problem is I have 10 textboxes same object. Shall I make 10 different textboxes or can I do with same textbox?
B
9
S
2
Posts: 28
Reputation: 585

Post » Sat Jul 29, 2017 12:38 pm

khelaghor wrote:I want to limit the input of the user from (1-4). Could you please tell me how can I do that?


You could use the Textbox:set text action before assigning to a variable e.g.:

If int(textbox.Text)>4 set Textbox text to 4; set variable to int(textbox.Text)
If int(textbox.Text)<0 set Textbox text to 0; set variable to int(textbox.Text)

khelaghor wrote:Problem is I have 10 textboxes same object. Shall I make 10 different textboxes or can I do with same textbox?


If you are assigning the values to an array, add an instance variable to the textbox object, give each textbox a unique value from 0-9 and then use that to assign the value to the array e.g.:

Set array value at (textbox.instanceNo, 0) to int(textbox.Text)
B
22
S
9
G
4
Posts: 476
Reputation: 4,327

Post » Sat Jul 29, 2017 3:13 pm

Thanks for the tips. Really appreciate it. :)
B
9
S
2
Posts: 28
Reputation: 585

Post » Sat Jul 29, 2017 3:31 pm

@khelaghor No worries, glad to be able to help :)
B
22
S
9
G
4
Posts: 476
Reputation: 4,327

Post » Sat Jul 29, 2017 6:54 pm

Would you gonna use TextBox with Type = number (maybe you love the spinners) ...
And would you want the numbers limited to 0,1,2,3 or 4 (natural numbers, so 1.333 is not allowed) ....
.... Then you have to deal with a lot of exceptions.

First this, the TextBox.text expression can only contain numbers.
Yet, it accepts the characters "-" and "+" and "," and "." as input.
It shows them, but they will not be inside the TextBox.text expression, unless they form a number.

So
if you type "4+4" in the Box, TextBox.text contains something, but it is not the empty string ""
if you type "-" in the the Box, TextBox.text contains something, but it is not the empty string ""
if you type "-5", TextBox.text contains "-5", that is a number.
If you type "4.", TextBox.text contains something, but it is not the empty string ""
If you type "4.1" , TextBox.text contains "4.1", that is a number.

So. Again. The TextBox.text expression can only contain numbers, even if it is showing something else.

This is a modest attempt to catch the exceptions.
https://www.dropbox.com/s/qsi250afm5ie9 ... .capx?dl=0
B
33
S
18
G
28
Posts: 2,493
Reputation: 20,950

Post » Sat Jul 29, 2017 7:55 pm

Thanks @99Instances2Go for your reply. I've already solved the problem. Now I'm dealing with another problem. How to make a table with rows and columns by writing code into construct 2. Its pretty easy in HTML but I can't figure out how I can do it in cosntruct
B
9
S
2
Posts: 28
Reputation: 585


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 0 guests