Setting up a "Generic" Button

K

Kululu17

Guest
Hey guys - this should be a really easy one for you pro programmers, but I seem to be beating my head against a wall. I want to set up the logic for a "generic" button object to change a variable in the game; for example changing some of the setup values, the map width, number of lives, whatever.

Setting up a button object that will change a specific variable is easy, and you can set up the button with default values, create a bunch of them and then reset the most of defaults using the 'with' command.... but the one thing I can't seem to figure out is a way to set/change the actual variable that I want to be changed.

For example in the create event you could put in

Value_Change = 1;
Min_Value = 1;
Max_Value = 10;

... and if you use the logic in the create event:

Value_To_Change = Variable_X;

Then you can set this using the with command for each instance created:

Button_1 = instance_create (100,100,OBJ_Btn);
with Button_1 {
Min_Value = 2;
Value_To_Change = Variable_Y;
}

This will change the parameters of that specific button... The problem is that for the variable you are trying to change, this just makes a copy of the current value of Variable_X (or Variable_Y), and then changes that value but doesn't push the changes from the step event, back to the underlying variable. Is there any way to code it so that you can reset the value to be changed

Thanks in advance!
 

Nallebeorn

Member
No, this isn't really possible. But you can create a script that changes the variable, pass the script id to the button and then run it with execute_script().

This is also more flexible than what you deired, since it allows button scripts that do anything, not just change a variable.
 
Oh posh - it is possible.

I have the button drawing and interaction in a single script as follows:
Code:
///scr_button(x1,y1,x2,y2,text)
var x1,y1,x2,y2,text,mo,pressed;
x1=argument0
y1=argument1
x2=argument2
y2=argument3
text=argument4
mo=0
pressed=0

draw_set_font(fnt_button)

if mouse_x>x1 and mouse_x<x2 and mouse_y>y1 and mouse_y<y2
    {
    if mouse_check_button(mb_left) mo=2 else mo=1
    if mouse_check_button_released(mb_left) pressed=1
    }

if mo=2 draw_set_color(c_white) else if mo=1 draw_set_color(merge_color(c_color,c_white,.2)) else draw_set_color(c_color)
draw_roundrect(x1,y1,x2,y2,0)
draw_set_halign(fa_center)
draw_set_valign(fa_center)
draw_set_color(c_black)
if mo=2 draw_set_color(c_color) else draw_set_color(c_white)
draw_text_shadow((x1+x2)/2,(y1+y2)/2,text)

return pressed;
Then, I have a single object for my menu and use the script as follows (draw event):
Code:
///Draw Menu
var xx,yy,button_width,button_height,buffer;
xx = 16
yy = 16
button_width = 192
button_height = 64
buffer = 16

if scr_button(xx,yy,xx + button_width,yy + button_height,"New Game")
     {
     scr_new_game()
     }

yy += button_height + buffer
if scr_button(xx,yy,xx + button_width,yy + button_height,"Load Game")
     {
     scr_load_game()
     }

yy += button_height + buffer
if scr_button(xx,yy,xx + button_width,yy + button_height,"Exit Game")
     {
     scr_exit_game()
     }
The drawing code can be with sprites and can be more complicated as needed. The cool thing is you can then just execute code in the scr_exit_game() or just modify variables as you mention. No need for a bunch of objects.
 
K

Kululu17

Guest
Hello - thanks for your responses. Would this mean you would need a separate script for each variable that you want to modify? Or is it possible to set up a single script that modifies variables?
 
M

Maximus

Guest
Hello - thanks for your responses. Would this mean you would need a separate script for each variable that you want to modify? Or is it possible to set up a single script that modifies variables?
Anything is possible,

 
Hello - thanks for your responses. Would this mean you would need a separate script for each variable that you want to modify? Or is it possible to set up a single script that modifies variables?
If you don't take the time to actually look at what the scripts do, you will never learn. Think through it.


You only need the one scr_button - I just used scripts for the actions to demonstrate the use, but you could do this as well:
Code:
///Draw Menu
var xx,yy,button_width,button_height,buffer;
xx = 16
yy = 16
button_width = 192
button_height = 64
buffer = 16

if scr_button(xx,yy,xx + button_width,yy + button_height,"Variable 1")
    {
     variable_01 += 1
     }

yy += button_height + buffer
if scr_button(xx,yy,xx + button_width,yy + button_height,"Variable 2")
     {
     variable_02 += 1
     }

yy += button_height + buffer
if scr_button(xx,yy,xx + button_width,yy + button_height,"Variable 3")
     {
     variable_03 += 1
     }
 
Top