GMS 2.3+ GUI Problems

So I'm trying to make it so there are button icons on the screen while my game is being played and always stay in the same place while my ship is moving. I've tried creating it numerous different ways and I'm just not getting it to work. I'm doing instance_create_layer(x + 10, y + 390, "Instances", Object16);

Maybe I'm just not putting it in the right place. My guess would have been draw or end draw.
idk

Please help.
 

TsukaYuriko

☄️
Forum Staff
Moderator
This has less to do with creation and more to do with how you're drawing it to the screen. Your guess is pretty spot-on.

Generally, if this is a user interface element, you'll probably want to draw it in the Draw GUI event. Unlike the regular Draw events, it is not relative to the room coordinates, and as such will not move alongside the rest of the room when the camera's position changes. To quote the manual:
When drawing in these event it is important that you understand that the draw coordinates do not change even when camera views are active and (0,0) is always the top left hand corner of either the application surface or the display (see the note at the bottom of this section), and the default width and height are 1:1 with the application surface.
 
This has less to do with creation and more to do with how you're drawing it to the screen. Your guess is pretty spot-on.

Generally, if this is a user interface element, you'll probably want to draw it in the Draw GUI event. Unlike the regular Draw events, it is not relative to the room coordinates, and as such will not move alongside the rest of the room when the camera's position changes. To quote the manual:
So when I put it in the Draw GUI event it's creating another one for every step it seems and making the button flare around the view as if it has a really long tail. Not sure why. Seems like this would happen if you were to draw this in the step event.
 

TsukaYuriko

☄️
Forum Staff
Moderator
Don't create it in the Draw GUI event. Only draw it. Create it somewhere else - wherever you were creating it before - exactly once.
 

Tyg

Member
yes thats what the create event is for..the draw event keeps triggering like the step event so you are endlessly creating instances and will eventually crash..wouldnt it be easier to create your object..drop it on the screen and turn it visible flag off when not need to be drawn? by default the object automatically draws the sprite attached to it you dont even need a draw event..unless your doing a 3d game then you use draw_gui and have to set stuff like blendable but still there is no need to keep creating instances ..hope that helps..just drag and drop your button onto the screen and leave it
 
yes thats what the create event is for..the draw event keeps triggering like the step event so you are endlessly creating instances and will eventually crash..wouldnt it be easier to create your object..drop it on the screen and turn it visible flag off when not need to be drawn? by default the object automatically draws the sprite attached to it you dont even need a draw event..unless your doing a 3d game then you use draw_gui and have to set stuff like blendable but still there is no need to keep creating instances ..hope that helps..just drag and drop your button onto the screen and leave it
So I guess I could drag it into the room and leave it but it would have to be programmed so that it's coordinates are always in the view where I want them. This just didn't seem like the best way to do it.
 

Tyg

Member
I still dont understand why you think you need to move the coordinates...if your background is like a big scrolling map..just use the draw gui event
 

Tyg

Member
And if you dont want to use Gui and your using viewports to scroll around a map..just draw it to the viewport co-ordinates
If you could explain what you are trying to do i can help you more
 
Here are the buttons I'm programming onto the screen. Right now I have them working using:

draw_sprite(spr_W, 0, global.cameraX + 45, global.cameraY + 416);

But I eventually want to program them so that they can be pressed on a touchscreen. So I need these to be objects. Problem is, everytime I put them in as an object on a draw layer they multiply and leave a trail which will certainly slow down my game.
 

Attachments

Tyg

Member
You are scrolling them with your camera coordinates..do you have clear display buffer checked on your room?
draw_sprite will draw a sprite then it scrolls and stays in the display buffer

better to just assign the sprite to the object and
instead of using draw sprite (which has nothing to do with the object)
x = global.cameraX+45;
y = global.cameraY +416;

if the button doesent show put
draw_self();

also any events can be handled by the objects event system like mouse enter or mouse leave or gesture events for like tablets

alternately you can draw gui event which draws over top of the application layer and then you dont need to update the x or y
 
Last edited:

erayzesen

Member
yes thats what the create event is for..the draw event keeps triggering like the step event so you are endlessly creating instances and will eventually crash..wouldnt it be easier to create your object..drop it on the screen and turn it visible flag off when not need to be drawn? by default the object automatically draws the sprite attached to it you dont even need a draw event..unless your doing a 3d game then you use draw_gui and have to set stuff like blendable but still there is no need to keep creating instances ..hope that helps..just drag and drop your button onto the screen and leave it
If I understood correctly what you are talking about does not make sense. Anyway all your objects are redrawn in every frame, there is such an event for gui drawings in GMS. So if you write this to Draw_GUI;
draw_text (16,16, "test");
will clean and draw again in every frame. Already in the game engine, the parts of the object to be drawn in each frame are drawn, then deleted and drawn again. Even if you create the button object with cr8eate, it will always delete it in the draw event (render event under the hood) and draw it all over again to display it.
 
Did you follow this?


If not, that's why it's multiplying.
I'm so confused. So my object is obj_w. So in the create event:

if instance_exists(obj_Aura){

instance_create_layer(global.cameraX + 45, global.cameraY + 416, "Instances", obj_w);
}

In the draw GUI event:

draw_self();

I know once I figure this out I will completely get it and feel stupid.
I even tried the if statement above in a script and then calling it in a draw event.
It's baffling.
 
Look...where do I put this code:

instance_create_layer(global.cameraX + 45, global.cameraY + 416, "Instances", obj_w);

So that it stays in a specific spot (global.cameraX + 45, global.cameraY + 416) in my screen at all times when I'm playing my game?

That's all I need to know.
 

TsukaYuriko

☄️
Forum Staff
Moderator
Put that code in a Create event, but change the coordinates to 45, 416. Then put draw_self(); in the Draw GUI event of obj_w.
 
Top