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

Legacy GM Button object not responding reliably

D

dyslexicfurby

Guest
So this one is pretty weird. I made a button object with a Left Released event. Basically the only thing in that event is
scr_attack_roll();
which calls a simple dice roll script:
Code:
randNum = irandom(10);
inString = "The dice roll is " + string(randNum);
obj_textboxlong.message = inString;
It then changes the 'message' variable for object TextBoxLong to display the diceroll.

However it seems to be a crap shoot if anything will happen when I click the button. Sometimes nothing, sometimes it will update the text box with the roll. One thing I notice is that once it starts updating, the button will start responding reliably. But until then it can be hit or miss. My project isn't complicated, I'm not sure what's going on here. Any thoughts?
 

CloseRange

Member
check the sprite mask in the buttons sprite. Make sure it is the right size. Also check your other code in the object to make sure nothing else is conflicting.
 
D

dyslexicfurby

Guest
I did some digging and removed some unneeded movement code from the step event of some objects (since they aren't moving) and the button seems to work okay now. these objects weren't really related to the button or the text box...is it possible the step event was causing some sort of lag by checking for keyboard input every step?
 
D

dyslexicfurby

Guest
Correction: after playing with it a bit more, the button is still not triggering reliably. my click area is correct that much I know. The text box takes a string in 'message', but that's all it should do. What else can I check?
 

CloseRange

Member
Unless you have a giant game it is very unlikely lag was the problem and even then removing the step event wouldn't have been enough to fix it.
I'd say make sure you arn't drawing in the draw gui (code easily gets messed up when drawing there) also you should show all the code for the button pressed would make it easier to analyze and find the problem
 
D

dyslexicfurby

Guest
I'm using the Draw event instead of the Draw Gui event. Here's my code:
Button create event:
Code:
///Initialize variables


xpos    = obj_button.x;
ypos    = obj_button.y;

str     = "Attack 1";
action  = 0;

padding     = 10;

default_width = 100;

x1          = xpos;
y1          = ypos;

if(string_width(str) <= default_width){
    x2 = xpos + default_width;
}
else{
    x2 = xpos + string_width(str); 
}

y2     = ypos + string_height(str);
Left Click Release event:
Code:
scr_attack_roll(2,20,id);
This calls a script to roll dice

Button Draw:
Code:
draw_set_colour(c_maroon);
draw_button(x1, y1, x2 + padding, y2 + padding, !(mouse_check_button(mb_left) && point_in_rectangle(mouse_x, mouse_y, x1, y1, x2 + padding, y2 + padding)));

draw_set_colour(c_white);
//the following is compensating halign/valign not working
textCenterX = string_width(str)/2;
textCenterY = string_height(str)/2;

buttonCenterX = x1 + 0.5*(x2 - x1);
buttonCenterY = y1 + 0.5*(y2 - y1);

draw_text((buttonCenterX - textCenterX) + (padding/2), (buttonCenterY - textCenterY) + (padding/2), str);
I don't think it's necessary but I included scr_attack_roll for due dilligence:
Code:
diceNum     = argument0;
diceSides   = argument1;
diceTotal   = 0;
inString    = "Rolled ";
roll        = 0;
target      = argument2; //Who is being attacked by dice?

do
{
    roll = irandom(argument1);
    inString += string(roll) + " ";
    diceTotal += roll;
    diceNum --;
}
until(diceNum <= 0);

inString += "Total is " + string(diceTotal);
obj_textbox2.str = inString;
obj_enemy.hp -= diceTotal;
if(obj_enemy.hp <=0){
    instance_destroy(target);
}
Essentially, the button does not reliably trigger the script call to scr_attack_roll
 
Last edited by a moderator:

CloseRange

Member
I think your problem is just the fact that you are drawing a button instead of having a sprite. The mouse pressed event works only if you have a sprite with an image mask. My suggestion is do something like this in a step event (or draw event if you really wanted)
Code:
if point_in_rectangle(mouse_x, mouse_y, x1, y1, x2 + padding, y2 + padding) {
     if mouse_check_button_released(mb_left) {
          scr_attack_roll(2, 20, id);
     }
}
that way it will check the mouse position relevant to the draw area instead of a button mask witch isn't there or is non exisant
 
D

dyslexicfurby

Guest
I wasn't using a mask. But putting the button check with button area in the step event worked! Thanks!
 
Top