How to make a GUI Switch?

TomOmNom

Member
I was wondering how I would make a switch in the GUI that can be called in an if statement to get its on-off position. I couldn't figure out how to store the on-off position properly.
 

samspade

Member
A switch and a gui button are separate and you can tackle each issue separately. How you do it depends a lot on what you're looking for.

I have a tutorial on gui buttons:


As well as a series about making buttons in general. The toggle button is what you're looking for. This one is part of a series though so it might not make sense to start with it.


The one thing I don't cover is how you would actually use the position. To do this, I would probably add a string name to the buttons as a variable, then you could create a script which returns the position of a button with a given name, like this:

GML:
///return_button_position(button_name)

with (button_parent) {
    if (button_name == argument0) {
        return position;
    }
}
return undefined;
 

TomOmNom

Member
I was hoping to be able to put the code in a script actually, so I could call it like:
GML:
if gui_switch(x,y,"other-variables")
{
    enable_something();
} else {
    disable_something();
}
 

samspade

Member
What is "other-variables" in this case? Assuming it was the name of the button, using my code above you could write something like:

GML:
if (return_button_position("other-variables") == 1)
{
    enable_something();
} else {
    disable_something();
}
 

samspade

Member
What do you mean by the name of the button?
To do this, I would probably add a string name to the buttons as a variable
Essentially, if you want to reference another instance you can do so in one of a few ways, the three most common are 1) object id, this only works if you have one and only one of an instance; 2) instance id, this works in certain circumstances, normally one in which you can get the instance id through some other method; and 3) a custom id, one you give it. The version I normally use is a string (e.g. name = "Mute Button"). Then you can loop through all instances of an object type (I normally use inheritance here as well) and check for a match.
 
Last edited:

TomOmNom

Member
I'm trying to create each button using a script, not by using objects, because I'm trying to limit the performance cost of the objects.
 

woods

Member
create a variable in your object
change that variable in the step event
draw different sprite on GUI with if statement

something like....

create event
thruster_actitve = false


step event
// if keyboard_check(ord"W")
{
//move forward code
thruster_active = thrue
}
else
{
thruster_active = false
}



draw gui event
if (thruster_active = true)
{
draw_sprite on
}
else
{
draw_sprite off
}
 

samspade

Member
I'm trying to create each button using a script, not by using objects, because I'm trying to limit the performance cost of the objects.
Unless you plan on having thousands of buttons or your buttons have incredibly complicated code, performance is not a real concern. To accomplish the same thing without GM objects is possible but it will be significantly more work and significantly more complicated. Even so, a similar approach would work, you'd just need to maintain your own list of 'objects' and instead of using with you'd likely be using for loops.
 
Top