• Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

Question - Code sprite_width not working?

  • Thread starter I suck at videogames
  • Start date
I

I suck at videogames

Guest
Hello everyone,

I was making Conway's game of life in game maker studio 2, however, if I try to compile it, something weird happens. It says there's an error in line 6 and 7 of my create event in obj_game (the object I use to update the fields on the board), it says
"Object: obj_game Event: Create at line 6: unknown function or script sprite_width"
the same for line seven. My code is at the bottom of this post. I have no idea what's causing this to happen and I'd really appreciate your help!

The create event
Code:
/// @description Setup the object
// You can write your code in this editor

toDrawSprite = spr_square;

spriteWidth = sprite_width(spr_square);
spriteHeight = sprite_height(toDrawSprite);

sizeOfBoard = 10;

//create the board
for (i = 0; i < sizeOfBoard; i += 1) {

    for (j = 0; j < sizeOfBoard; j += 1) {

        global.board[j,i] = false;

    }

}

//make the "nextBoard"
for (i = 0; i < sizeOfBoard; i += 1) {

    for (j = 0; j < sizeOfBoard; j += 1) {

        nextBoard[j,i] = false;

    }

}
The draw event
Code:
/// @description Draw and update the field
// You can write your code in this editor

//draw the current board
for(i = 0; i < sizeOfBoard; i += 1) {

    for (j = 0; j < sizeOfBoard; j += 1) {
 
        currentField = board[j,i];
     
        toDrawX = j * spriteWidth;
        toDrawY = i * spriteHeight;
     
        switch(currentField) {
     
        case true:
            draw_sprite(toDrawSprite, 1, toDrawX, toDrawY);
            break;
        case false:
            draw_sprite(toDrawSprite, 0, toDrawX,toDrawY);
            break;
        default:
            break;
        }
 
    }

}

//update field for next turn;
for (i = 0; i < sizeOfBoard; i += 1) {

    for (j = 0; j < sizeOfBoard; j += 1) {

        currentBoard[i,j] = false;

        currentField = board[j,i];
     
        surroundingFields = 0;
     
        if(board[j-1,i-1]) surroundingFields += 1;
        if(board[j,i-1]) surroundingFields += 1;
        if(board[j+1,i-1]) surroundingFields += 1;
        if(board[j-1,i]) surroundingFields += 1;
        if(board[j+1,i]) surroundingFields += 1;
        if(board[j-1,i+1]) surroundingFields += 1;
        if(board[j,i+1]) surroundingFields += 1;
        if(board[j+1,i+1]) surroundingFields += 1;
     
        if(currentField) {
     
            if(surroundingFields >= 4 /*If field is overpopulated*/ || surroundingFields <= 1 /*If the field dies of solitude*/) nextBoard[j,i] = false;
     
        } else {
     
            if(surroundingFields == 3) nextBoard[j,i] = true;
     
        }

    }

}

board = nextBoard;
 

psyke

Member
Yeah, it's because sprite_width is not a function, it's a variable.

You can use it like this:
Code:
my_object.sprite_width
It will take the width from the current sprite assigned to the object. If you want to get the width from a specific sprite, then yes, you should use sprite_get_width instead.

EDIT: please note that sprite_width will also take into consideration the image_xscale. So if your sprite is 32 pixels wide, and you set the image_xscale to 2, it will return 64 as the width.
 

hippyman

Member
sprite_width also considers scale as well as size. So say you have a 32x32 sprite. If it has a x scale of 0.5 and a y scale of 2, sprite_width will return 16 and sprite_height will return 64.

sprite_get_* functions return actual size in pixels, so with the same scenario above, sprite_get_width and sprite_get_height will return 32x32 no matter what.
 
Top