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

GML Can't draw sprite in Draw GUI Event

V

Vitalii Lukyanov

Guest
Hello, I'm trying to draw a panel with health(hearts) but nothing shows up.
Here's my Draw Gui Event(for panel object):
for (i = 0; i < player.life; i++) {
coord = global.hearts_coords;
draw_sprite(heart, -1, x + coord[0], y + coord[1]);
}
Every variable was checked with debugging and they are not undefined and contain values that I needed them to contain. But draw_sprite() seems to do nothing.
Thanks in advance!
 
V

Vitalii Lukyanov

Guest
draw_sprite(sprite, subimg, x, y);
subimg can't be a negative number
Well, I tried making it 100 just in case and there was no difference. Plus, in documentation it says this:
subimg |The sub-image (frame) of the sprite to draw (image_index or -1 correlate to the current frame of animation in the object).
 

marasovec

Member
The number has to be between 0 and number of frames (images) in the sprite you are using
EDIT: Is the object visible?
 

marasovec

Member
Wait you are drawing it inside GUI right? So it should look like this
Code:
draw_sprite(heart, -1, coord[0], coord[1]);
not like this
draw_sprite(heart, -1, x + coord[0], y + coord[1]);
 
V

Vitalii Lukyanov

Guest
Wait you are drawing it inside GUI right? So it should look like this
Code:
draw_sprite(heart, -1, coord[0], coord[1]);
not like this
draw_sprite(heart, -1, x + coord[0], y + coord[1]);
well, now it shows up in the corner of the screen
 

PlayerOne

Member
If my memory is correct you dont need to add x+ or y+ to the argument when drawing in the gui. Only a number will suffice.

...and I got ninja'd while typing this out. :p
 

marasovec

Member
Yes bacause Draw event and Draw GUI event are diffetent. GUI is drawn using coordinates in view instead of in room
EDIT: So if the coords are higher than the view size or negative it draws the sprite outside the view
 
V

Vitalii Lukyanov

Guest
Yes bacause Draw event and Draw GUI event are diffetent. GUI is drawn using coordinates in view instead of in room
Ooooh, but, how can I draw sprite on the panel itself? It's just panels are not in one place everytime and I don't want to make programmers nightmare in my code.
EDIT: 💩💩💩💩, I forgot to tell that these coords are in panel, I mean that I consider the left upper corner of panel as (0,0)
 
V

Vitalii Lukyanov

Guest
You have to factor the gui size when drawing. The image above shows that if you have 1280 x 720 gui size and you need to draw it in the lower left hand corner of the window you would have to set the draw setting to draw_sprite(sprite,index,1900,650).
Ehm, I don't understand.
 
The gui layer will auto resize itself to fit the first game window, if you're not touching it in code. To draw on the gui layer, the top left of the screen is always 0,0 and the bottom right is always the width,height of your gui layer. This is different to the normal draw event. As an example, let's say your room width and height are 5000 by 5000 and your camera width is 1280 by 720. You position your camera so it is in the center of your room, and you want to draw a sprite in the normal draw layer in the center of the camera. That code would be x = room_width/2 and y = room_height/2. However, the code for that in the gui layer would be x = display_get_gui_width()/2 and y = display_get_gui_height()/2.

Now lets move the camera so that it's in the top left corner of the room. You won't be able to see the sprite you're drawing in the normal draw layer anymore as room_width/2 (5000/2=2500 in this example) is to the right of the camera display (remember the camera width is only 1280). Same goes for the y coord. However, using x = display_get_gui_width()/2 and y = display_get_gui_height()/2 in the gui layer will mean that the sprite being drawn in the gui layer is -always- in the center of your camera, regardless of where the camera is positioned or the size of the room.

If you draw something at x = 0 and y = 0 in the gui layer, it will ALWAYS be shown in the top left corner, if you draw something at x = display_get_gui_width() and y = display_get_gui_height(), it will ALWAYS be drawn in the bottom right of the screen, if you draw something at x = display_get_gui_width()+100, it will NEVER show because it is outside the bounds of the gui layer. Basically, the gui layer is completely separate to both your camera and your room coordinates, it won't move with your camera and you can always treat 0,0 as the top left and display_get_etc as the bottom right, so you just have to figure out what the actual screen coordinates would be for where you want to draw and then simply draw there, don't bother trying to adjust for where the camera is in the room.
 
V

Vitalii Lukyanov

Guest
The gui layer will auto resize itself to fit the first game window, if you're not touching it in code. To draw on the gui layer, the top left of the screen is always 0,0 and the bottom right is always the width,height of your gui layer. This is different to the normal draw event. As an example, let's say your room width and height are 5000 by 5000 and your camera width is 1280 by 720. You position your camera so it is in the center of your room, and you want to draw a sprite in the normal draw layer in the center of the camera. That code would be x = room_width/2 and y = room_height/2. However, the code for that in the gui layer would be x = display_get_gui_width()/2 and y = display_get_gui_height()/2.

Now lets move the camera so that it's in the top left corner of the room. You won't be able to see the sprite you're drawing in the normal draw layer anymore as room_width/2 (5000/2=2500 in this example) is to the right of the camera display (remember the camera width is only 1280). However, using x = display_get_gui_width()/2 and y = display_get_gui_height()/2 in the gui layer will mean that the sprite being drawn in the gui layer is -always- in the center of your camera, regardless of where the camera is positioned or the size of the room.

If you draw something at x = 0 and y = 0 in the gui layer, it will ALWAYS be shown in the top left corner, if you draw something at x = display_get_gui_width() and y = display_get_gui_height(), it will ALWAYS be drawn in the bottom right of the screen, if you draw something at x = display_get_gui_width()+100, it will NEVER show because it is outside the bounds of the gui layer. Basically, the gui layer is completely separate to both your camera and your room coordinates, it won't move with your camera and you can always treat 0,0 as the top left and display_get_etc as the bottom right, so you just have to figure out what the actual screen coordinates would be for where you want to draw and then simply draw there, don't bother trying to adjust for where the camera is in the room.
Thank you! Now I understand how this works.
 
Top