Question about arrays

R

RisingKane

Guest
Hello everyone!

I'm facing some trouble with my codes lately, and I'd like to share it with you and see if you guys can help me.

Code:
var bt_mov_up_x = inst_01E615EC.x, bt_mov_up_y = inst_01E615EC.y;
var bt_mov_down_x = inst_956A28E7.x, bt_mov_down_y = inst_956A28E7.y;
var bt_mov_left_x = inst_719C077E.x, bt_mov_left_y = inst_719C077E.y;
var bt_mov_right_x = inst_2D859A0E.x, bt_mov_right_y = inst_2D859A0E.y;

//UP BUTTON
//Position [1]
if (bt_mov_up_x == 384 && bt_mov_up_y == 256 ) {
    vec_exe[0] = "mov_up";
    empty = false;
}

//Position [2]
else if (bt_mov_up_x == 384 && bt_mov_up_y == 288 ) {
    vec_exe[1] = "mov_up";
    empty = false;
}

//Position [3]
else if (bt_mov_up_x == 384 && bt_mov_up_y == 320 ) {
    vec_exe[2] = "mov_up";
    empty = false;
}

//Position [4]
else if (bt_mov_up_x == 384 && bt_mov_up_y == 352 ) {
    vec_exe[3] = "mov_up";
    empty = false;
}

//DOWN BUTTON
//Position [1]
else if (bt_mov_down_x == 384 && bt_mov_down_y == 256 ) {
    vec_exe[0] = "mov_down";
    empty = false;
}

//Position [2]
else if (bt_mov_down_x == 384 && bt_mov_down_y == 288 ) {
    vec_exe[1] = "mov_down";
    empty = false;
}

//Position [3]
else if (bt_mov_down_x == 384 && bt_mov_down_y == 320 ) {
    vec_exe[2] = "mov_down";
    empty = false;
}

//Position [4]
else if (bt_mov_down_x == 384 && bt_mov_down_y == 352 ) {
    vec_exe[3] = "mov_down";
    empty = false;
}

//LEFT BUTTON
//Position [1]
else if (bt_mov_left_x == 384 && bt_mov_left_y == 256 ) {
    vec_exe[0] = "mov_left";
    empty = false;  
}

//Position [2]
else if (bt_mov_left_x == 384 && bt_mov_left_y == 288 ) {
    vec_exe[1] = "mov_left";
    empty = false;
}

//Position [3]
else if (bt_mov_left_x == 384 && bt_mov_left_y == 320 ) {
    vec_exe[2] = "mov_left";
    empty = false;
}

//Position [4]
else if (bt_mov_left_x == 384 && bt_mov_left_y == 352 ) {
    vec_exe[3] = "mov_left";
    empty = false;
}

//RIGHT BUTTON
//Position [1]
else if (bt_mov_right_x == 384 && bt_mov_right_y == 256 ) {
    vec_exe[0] = "mov_right";
    empty = false;
}

//Position [2]
else if (bt_mov_right_x == 384 && bt_mov_right_y == 288 ) {
    vec_exe[1] = "mov_right";
    empty = false;
}

//Position [3]
else if (bt_mov_right_x == 384 && bt_mov_right_y == 320 ) {
    vec_exe[2] = "mov_right";
    empty = false;
}

//Position [4]
else if (bt_mov_right_x == 384 && bt_mov_right_y == 352 ) {
    vec_exe[3] = "mov_right";
    empty = false;
}
else {
    vec_exe[0] = 0;
    vec_exe[1] = 0;
    vec_exe[2] = 0;
    vec_exe[3] = 0;
   
    empty = true;
}
- In the code above, the bt_mov variables receive the x and y values of the 4 instances of the button object on screen;
- I have 4 positions in my screen, and its corresponding position in my array vec_exe:
[0] 384×256
[1] 384×288
[2] 384×320
[3] 384×352​
- Then, through checking if the position of the buttons equals to one of the positions above, a string value is assigned to the corresponding position of vec_exe.
- If none of the buttons are set in some of the 4 pre-determined positions, the 4 positions of 'vec_exe' are set to 0.
- That code is a step event, from an object called 'obj_area_cod2'

Code:
//Starts the game

var msg = "";

with (obj_area_cod2) {
   
    for (i = 0; i <= array_length_1d(vec_exe) - 1; i += 1) {
        msg += string((vec_exe[i])) + "#";
    }  
}
show_message(msg);
Then, in the code above (thanks to NazGhuL, who helped me), I try to display a message containing the content of all the positions of the vec_exe array. But the problem is that, sometimes it shows the correct string, and sometimes it doesn't, especially when there are two or more elements in the array at the same time. Sorry if it's mess and if I wasn't clear enough.

Anyone knows what could be happening? Thanks!
 
what exactly is this code supposed to be accomplishing? Can you describe what this code is supposed to be doing, instead of describing the manner in which it tries to do it?
 
R

RisingKane

Guest
what exactly is this code supposed to be accomplishing? Can you describe what this code is supposed to be doing, instead of describing the manner in which it tries to do it?
Well, it should show a message box with the content of all the elements from the vec_exe array, but it's not working properly.

To be clearer, on my screen, I have four draggable buttons and something like a table. When a button is dragged and dropped into one of the 4 positions of the table, a certain string value is assigned to one position of the vec_exe array.

table.png

Let's say if I drag up to the position 2, my final message should be:

0
'mov_up'
0
0


Or, if I drag up to 1, left to 2 and down to 4, it should be:

'mov_up'
'mov_left'
0
'mov_down'

And so on...

I'd like the vec_exe array to change dynamically, in real time. As soon as the user attaches or removes a button from one of the positions of the table, the array would be updated, and when the TEST button is pressed, the updated array is shown in the message box. But it doesn't seem to be working correctly.

Sorry if I still wasn't clear enough, and thanks for your support.
 

Nux

GameMaker Staff
GameMaker Dev.
Its not working because you're overusing "else if"; the first array item is found and added to your array, then the entire conditional tree breaks, meaning that no further items will be added.

To fix this, you must change your code to have an "if" instead of an "else if" at each new button. For example:
Code:
//DOWN BUTTON
//Position [1]
// below you can see "else if" has been changed to "if"
if (bt_mov_down_x == 384 && bt_mov_down_y == 256 ) {
   vec_exe[0] = "mov_down";
   empty = false;
}

// then you would continue your code
 
R

RisingKane

Guest
Its not working because you're overusing "else if"; the first array item is found and added to your array, then the entire conditional tree breaks, meaning that no further items will be added.

To fix this, you must change your code to have an "if" instead of an "else if" at each new button. For example:
Code:
//DOWN BUTTON
//Position [1]
// below you can see "else if" has been changed to "if"
if (bt_mov_down_x == 384 && bt_mov_down_y == 256 ) {
   vec_exe[0] = "mov_down";
   empty = false;
}

// then you would continue your code
It worked out very nice, thanks a lot Nux! The only extra thing I did, was to initialize all the elements in vec_exe with 0 as their value. Now the message is showing all the correct elements and it's being updated properly. Thanks again!
 
Top