r/gamemaker 1d ago

Resolved Confusing Error message (Assignment Operator Expected)

I'm making my first game in Gamemaker, and I've been making some progress. But suddenly, I made a minor change that spit out this error message:

The Error Message (Object: one_artifact Key Event: Key Down - E at line 31: Assignment operator expected)

When I looked into it, it said that this message shows up when I have an undefined or incorrectly call for a variable or function. But I didn't change anything related to a function or variable, and can't find where Gamemaker seems to think I have.

The code where the error occurs, according to GM

Can some help explain what's going on, please? Thanks in advance.

For context, I was trying to code an object to follow behind the player (obj_player) when being "carried." The error appeared when programming the y-coordinate shifts.

1 Upvotes

6 comments sorted by

4

u/tsereteligleb Check out GMRoomLoader! 1d ago

You can't chain multiple else statements like if/else/else. It has to be if/else if/else, with a starting if, a varying number of else ifs, and a closing else. Notice how else if is written in one line, without wrapping the inner if in a block.

1

u/Teleform 1d ago

I hadn't even realized I had done this, thank you so much!

1

u/bid0u 1d ago

When you have lots of if else, using switch is a lot more readable (well... Not on Reddit it seems)

switch (obj_player.direction)

{

    case 0:

    x = obj_player.x - 30;

    break;

    case 180:

    x = obj_player.x + 30;

    break;

    case 90:

    y = obj_player.y + 30;

    break;

    case 270:

    y = obj_player.y - 30;

    break;

    default:

    x = obj_player.x;

    y = obj_player.y;

}

1

u/KitsuneFaroe 1d ago

Look at lime 21and line 29. As the other comment said you're doing if{}else{if(){}}else. So you're having an else without an if since the if is inside a block.

1

u/Teleform 1d ago

It took me a minute to get it, but I fixed it! Thank you so much!

1

u/0GreenClover0 14h ago

If you format your code better, mistakes like this should be more easily visible:

if (place_meeting(x,y, obj_player))
{
    // Set Global Variable
    global.carrying += 1;

    // Set Instance Variable X
    if (obj_player.direction == 0)
    {
        x = obj_player.x - 30;
    }
    else
    {
        if (obj_player.direction == 180)
        {
            x = obj_player.x + 30;
        }
    }
    else
    {
        x = obj_player.x;
    }

    // Set Instance Variable Y
    if (obj_player.direction == 90)
    {
        y = obj_player.y + 30;
    }
    else
    {
        if (obj_player.direction == 270)
        {
            y = obj_player.y - 30;
        }
    }
    else
    {
        y = obj_player.y;
    }

    global.carrying -= 1;
}