SOLVED Get X POSITION of sprite

DGNNY

Member
Henlo
on my draw GUI event, I draw an icon for an ability and draw text over it to represent the cooldown
i want the position of the text to be relative to the position of the icon, but i don't know how to get the x position of it.
GML:
sprite_get_xoffset
returns 0 because it only gives the position of the origin according to the sprite.
does anyone know how i can get the position of the icon sprite?
GML:
draw_sprite(shotgunicon,0,607,1);
shotgunxpos = sprite_get_xoffset(shotgunicon)       
if (shotgunCD > 0)
{
    draw_text(shotgunxpos,1,round(shotgunCD / 60));
}
this is the code i am using
 
Last edited:

samspade

Member
If your sprite is attached to an object, then the x and y of the object is the x and y origin point of the sprite (in room space). If it is just a random sprite (e.g. not connected to any object) then it is just the x and y you are drawing it at.
 

Nidoking

Member
In what way are you drawing the icon? You must be providing an x and y coordinate to the draw instruction somehow.
 

DGNNY

Member
In what way are you drawing the icon? You must be providing an x and y coordinate to the draw instruction somehow.
GML:
draw_sprite(shotgunicon,0,607,1);
If your sprite is attached to an object, then the x and y of the object is the x and y origin point of the sprite (in room space). If it is just a random sprite (e.g. not connected to any object) then it is just the x and y you are drawing it at.
its not attached to anything, its on a draw gui event
i want to know if i can set a variable to be the x position of the sprite, so i can make the x position of the text relative to that variable, instead a number that wouldn't change if the shotgun icon moved.
 

samspade

Member
its not attached to anything, its on a draw gui event
You can have a sprite attached to an object and draw it to the GUI. It's my preferred method. But you don't need to.

i want to know if i can set a variable to be the x position of the sprite, so i can make the x position of the text relative to that variable, instead a number that wouldn't change if the shotgun icon moved.
Yes. In fact it is why I prefer to have them attached to objects because then you get the x and y for free, but you can also just create your own variables such as my_x, my_y and then use them.
 

DGNNY

Member
You can have a sprite attached to an object and draw it to the GUI. It's my preferred method. But you don't need to.
Yes. In fact it is why I prefer to have them attached to objects because then you get the x and y for free, but you can also just create your own variables such as my_x, my_y and then use them.
i'm still kinda new to this so it took me a bit, but i appreciate your help and i think i got it
i made an object with x position relative to the camera, and drew a sprite over the object on the GUI layer
if thats what you were saying to do, thanks a bunch!
 

samspade

Member
i'm still kinda new to this so it took me a bit, but i appreciate your help and i think i got it
i made an object with x position relative to the camera, and drew a sprite over the object on the GUI layer
if thats what you were saying to do, thanks a bunch!
It sounds like it is one of the two main ways-draw a sprite using room position relative to the camera. The other being draw a sprite directly in the gui layer. I prefer the second, but both are really common and work just fine.
 
Top