How do I compare input text to variable

  • Hi,

    I'm trying to make some elearning math content.

    I need to be able to show on screen a question (eg 1 + 2), then have a input text field that the user will enter the answer into.

    Once they've entered the answer they need to click a check answer button that then marks the question right or wrong.

    Can someone point me in the right direction, I've look through the docs and the forum but can't see anything similar to this.

    Thanks

  • You should use a textbox. You can access the textbox content by using textbox.Text, so, you just compare this to your correct answer, when the button is pressed, and check if they match.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Yes, I already have a textbox, but I don't know how to access/compare when the button is pressed?

  • Something like this?

    On button clicked
    System -> Compare two values -> Int(TextBox.Text) equals CorrectAnswer
    [/code:30aghlwo]
    
    .Text property of the TextBox stores string data, and I assume that the CorrectAnswer variable stores numbers.
    That's why to compare them you need to use Int(), which converts string into number. 
    
    Another way:
    [code:30aghlwo]On button clicked
    TextBox -> Compare text -> Str(CorrectAnswer)[/code:30aghlwo]
    
    Here is an opposite thing - you need to convert the number CorrectAnswer into string, you can do it with Str() function.
  • thanks, that worked perfectly

    one further question... is there such a thing as null values in Construct?

    I have it set that if the Textbox is equal to CorrectAnswer its correct, else its wrong, but I would like it that if nothing has been entered into the Textbox then nothing happens

    I've tried

    Int(TextBox.Text) equals " "
    Int(TextBox.Text) equals Null
    Int(TextBox.Text) equals NaN[/code:378j1uy1]
    
    So I'm guessing this isn't the way to do it in Construct
  • No, there is no Null in C2. Empty string is always "" (quotes without space in between).

  • sorry it was a typo above I meant ""

    currently I have

    Button On Clicked
    System -> Compare Two Values -> int(TextBox.Text) = ""
    Text -> Set text to "empty"[/code:21sy3pkg]
    
    I set the Text field up so I could see if there function was working... which it isn't
  • That's because you are comparing number with string!

    Int() is number and "" is string.

    You should compare either TextBox.Text="" or Int(TextBox.Text)=0

  • ah yes, of course

    using TextBox.Text="" works fine

    thanks

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