• 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!

Having troubles referencing another object within an object

M

MegaSkeleton

Guest
Basically when I initialize my game board, I create a ds_grid and create an object for each grid in that cell:

Code:
gameCell = ds_grid_create(10,10);

//other stuff

var xx, yy, i, j;
xx = boardLayout.x;
for (i = 0; i < 10; i += 1)
{
    yy = boardLayout.y;
    for (j = 0; j < 10; j += 1)
    {
        // Create the instance, accounting for the disparity in cell width/height and the size of the O/X sprites
        gameCell[# i,j] = instance_create(xx + widthOffset, yy + heightOffset, objBoardCell);
      
        yy += boardLayout.cellHeight;
    }
    xx += boardLayout.cellWidth;
}
Later when I do checks of what boardCell the mouse is over when it releases a click,
Code:
var xx, yy, i, j;
xx = objGameBoard.boardLayout.x;
for (i = 0; i < 10; i += 1)
{
    yy = objGameBoard.boardLayout.y;
    for (j = 0; j < 10; j += 1)
    {
        // Check to see if the mouse is within this location      
        if ((mouse_x >= xx) && (mouse_x < (xx + objGameBoard.boardLayout.cellWidth)) &&
            (mouse_y >= yy) && (mouse_y < (yy + objGameBoard.boardLayout.cellHeight)))
        {      
            // Check the cell at this location
            if ((!gameCell[# i, j].occupied) ||

I get a var not set error:

Mouse Event Glob Left Released for objGameBoard
Variable objGameBoard.gameCell not set before reading it.

When I hardcode if ((!gameCell[# i, j].occupied) || to an object ID like (100003).occupied it works fine, it's clearly by the error trying to reference a variable in the objGameBoard instead of using the gameCells I initialize. How can I programatically refer to it?
 
S

sndfnts

Guest
In game maker, you may have to use the with keyword to access variables from another instance.
Code:
var xx, var yy                 //local to script
inst_v = 0                 //local to current instance
with (another_id) {         // in the following block, change scope to another_id
    xx = x               //sets xx (local to script) to to other_id.x
        if (occupied == true)        // checks if another_id.occupied is true
            other.inst_v = 1     //need to use other to access current instance variable
}
The with keyword lets you write code that is in the other instance's scope while retaining access to your local variables (xx, yy). If you want to access the calling instance's variables (inst_v), then you can refer by using "other"
 

FrostyCat

Redemption Seeker
I don't see boardLayout anywhere in objGameBoard's Create event. If it is declared with var, get rid of the var so that it'll last.
 
M

MegaSkeleton

Guest
Thanks for the replies. When I introduce a
Code:
with(gameCell[# i, j]){
                if ((!gameCell[# i, j].occupied) ||
Outside of the if statement, I still get the same error, (objGameBoard.gameCell not set)....but what the heck? I'm not asking for an objGameBoard var, isn't that what with statement is for? How can I reference outside of it if it keeps assuming so. Is it because it's within another if statement referring to the objGameBoard object?

regarding boardLayout, I omitted a lot to get to the parts that were erroring out.
 
M

MegaSkeleton

Guest
Is the solution to define all the ids of the gamecells and find a way to iterate through them appropriately? I'm trying to think of the implications in the other scripts I'm trying to get up and running again (it's an old project), that include some fairly hefty board cell checks because I use logic for eliminating pieces similar to Go, and am constantly referencing adjacent cells.
 
Top