Help with 8 Directions

Whoa... this is overly complicated! :D

//CREATE
spd=2;

// STEP    
horizontal = keyboard_check(ord('D'))-keyboard_check(ord('A'));   // Will return 1,0,-1 appropriately
vertical = keyboard_check(ord('S'))-keyboard_check(ord('W'));      // same thing
hspd=horizontal*spd;                                                                 // apply speeds
vspd=vertical *spd;
// this is where a collision code would go
x+=hspd;                                                                                  //set new position
y+=vspd;
if (horizontal!=0)||(vertical!=0) {                                               // if input is detected
    image_angle=point_direction(0,0,horizontal,vertical);}            // gets 45° angle

I hope I didnt make any mistakes writing all that code from memory... To elaborate on the first part: Keyboard_check will simply return 1 if pressed (true) or 0 (false). Thus we can simplify the first couple of lines! Next the horizontal and vertical speeds are set, depending on whether an input in that direction was detected. Now we can do collisions, and afterwards the instance may finally move.

The last line of code: If there is new input, so if either is NOT zero, THEN update the image_angle. Otherwise the image_angle would always snap back to 0 => facing right when we stand still/AFK. As we are using horizontal and vertical the image will face the way we are 'pressing buttons' regardless of whether our collision code told us that our hspd must now be zero. As both values may only contain [-1,0,1] the resulting angle is guaranteed to be a valid 45° angle.

/r/gamemaker Thread