How do I compare text input with a random text from an Array

  • Hi friends

    I am trying to design a simple word quiz game. I successfully displayed a random word as the question by using array and ajax. Then I want the user to type the same word in the answer text box as a practice. If correct, a simple game will be shown for a short time before the user continues the quiz study. However, when I set the condition as answer.text = question.text (which is the randomly displayed word from the array), the action won't be triggered even if I type in the displayed word. In comparison, if I manually typed in a word and make it the "set text" of the answer text box, it works well. Sorry for the lengthy question. The image might make it more clear. It seems the randomed displayed word was not recognized as a value. Thank you and I appreciate your time and help!

  • What type of object is your "answer"? Its icon looks different from the TextBox icon. Is it some custom plugin?

    Have you tried running the project in Debug Mode (ctrl-F4)? You can check the contents of the array, question, answer objects before pressing the button.

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • Thanks dop2000! I used a textbox addon, but I also tried the normal textbox before that. Neither worked. I tried the Debug Mode, but failed to find any glaring issue. I cannot figure it out why it doesn't work when I set the textBox.text as the question.text which is a randomly displayed text from the array. I don't understand why a randomly displayed text from the array hasn't been recognized by Construct 2 as a value.

  • Do you see if question object actually contains any text after you click it?

    Maybe there is a space before/after the word? Try adding trim() to remove all spaces and line breaks.

    If question.text is empty, maybe you need to add Round or Int before Random(0,26).

    Also, in event #3 you can use loopindex instead of i.

    If nothing helps, could you share your capx?

  • Thanks for your kind help dop2000! I am at my wit's end now. Could you help check the capx? Really appreciate your help!

    https://goo.gl/CT8Kyz

  • Your words in the array start with line break.

    Just add trim() to remove all invisible characters (spaces, line breaks) from the beginning and end of the string.

    Also you can replace "i" with loopindex:

  • THANK YOU so much for all the help dop2000!! It works now. Btw, how can I be as nearly good as you guys in the future? Do I need to learn javascript to know all these?

  • Ok, I'll tell you my secret how to become an expert in C2

    When you are adding/editing an action, there should be a semitransparent window somewhere on the screen - "Object with expressions". If you don't see it, press Ctrl-F4.

    Explore everything in this window, it lists all expressions for all objects. The most useful one is System, but others also contain lots of interesting stuff.

    That's it That's my secret - I know where to lookup expressions. And now you are an expert too!

    And no, I don't know javascript

  • Thanks buddy! Never know this. Learned so much from you!

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