There is a new version of the forums ready for testing. Please post here if you're willing to help test! We could also use the help of coders!

Author Topic: Changing button text colour with mouseover [SOLVED]  (Read 385 times)

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: Adventure Game Studio
  1. function room_RepExec(){
  2.   if (Button.GetAtScreenXY(mouse.x, mouse.y) == bBegin){
  3.    
  4.     bBegin.TextColor = 63488;
  5.    
  6.   }
  7.   else{
  8.  bBegin.TextColor = 65535;
  9.   }
  10. }

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

Any help much appreciated, thanks!
Shaun
« Last Edit: 19 May 2022, 18:01 by shaun9991 »
Support Cloak and Dagger Games on Patreon: https://www.patreon.com/user?u=460039

Re: Changing button text colour with mouseover
« Reply #1 on: 18 May 2022, 21:36 »
I think you're missing a null check. I haven't tested this, but maybe try something like this?

Code: Adventure Game Studio
  1. function room_RepExec(){
  2.   Button* tempbutton = GUIControl.GetAtScreenXY(mouse.x, mouse.y).AsButton;
  3.   if (tempbutton == null || tempbutton != bBegin) bBegin.TextColor = 65535;    
  4.   else bBegin.TextColor = 63488;
  5. }

Re: Changing button text colour with mouseover
« Reply #2 on: 19 May 2022, 00:50 »
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: Adventure Game Studio
  1.   if (tempbutton != bBegin)
  2.  

This will also cover null value, because if tempbutton is null, then it's already not equal to bBegin
« Last Edit: 19 May 2022, 01:23 by Crimson Wizard »

eri0o

Re: Changing button text colour with mouseover
« Reply #3 on: 19 May 2022, 01:06 »
@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: Adventure Game Studio
  1. // room script file
  2.  
  3. function room_RepExec()
  4. {
  5.   if (Button.GetAtScreenXY(mouse.x, mouse.y) == btnAbout)
  6.   {      
  7.     btnAbout.TextColor = 63488;      
  8.   }
  9.   else
  10.   {
  11.     btnAbout.TextColor = 65535;
  12.   }
  13. }

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?

Re: Changing button text colour with mouseover
« Reply #4 on: 19 May 2022, 07:59 »
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

Re: Changing button text colour with mouseover
« Reply #5 on: 19 May 2022, 13:05 »
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: Adventure Game Studio
  1.   if (tempbutton != bBegin)
  2.  

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.

Re: Changing button text colour with mouseover (SOLVED)
« Reply #6 on: 19 May 2022, 18:00 »
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

Re: Changing button text colour with mouseover [SOLVED]
« Reply #7 on: 20 May 2022, 14:22 »
Turns out there was an unused transparent button that covered the whole gui, I just removed this and everything worked fine :)

Ah, a classic!   ;)