• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker Placing game piece in relation to game board

S

skaiiwalker

Guest
I'm trying to build a board based game where you move pieces around on a board. I want the piece to be locked to a grid based on the board. In the current version of the game, it crashed as soon as it loads up. Here's the error:

___________________________________________
############################################################################################
ERROR in
action number 1
of Alarm Event for alarm 1
for object obj_gamePiece:

Variable obj_gameBoard.col(100133, -2147483648) not set before reading it.
at gml_Object_obj_gamePiece_Alarm_1 (line 3) - self.x = obj_gameBoard.x + 25 + (50 * self.col);
############################################################################################
gml_Object_obj_gamePiece_Alarm_1 (line 3)
___________________________________________


Here's the code in the gamepiece object that is breaking for some reason:
GML:
// create event
row = 5;
col = 0;

// step event
if (!global.isPaused)
{
    if (keyboard_check_released(ord("A")))
    {
        col--;
    }
   
    if (keyboard_check_released(ord("D")))
    {
        col++;
    }
   
    if (keyboard_check_released(ord("W")))
    {
        row--;
    }
   
    if (keyboard_check_released(ord("S")))
    {
        row++;
    }
}

alarm[1] = 1;


// alarm 1 event
// this code kept getting angry at me for trying to read from the gameboard
// object before it was created, so I moved it into alarm 1

with (obj_gameBoard)
{
    self.x = obj_gameBoard.x + 25 + (50 * self.col);
    self.y = obj_gameBoard.y + 25 + (50 * self.row);
}
So the code really doesn't like my trying to call its own variables instead of gameboard variables in the with section. I don't know why that is. Please help me understand how to fix this
 

TailBit

Member
obj_gameBoard is running the code in the with() .. so no need for obj_gameBoard.x .. x alone will do .. to refer to the gamepiece you will have to use other instead of self
 

curato

Member
make sure all instances and any objects you might be creating exist prior to that line. Also, I never really tried using self in that particular case. Usually I would use other in a situation like that.
 

NightFrost

Member
You are using with, so the bracketed code will run in the scope of gameboard instance. Well, all of them, but assumedly you have only one and no more are created. This means prepending variables with obj_gameBoard is unnecessary because that's already the scope (and potential source of bugs because in case of multiple instances.) Likewise, self also means obj_gameObject instance here because of the with so you should instead be using other to refer back to scope that initiated the scope switch. In general I would avoid using self, and if you need the id, simply use id.
 
Top