Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: shaun9991 on Wed 18/05/2022 21:07:38

Title: Changing button text colour with mouseover [SOLVED]
Post by: shaun9991 on Wed 18/05/2022 21:07:38
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) Select
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
Title: Re: Changing button text colour with mouseover
Post by: 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?

Code (ags) Select
function room_RepExec(){
  Button* tempbutton = GUIControl.GetAtScreenXY(mouse.x, mouse.y).AsButton;
  if (tempbutton == null || tempbutton != bBegin) bBegin.TextColor = 65535;   
  else bBegin.TextColor = 63488;
}
Title: Re: Changing button text colour with mouseover
Post by: 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) Select

  if (tempbutton != bBegin)


This will also cover null value, because if tempbutton is null, then it's already not equal to bBegin
Title: Re: Changing button text colour with mouseover
Post by: eri0o on Thu 19/05/2022 01:06:47
@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) Select
// 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?
Title: Re: Changing button text colour with mouseover
Post by: shaun9991 on Thu 19/05/2022 07:59:33
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
Title: Re: Changing button text colour with mouseover
Post by: Laura Hunt on Thu 19/05/2022 13:05:14
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) Select

  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.
Title: Re: Changing button text colour with mouseover (SOLVED)
Post by: shaun9991 on Thu 19/05/2022 18:00:48
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!
Title: Re: Changing button text colour with mouseover [SOLVED]
Post by: Matti on Fri 20/05/2022 14:22:27
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!   ;)