Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Mel_O_Die on Mon 16/10/2006 22:54:45

Title: Sound on gui rollover
Post by: Mel_O_Die on Mon 16/10/2006 22:54:45
Hi!

I want to play a little buzz when the mouse activates a GUI butto's mouseover mode

how can i do that?  ??? ???

thanks!
Title: Re: Sound on gui rollover
Post by: Theeph on Tue 17/10/2006 06:50:54
Hiya.

You'll need to use a piece of code like this:


GUIControl *theControl = GUIControl.GetAtScreenXY(mouse.x, mouse.y);
if (theControl == lstSaveGames) {
  // The mouse is over the Save Games list box.
}
else if (theControl == null) {
  // The mouse is not over a control.
}
else {

  GUI *onGui = theControl.OwningGUI;
     
     if(theControl.ID == 1)    // if the mouse is over control number 1
     {
        PlaySound(1);        // play the appropriate sound for control 1
     }

     else if(theControl.ID == 2)
     {
             // keep going until you run out of buttons to attach sounds to.
     }

}




I don't know how much of a good idea this next bit is, but it will work:

Paste the above into your global script inside the #repeatedly_execute section.

Something like this:


#sectionstart repeatedly_execute  // DO NOT EDIT OR REMOVE THIS LINE
function repeatedly_execute()
  {

        if(gYourGuiHere.visible == true)
               {
                    <<put the above code in here>>
               }
  }


Hope that helps. PM if you can't get it to work.
Title: Re: Sound on gui rollover
Post by: Mel_O_Die on Tue 17/10/2006 13:51:43
thanks very much! that works fine!

(i hade just to fix a thing because with your method the sound is looped :) )
Title: Re: Sound on gui rollover
Post by: Theeph on Tue 17/10/2006 14:21:57
Oops. Sorry for the loopage.

Glad it worked.
Title: Re: Sound on gui rollover
Post by: Khris on Tue 17/10/2006 19:40:33
You didn't post the adjusted code, but this should shorten it:

function repeatedly_execute_always() {
  GUI*g=GUI.GetAtScreenXY(mouse.x, mouse.y);
  if (GUI==xxx) {   // replace xxx with GUI's name
    GUIControl*gc=GUIControl.GetAtScreenXY(mouse.x, mouse.y);
    if (IsSoundPlaying()==0 && gc.AsButton!=null) PlaySound(x);  // replace x with sound's number
  }
}


Edit: Won't work as intended, see below.
Title: Re: Sound on gui rollover
Post by: Mel_O_Die on Tue 17/10/2006 20:19:58
Quote from: KhrisMUC on Tue 17/10/2006 19:40:33
You didn't post the adjusted code, but this should shorten it:

Oh yesÃ,  ::) excuse me :)

for doing that i used a globalint initially set as "0" and changed to "1" when the sound is played once and after i reset the global int when the mouse is not over a control :)



GUIControl *theControl = GUIControl.GetAtScreenXY(mouse.x, mouse.y);

if (theControl == null) { // The mouse is not over a control.
Ã,  SetGlobalInt(101, 0); // sets the globalint to 0
}

if (GetGlobalInt(101)==0){Ã,  //check the global int
Ã,  Ã, if (gYOURGUI.Visible){
Ã,  Ã,  Ã,  if((theControl.ID == 2)||(theControl.ID == 5)||(theControl.ID == 7)||(theControl.ID == 3)||(theControl.ID == 6))Ã, { Ã,  // if the mouse is over controls number 2,5,7,3,6

Ã,  Ã,  Ã,  Ã, Ã,  SetGlobalInt(101, 1);Ã,  // sets the globalint to 1 for play sound once
Ã,  Ã,  Ã,  Ã, Ã,  PlaySound(9);Ã,  Ã,  Ã,  Ã,  // play the appropriate sound for controls
Ã,  Ã,  Ã,  }
Ã,  Ã, }
}



I will try with your method who seems easier :)

and i've an other question, what's the difference between
function repeatedly_execute_always() {
and
function repeatedly_execute() {


i've all my scripts who needs to be executed each frame in the simply execute, what the allways can give to me?
Title: Re: Sound on gui rollover
Post by: GarageGothic on Tue 17/10/2006 22:56:47
The repeatedly_execute_always runs also during blocking functions (characters talking, some walk modes) while the normal repeatedly_execute doesn't. I assume the player can't use the interface during such scenes, so I would assume the normal repeatedly_execute should be enough in your case.
Title: Re: Sound on gui rollover
Post by: Theeph on Wed 18/10/2006 08:27:56
Quote from: KhrisMUC on Tue 17/10/2006 19:40:33
function repeatedly_execute_always() {
  GUI*g=GUI.GetAtScreenXY(mouse.x, mouse.y);
  if (GUI==xxx) {   // replace xxx with GUI's name
    GUIControl*gc=GUIControl.GetAtScreenXY(mouse.x, mouse.y);
    if (IsSoundPlaying()==0 && gc.AsButton!=null) PlaySound(x);  // replace x with sound's number
  }
}


Won't that still repeat the sound over and over?

IsSoundPlaying() will see if there is a sound currently playing but if the sound finishes won't it just start up again? May not be a problem if the button sound is a full length song or anything, but if it's a half-second blip it'd still seem to loop.

Or am I missing something?

If not - is there any way apart from globalInts to get the off switch to work?
Title: Re: Sound on gui rollover
Post by: Khris on Wed 18/10/2006 09:12:07
Of course, you're right.
Global Ints are completely redundant, you can always use "script-ints" or pointers, like this:

Button*oldb; // pointer holding current/old button

function repeatedly_execute() {
  GUI*g=GUI.GetAtScreenXY(mouse.x, mouse.y);
  if (GUI==xxx) {   // replace xxx with GUI's name
    GUIControl*gc=GUIControl.GetAtScreenXY(mouse.x, mouse.y);
    Button*b=gc.AsButton;
    if (b!=null && b!=oldb) {
      PlaySound(x);  // replace x with sound's number
      oldb=b; // set old button to current one
    }
  }
}
Title: Re: Sound on gui rollover
Post by: Theeph on Wed 18/10/2006 12:08:13
Hmm... I like the use of data encapsulation versus the global system.

I've recently been scripting with 2.62 as well as another project with 2.72. It's right about here it gets confusing.

Cheers though. Good tip.