Changing button text colour with mouseover [SOLVED]

Started by shaun9991, Wed 18/05/2022 21:07:38

Previous topic - Next topic

shaun9991

Hi all!

Very basic question here, I'm missing something very obvious. I just want text on a gui button to change colour when the mouse is hovering over it. This is the code I'm using:

Code: ags
function room_RepExec(){
  if (Button.GetAtScreenXY(mouse.x, mouse.y) == bBegin){
    
    bBegin.TextColor = 63488;
    
  }
  else{
 bBegin.TextColor = 65535;
  }
}


It doesn't work at all. What am I doing wrong?

Any help much appreciated, thanks!
Shaun
Support Cloak and Dagger Games on Patreon: https://www.patreon.com/user?u=460039

Laura Hunt

I think you're missing a null check. I haven't tested this, but maybe try something like this?

Code: ags
function room_RepExec(){
  Button* tempbutton = GUIControl.GetAtScreenXY(mouse.x, mouse.y).AsButton;
  if (tempbutton == null || tempbutton != bBegin) bBegin.TextColor = 65535;    
  else bBegin.TextColor = 63488;
}

Crimson Wizard

#2
Quote from: Laura Hunt on Wed 18/05/2022 21:36:11
I think you're missing a null check. I haven't tested this, but maybe try something like this?

There's no need for a null check when you are comparing a pointer value.

Code: ags

  if (tempbutton != bBegin)


This will also cover null value, because if tempbutton is null, then it's already not equal to bBegin

eri0o

@shaun9991, I just tested and that works. I loaded up a new game using the Sierra Template, and made the test below using the btnAbout in the gPanel of that template.
Code: ags
// room script file

function room_RepExec()
{
  if (Button.GetAtScreenXY(mouse.x, mouse.y) == btnAbout)
  {      
    btnAbout.TextColor = 63488;      
  } 
  else 
  {
    btnAbout.TextColor = 65535;
  }
}


So there's something else at play that is not the script. Can you tell me about the specific button properties? I can tell that if the button is not Enabled, it will not work. Are you sure there's no other GUI on top of it? Is room_RepExec really linked in the room editor?

shaun9991

Thanks everyone!! :)

eri0o I think you are correct, there might be something else going on elsewhere in the script that is cancelling it out. I'll check it tonight
Support Cloak and Dagger Games on Patreon: https://www.patreon.com/user?u=460039

Laura Hunt

Quote from: Crimson Wizard on Thu 19/05/2022 00:50:49
Quote from: Laura Hunt on Wed 18/05/2022 21:36:11
I think you're missing a null check. I haven't tested this, but maybe try something like this?

There's no need for a null check when you are comparing a pointer value.

Code: ags

  if (tempbutton != bBegin)


This will also cover null value, because if tempbutton is null, then it's already not equal to bBegin

True. Sometimes I tend to be super conservative with my null checks, but you're right of course, in this case it wouldn't be necesssary. eri0o's suggestions are probably on the right track.

shaun9991

Got it working with my original code, thanks to eri0o for the suggestion to dig around a little.

Turns out there was an unused transparent button that covered the whole gui, I just removed this and everything worked fine :)

Thanks!
Support Cloak and Dagger Games on Patreon: https://www.patreon.com/user?u=460039

Matti

Quote from: shaun9991 on Thu 19/05/2022 18:00:48
Turns out there was an unused transparent button that covered the whole gui, I just removed this and everything worked fine :)

Ah, a classic!   ;)

SMF spam blocked by CleanTalk