Legacy GM Check if objects are horizontally aligned [GML]

D

Dennis Lenz

Guest
Hi folks!

Something is baffling me here, and i have no idea why it doesn´t work as expected:

I have four objects on the screen. They´re all of the same type. They are created via drag and drop.
Upon creation i store their ID in an global array, like so:

Code:
gNew_GUI_Object++;
gObject_ID[gNew_GUI_Object] = id;
Now, each GUI Object has a key_press event, to move it pixel-per-pixel up/down/left/right.
Upon moving an GUI object e.g. up, i do this (key up event):

Code:
y--;
if (gNew_GUI_Object > 1)
{
    var c;
    for (c=1; c<gNew_GUI_Object; c++)
    {
        if (gObject_ID[c] != id) // dont check myself
        {
            if (gObject_ID[c].y  == y)
            {
                // draw a damn line to indicate, that this object is aligned with another one. (same y coords).
            }
        }
    }
}
...now my problem. It simply doesn´t do anything! I never see a line :-(
Can anybody help me, what am i doing wrong here?

Thanks!!
 
Last edited by a moderator:

Phil Strahl

Member
Maybe it's because you try to draw the line in the key-up event. Have you tried using a flag that you set to true in the key_up event, e.g.
Code:
if (gObject_ID[c].y == y)
{
   draw_line = true;
   line_x1 = x;
   line_y1 = y;
}
else
{
  draw_line = false;
}
and in the draw event:
Code:
if (draw_line)
{
   draw_line(line_x1, line_x2, ... etc.
}
 
W

Wild_West

Guest
Hi folks!

Something is baffling me here, and i have no idea why it doesn´t work as expected:

I have four objects on the screen. They´re all of the same type. They are created via drag and drop.
Upon creation i store their ID in an global array, like so:

gNew_GUI_Object++;
gObject_ID[gNew_GUI_Object] = id;

Now, each GUI Object has a key_press event, to move it pixel-per-pixel up/down/left/right.
Upon moving an GUI object e.g. up, i do this (key up event):

y--;
if (gNew_GUI_Object > 1)
{
var c;
for (c=1; c<gNew_GUI_Object; c++)
{
if (gObject_ID[c] != id) // dont check myself
{
if (gObject_ID[c].y == y)
{
// draw a damn line to indicate, that this object is aligned with another one. (same y coords).
}
}
}
}

...now my problem. It simply doesn´t do anything! I never see a line :-(
Can anybody help me, what am i doing wrong here?

Thanks!!
The draw_ anything code only works inside a draw event, you need to take that line for drawing a line out of that key press event. If nothing else is flawed that's the only other thing that'd be preventing something being drawn to the screen.t
 
W

Wild_West

Guest
Maybe it's because you try to draw the line in the key-up event. Have you tried using a flag that you set to true in the key_up event, e.g.
Code:
if (gObject_ID[c].y == y)
{
   draw_line = true;
   line_x1 = x;
   line_y1 = y;
}
else
{
  draw_line = false;
}
and in the draw event:
Code:
if (draw_line)
{
   draw_line(line_x1, line_x2, ... etc.
}
You literally posted like a MINUTE before me lmao
 
D

Dennis Lenz

Guest
Maybe it's because you try to draw the line in the key-up event. Have you tried using a flag that you set to true in the key_up event, e.g.
Code:
if (gObject_ID[c].y == y)
{
   draw_line = true;
   line_x1 = x;
   line_y1 = y;
}
else
{
  draw_line = false;
}
and in the draw event:
Code:
if (draw_line)
{
   draw_line(line_x1, line_x2, ... etc.
}
I do set a variable to true, that one is checked in the draw event,yes. And if true -> then draw a line :)
So that´s not the problem here, i´m afraid... could it be that my comparison is wrong somehow? I mean i can access the instances on screen this way, or not?
 
D

Dennis Lenz

Guest
Okay, i think i found the problem. Just checked the id´s in my global array. They are all the same :eek: ... i have to investigate.

EDIT: Yes, it was my fault. I was storing the same ID over and over again in the array. Of course then, it couldn´t work.

Sorry anyone :) Topic can be closed / deleted - whatever !
 
Top