ID:1465344
 
(See the best response by Ss4toby.)
Code:
while(bounds_dist(src, target) > X)
if(step_towards(src, target)) continue


Problem description:
I'm trying to move src to be X pixels from the target. The issue is, it doesn't align the object with the target. Is there a way to capture the alignment of objects?

I'm currently concocting a rather complicated step control system using bounds() but I was hoping there'd be an easier way.
Consider the objects X and O.

+++++++++++++
+X+++++++++++
+++++++++++++
++O++++++++++
+++++++++++++

I want to move X so that it aligns with O's bounding box from any given direction. Suppose that direction is NORTH. Using my code, I'll get:

+++++++++++++
+++++++++++++
+X+++++++++++
++O++++++++++
+++++++++++++

Because it moves X towards O, and their bounding boxes are 1 pixel apart. I used an alternative, using get_dir() for direction, and it still produces similar results. The desired result it:

+++++++++++++
+++++++++++++
++X++++++++++
++O++++++++++
+++++++++++++
Best response
You would need to calculate x and y differences when in range.

mob/Click()
while(src)
if(bounds_dist(src, usr)<=1)//If only 1 pixel away or less
var/xDiff=((usr.x*32)+usr.step_x)-((x*32)+step_x)//calculate x difference in pixels
var/yDiff=((usr.y*32)+usr.step_y)-((y*32)+step_y)//calculate y difference in pixels
if(xDiff)
if(xDiff>0)
step(src,EAST)
else
step(src,WEST)
else
break//if no x difference, then they must be aligned
if(yDiff)
if(yDiff>0)
step(src,NORTH)
else
step(src,SOUTH)
else
break//if no y difference, then they must be aligned
else
step_towards(src,usr)
sleep(1)


*Edit*

In my example, usr would represent the 'target' variable you use.
Definitely help. I had no idea pixel_x was a variable. But now I have a new issue.

    BoundAlign(var/atom/movable/Ref, var/atom/movable/Trg, var/Side, var/x_dist = 0, var/y_dist = 0)
/* This function should align Ref to Target. Side is either "Left", "Right", "Top", "Bottom". x_dist is the
number of pixels in the x-direction from the bottom of Trg's bounding box you want to move Ref to. y_dist
is the same thing for y. By default, this code should try to move Ref to align with Trg's bottom left corner.*/


var/xDiff = ((Ref.x * 32) + (Ref.step_x)) - ((Trg.x * 32) + (Trg.step_x))
var/yDiff = ((Ref.y * 32) + (Ref.step_y)) - ((Trg.y * 32) + (Trg.step_y))
switch(Side)
if("Top")
while(xDiff != x_dist)
if(xDiff < 0)
if(step(Ref, EAST, 1))
xDiff = ((Ref.x * 32) + (Ref.step_x)) - ((Trg.x * 32) + (Trg.step_x))
else break
else if(xDiff > x_dist)
if(step(Ref, WEST, 1))
xDiff = ((Ref.x * 32) + (Ref.step_x)) - ((Trg.x * 32) + (Trg.step_x))
else break
while(yDiff != y_dist)
if(yDiff > 0)
if(step(Ref, SOUTH, 1))
yDiff = ((Ref.y * 32) + (Ref.step_y)) - ((Trg.y * 32) + (Trg.step_y))
else break
else if(yDiff < y_dist)
if(step(Ref, NORTH, 1))
yDiff = ((Ref.y * 32) + (Ref.step_y)) - ((Trg.y * 32) + (Trg.step_y))
else break


Calling this with proper arguments aligns x but not y. I figured the if(step()) else break would still work if it was unable to move. I'm guessing that for some reason step(ref, SOUTH) is returning 0 but it shouldn't since Ref has 0 density.
You can use
 if(src.dir==get_dir(src,target))


All clear, I missed 2 statements, I'm just stupid. Thanks for everything guys.