game-developmentgame-makergml

Unexpected syntax error when using Macro in conditional statement gml


I am very new to Gamemaker, and I am trying to make a simple space invader game. In the player object's step event, I am trying to check for collisions with the left and right sides of the camera. I know this might not be the best way to do this, but I ran into a strange problem while trying to use a Macro variable I defined in the create event. The code below gives me a compile error: "unexpected syntax error" only when I use the macro value in the expression.

collisionLeft = ((x - sprite_width/2) - MOVE_SPEED < 0) ? true : false;
collisionRight = ((x + sprite_width/2) + MOVE_SPEED > camera_get_view_width(view_camera[0])) ? true : false;

I have tried swapping out the MOVE_SPEED macro for a number literal and it seems to accept this. I have also tried to storing the left side of the conditional statement in a variable like the code below. The compiler seems to accept this:

var leftArg = (x - sprite_width/2) - MOVE_SPEED;
var rightArg = (x + sprite_width/2) + MOVE_SPEED;

collisionLeft = (leftArg < 0) ? true : false;
collisionRight = (rightArg > camera_get_view_width(view_camera[0])) ? true : false;

I realize there are easy workarounds for this but maybe my understanding of how macros/conditionals work in gml is flawed so any help would be appreciated! Again I am very new to Gamemaker I literally just started yesterday lol.


Solution

  • Make sure not to include a ; in your macros - the contents are copied (almost) verbatim, so if you wrote

    #macro MOVE_SPEED 3;
    var a = MOVE_SPEED + 1;
    

    that would be the same as

    var a = 3; + 1;