• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Legacy GM [SOLVED] "for" statement for arrays malfunction

E

Edwin

Guest
Hello, fellas.

I have a small problem with using “for” statement to set all the arrays to 0 by subtracting them while they are not smaller or equal to 0. So when all my arrays are smaller or equal to 0, the object needs to be destroyed. But it does not work as I expected: object is destroying only when one of these arrays are smaller or equal to 0 (may be another reason for this).

Here is my code:

object0:

Create event:
Code:
flag = false;

array_string[0] = "Hello.";
array_string[1] = "How are you?";
array_string[2] = "I hope you feel okay right now.";

array_alpha = array_create(array_length_1d(array_string));

for (var i = 0; i < array_length_1d(array_string); i++) {
    array_alpha[i] = 1;
}
Step event:
Code:
if (!flag) {
    if (keyboard_check_pressed(vk_enter)) {
        flag = !flag;
        array_alpha[0] = .5;
    }
} else
if (flag) {
    for (var i = 0; i < array_length_1d(array_string); i++) {
        if (array_alpha[i] > 0) {
            array_alpha[i] -= .01;
        } else {
            instance_destroy(id);
        }
    }
}
Draw event:
Code:
for (var i = 0; i < array_length_1d(array_string); i++) {
    draw_set_alpha(array_alpha[i]);
    draw_text(x, y+(i*24), string(array_string[i]));
    draw_set_alpha(1);
}
If you will compile this project, after pressing enter key you will see that first string disappears and destroying, without waiting for others.

Ask me anything you want to. I will be appreciated by any help.
 

samspade

Member
Hello, fellas.

I have a small problem with using “for” statement to set all the arrays to 0 by subtracting them while they are not smaller or equal to 0. So when all my arrays are smaller or equal to 0, the object needs to be destroyed. But it does not work as I expected: object is destroying only when one of these arrays are smaller or equal to 0 (may be another reason for this).

Here is my code:

object0:

Create event:
Code:
flag = false;

array_string[0] = "Hello.";
array_string[1] = "How are you?";
array_string[2] = "I hope you feel okay right now.";

array_alpha = array_create(array_length_1d(array_string));

for (var i = 0; i < array_length_1d(array_string); i++) {
    array_alpha[i] = 1;
}
Step event:
Code:
if (!flag) {
    if (keyboard_check_pressed(vk_enter)) {
        flag = !flag;
        array_alpha[0] = .5;
    }
} else
if (flag) {
    for (var i = 0; i < array_length_1d(array_string); i++) {
        if (array_alpha[i] > 0) {
            array_alpha[i] -= .01;
        } else {
            instance_destroy(id);
        }
    }
}
Draw event:
Code:
for (var i = 0; i < array_length_1d(array_string); i++) {
    draw_set_alpha(array_alpha[i]);
    draw_text(x, y+(i*24), string(array_string[i]));
    draw_set_alpha(1);
}
If you will compile this project, after pressing enter key you will see that first string disappears and destroying, without waiting for others.

Ask me anything you want to. I will be appreciated by any help.
A small note, array_create allows you to set a starting value. In your case you could create and set the alpha array in one line by saying
array_alpha = array_create(array_length_1d(array_string), 1);

However to your primary question, yes, you are telling it to loop through and if even one value is equal to or below 0, to destroy the entire thing. You can see this in your if statement inside the for loop which just says if the value is not greater than zero, destroy the object.

One way to resolve this would be to check whether all values in the array are 0 in a second for loop like this:

Code:
var all_values_false = true;
for (var i = 0; i < array_length_1d(array_string); i++) {
    if (array_alpha[i] > 0) {
        all_values_false = false;
        break;
    }
}
if (all_values_false == true) {
    instance_destroy(id);
}

It might be helpful to read this: with Block Recipe Cards. All of the examples there apply equally to for loops.
 
E

Edwin

Guest
A small note, array_create allows you to set a starting value. In your case you could create and set the alpha array in one line by saying
array_alpha = array_create(array_length_1d(array_string), 1);
Unfortunately, this feature only works on GameMaker: Studio with version 2 and beyond. On older versions, it does not exist at all.

Anyway, thank you for your help. But for some reason, the text disappears in turn, not immediately. How you can see in my code, I set first "array_alpha" to .5 when you press enter key to make it disappear a little faster than others. I made this for checking does it destroying or not.

If you know how to fix it, I would be very happy to find out how. Thanks again.


Edit:
I fixed it by deleting break in "for" loop.
Code:
if (flag) {
    var all_values_false = true;
    for (var i = 0; i < array_length_1d(array_string); i++) {
        if (array_alpha[i] > 0) {
            array_alpha[i] -= .01;
            all_values_false = false;
        }
    }
    if (all_values_false == true) {
        instance_destroy(id);
    }
}
 
Last edited:

samspade

Member
Unfortunately, this feature only works on GameMaker: Studio with version 2 and beyond. On older versions, it does not exist at all.

Anyway, thank you for your help. But for some reason, the text disappears in turn, not immediately. How you can see in my code, I set first "array_alpha" to .5 when you press enter key to make it disappear a little faster than others. I made this for checking does it destroying or not.

If you know how to fix it, I would be very happy to find out how. Thanks again.


Edit:
I fixed it by deleting break in "for" loop.
Code:
if (flag) {
    var all_values_false = true;
    for (var i = 0; i < array_length_1d(array_string); i++) {
        if (array_alpha[i] > 0) {
            array_alpha[i] -= .01;
            all_values_false = false;
        }
    }
    if (all_values_false == true) {
        instance_destroy(id);
    }
}
array_create is listed in the documentation for GMS 1.4 and supposedly it allows default variables, although I guess the documentation could be wrong,

https://docs.yoyogames.com/source/d...e/001_gml language overview/array_create.html
 
Top