• 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 trouble getting a sprite's width/height

B

bdonohue

Guest
Hi,

I'm trying to reference the width/height of one sprite from within another object. I though I could use this but it's not working:

myWidth = sprite_get_width(mySpriteName);

This returns a number that is definitely not the width of the sprite. What am I doing wrong?

Also, is it possible in GML to access properties of objects using standard dot notation like in other languages? Eg;

myWidth = mySpriteName.width

Thanks!
 
  • Wow
Reactions: Mut
W

whale_cancer

Guest
Hi,

I'm trying to reference the width/height of one sprite from within another object. I though I could use this but it's not working:

myWidth = sprite_get_width(mySpriteName);
This returns a number that is definitely not the width of the sprite. What am I doing wrong?
Can you post the sprite somewhere? I see absolutely no reason this shouldn't work and thus assume you are referencing the sprite incorrectly.

Edit: Re: Second question. You can reference instances like that, but not objects or sprites (AFAIK).
 

obscene

Member
sprite_get_width() is the correct way, but you have to feed it an index instead of a string so make sure you are doing that.

Correct: sprite_get_width(mySpriteName);
Correct: sprite_get_width(object_something.sprite_index);
Incorrect: sprite_get_width("mySpriteName");

You can also do ...

object_something.sprite_width or instance_id.sprite_width but those require an activea instance of the object to be in the room.
 
W

whale_cancer

Guest
object_something.sprite_width or instance_id.sprite_width but those require an activea instance of the object to be in the room.
Should also be noted that if you reference an object like that, it will use the first placed instance of that object. This can be relevant if the same object can have different sprites at the same time.
 
B

bdonohue

Guest
Ok, I figured it out. It seems I'm getting "sprites" and "objects" confused. More correctly, I was thinking they were the same thing. So, I had used an object name for sprite_get_width() instead of a sprite name. Doh.

Thank you for the feedback... always much appreciated :)
 
Top