GameMaker Macros somehow affecting each other? [Solved]

K3fka

Member
Hey, everyone. I'm having a pretty strange issue involving macros. I want to have four macros: tlx, tly, brx, and bry, which would correspond to top-left and bottom-right coordinates of a sprite (given that the origin is centered).

I'm finding that for the purpose of collisions, I want to subtract 1 from each of these values. Here's where the problem lies. When I subtract 1 from my brx and bry macros, things work well. Here's a screenshot (I'm drawing a rectangle with these macros as the coordinates to demonstrate).

You can see, the right side of the rectangle matches up perfectly with the end of the wall, which is what I want.

However, when I add the subtraction to the other macros as well...

Not only is the left side of the rectangle not moving, but it seems to be affecting the brx macro as well somehow.

Here's my macro code:
Code:
#macro tlx (x - (abs(sprite_width) / 2) - 1)
#macro tly (y - (abs(sprite_height) / 2) - 1)
#macro brx (x + (abs(sprite_width) / 2) - 1)
#macro bry (y + (abs(sprite_height) / 2) - 1)
Here's my rectangle drawing code:
Code:
draw_self();
draw_set_color(c_green);
draw_rectangle(tlx, tly, brx, bry, true);
I appreciate any help you guys can offer me.
 

xDGameStudios

GameMaker Staff
GameMaker Dev.
if you want to reduce the square by one pixel (1px border)

the brx and the bry... need to be decreased by one (-1) like you did... but the tlx and the tly need to be added 1 ;)
not sure if that is the problem though!!

EDIT:: this can also happen if your sprite as a odd size... and you shouldn't need "abs".. because the size is always positive! you should be using "sprite_width div 2" so it divides and rounds!
 

K3fka

Member
if you want to reduce the square by one pixel (1px border)

the brx and the bry... need to be decreased by one (-1) like you did... but the tlx and the tly need to be added 1 ;)
not sure if that is the problem though!!

EDIT:: this can also happen if your sprite as a odd size... and you shouldn't need "abs".. because the size is always positive! you should be using "sprite_width div 2" so it divides and rounds!
Appreciate the help, but I want to shift the collision rectangle over to the left and up by one pixel, so I'm subtracting one. The sprite is 24x24 so decimals won't become an issue. And the abs() is due to use of image_xscale (set it to -1 to x-flip and your sprite_width becomes negative).

You can't judge how things are actually being checked using draw rectangle as it is inherently flawed, especially when scaling up a pixel art game. (see here: https://forum.yoyogames.com/index.php?threads/drawing-lines-not-recommended.18246/#post-116669). Create a 1px sprite and then draw it scaled using draw_sprite_ext() and the x/yscale values instead.
After more experimentation, it goes further than a drawing issue. Here's me writing the values of tlx, brx, tly, and bry to the screen.


tlx - brx
tly - bry

As I would expect, tlx is equal to (36 - (24 / 2)) the player's x position minus half of the sprite_width. brx is of course equal to (36 + (24 / 2) - 1).

Now when I add my -1's in to the tl macros...

And we can clearly see that not only is the tlx macro completely unaffected, but the brx macro has now increased. No other code has changed between these two screenshots.
 

K3fka

Member
After a bit more digging, I've realized this has been caused by the use of the macros for collision as well. Changing the values didn't allow me to move as close to the walls as before, which of course affected the numbers. This is solved now.
 
Top