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

0 favourites
  • 10 posts
From the Asset Store
Total customisation of the input! You can combine inputs from all peripherals. Make your game accessible for everyone!
  • 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

    mega.nz

  • 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)

  • Thanks mekonbekon . I', trying it right now.

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

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Problem is I have 10 textboxes same object. Shall I make 10 different textboxes or can I do with same textbox?

  • 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)

    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)

  • Thanks for the tips. Really appreciate it.

  • khelaghor No worries, glad to be able to help

  • 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

  • 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

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)