Legacy GM [SOLVED] 'Else' in a For loop

D

Drydareelin

Guest
Hi all,

I've got a create event with a bunch of words in an array set up like so,

Code:
word[0] = "this";
word[1] = "is";
word[2] = "an";
word[3] = "example";
...
and the idea is that when I type a word in to a box, it checks through the array to see if it exists.

Currently I have the loop as;

Code:
if (check == true)  //set by another object
{
    for (var i = 0; i <= word_num; i++) // word_num = total number of words
    {
        if (word[i] == oInput.text)
        {
            show_message("Word " + oInput.text +" found.");  // Just for now, this will do more later.
            check = false;
            exit;
        } 

        // else...   
    }
}
And it works as intended. The problem is when I check for a word not in the list, I get an error. I've tried to include;

Code:
else
{
    show_message("Word not found.");
}
But this gives me that message for every word regardless.

Code:
if (word[word_num] != oInput.text)
{
   show_message(...);
}
But this gives me the same error as if there was no else statement at all.

Any ideas?

Edit: Meant 'create event', not 'text file'.
 
Last edited by a moderator:

PNelly

Member
You have you're code broken apart but what does it look like when you've put it all together?

Secondly, do the contents of the text file look like:

Code:
this
is
an
example
or like this

Code:
word[0] = this;
word[1] = is;
word[2] = an;
word[3] = example;
?

Finally, what code are you using to read that text file?
 
D

Drydareelin

Guest
Okay my bad.

So in object oWords

create event,
Code:
word[0] = this;
word[1] = is;
word[2] = an;
word[3] = example;
...

check = false;
word_num = array_length_1d(word);
step event,
Code:
if (check == true) {
    for (var i = 0; i <= word_num; i++) {
        if (word[i] == oInput.text) {
            show_message(...);  
            check = false;
            exit;
        }
        if (word[word_num] != oInput.text) // This is the part I need help with
        {
             show_message(...);
             check = false;
             exit;
        }    
    }
}

oInput,
step event,
Code:
...
if (enter)      // Check if enter key was pressed
{
    with (oWords)
        check = true;
}
 

YellowAfterlife

ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ
Forum Staff
Moderator
First things first, you probably want to use "break" instead of "exit", as that ends the entire script/event rather than loop. Then, it does execute every time because at least one word would not match. Perhaps you'd want to do it like so:
Code:
if (check == true) //set by another object
{
    var found = false;
    for (var i = 0; i <= word_num; i++) // word_num = total number of words
    {
        if (word[i] == oInput.text)
        {
            found = true;
            check = false;
            break;
        }
    } // for
    if (found) {
        show_message("Word " + oInput.text +" found."); // Just for now, this will do more later.
    } else show_message("Word not found.");
} // if (check)
 
D

Drydareelin

Guest
I've plugged that in but still getting out the same error unfortunately,

Execution Error - Variable Index [0,85] out of range [1,85] - -1.word(100003,85)
at gml_Object_oWords_Step_0
 
D

Drydareelin

Guest
That was pretty much it (which made me irrationally upset.)

Final code;

Code:
if (check) {
    var found = false;
    for (var i = 0; i < word_num; i++) {
        if (word[i] == oInput.text) {
            found = true;
            check = false;
            break;
        }
    }
    if (found) {
        show_message("Word '" + oInput.text + "' found as word " + string(i + 1) + "."); 
    }
    else {
        show_message("Word '" + oInput.text + "' not found.");
        check = false;
        break;
    }
}
Thank you very much!
 

TheouAegis

Member
for(var i=0; i<word_num && check; i++)
if word[ i] == oInput.text {
check = false;
show_message("Word '"+oInput.text+"' found as word "+sring(i+1)+".");
}
if i==word_num show_message("Word '"+oInput.text+"' not found.");
 
Top