In game GUI Help

Hey, i'm just wondering the best way to do a button GUI in a game that uses a dynamic camera. I usually resort to putting all the button positions and mouse events in the step event then drawing them in the GUI event but i just feel theres alot of manual stuff involved and it's not very future proof so does anyone know of a better way to do user interactable GUIs? A game i'm going to be working on requires alot of buttons and i feel hard coding them isn't the way to go.
 

Rob

Member
I tend to use a for loop to draw a series of buttons from within the GUI layer.

I also check for the mouse within that for loop as well as the mouse click and then run whatever code I need to (even if it's just changing the state)

I don't know if that's a "better" way but it has been working for me for a while and I prefer it to my previous methods.
 
Yeah that's pretty much what i have been doing but i do mouse click code in step event instead. I didn't realise you could do mouse click code inside the gui event that will atleast save abit of time so i can do it all in one place
 

samspade

Member
I generally place them in the room editor. If your GUI is a different size than your normal view you can set up one of the views to have the same dimension for placement purposes, and then disable it. Then just have a blank draw event, and draw them in the GUI instead.
 
I generally place them in the room editor. If your GUI is a different size than your normal view you can set up one of the views to have the same dimension for placement purposes, and then disable it. Then just have a blank draw event, and draw them in the GUI instead.
I'm not quite sure what you mean, do you mean placing the buttons sprites/objects in the room and i already use the draw gui event
 

samspade

Member
I'm not quite sure what you mean, do you mean placing the buttons sprites/objects in the room and i already use the draw gui event
Yes, place instances of your button objects in the room, using the room editor and a view which matches your gui dimensions.
 
Yes, place instances of your button objects in the room, using the room editor and a view which matches your gui dimensions.
But then how do you draw them, and even if you use the draw gui event to draw it in the proper place if the camera moves fast enough the button (Clickable area) wont be in the same place as the graphic for the button. That's how i used to do it but it ended up being alot of extra coding for not much gain so i went with drawing it in the gui event. Thanks for the suggestion though it may not help me but someone looking at this post may see it and would rather use that way to do things.
 

Rob

Member
Yeah that's pretty much what i have been doing but i do mouse click code in step event instead. I didn't realise you could do mouse click code inside the gui event that will atleast save abit of time so i can do it all in one place
I'm pretty sure it's considered "naughty" to do it (from what I've read) and one time I put ALL of my game code into the draw/draw_gui layers to see what would happen - with hilarious results (strange bugs) - but small amounts of code seems to run fine
 
Last edited:

samspade

Member
I draw them using the draw gui event. A basic gui button, placed in the room through the room editor (or code) looks like this:

Code:
///create event
//whatever you want/need

///step event
if (whatever you are using to detect use e.g. mouse or keyboard) {
    //do whatever button is supposed to do
}

///draw event
//leave blank so it isn't drawn

///draw gui event
draw_self();
a very simple click detection for the gui could be (doing from memory so there might be mistakes):

Code:
if (device_mouse_check_button_pressed(0, mb_left)) {
    if (position_meeting(device_mouse_x_to_gui(0), device_mouse_y_to_gui(0), id) {
        //do stuff
    }
}
But it could be more complicated - that for example only detects a single touch and assumes that you want it to be clickable with a mouse (instead of keyboard for example)
 
I draw them using the draw gui event. A basic gui button, placed in the room through the room editor (or code) looks like this:

Code:
///create event
//whatever you want/need

///step event
if (whatever you are using to detect use e.g. mouse or keyboard) {
    //do whatever button is supposed to do
}

///draw event
//leave blank so it isn't drawn

///draw gui event
draw_self();
a very simple click detection for the gui could be (doing from memory so there might be mistakes):

Code:
if (device_mouse_check_button_pressed(0, mb_left)) {
    if (position_meeting(device_mouse_x_to_gui(0), device_mouse_y_to_gui(0), id) {
        //do stuff
    }
}
But it could be more complicated - that for example only detects a single touch and assumes that you want it to be clickable with a mouse (instead of keyboard for example)
hmm, i can see that working in some instances better than drawing the buttons from scratch. Thanks for the suggestion :)
 
Top