Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Newbie Newt on Thu 06/10/2016 23:14:24

Title: GUI Control mouse click question
Post by: Newbie Newt on Thu 06/10/2016 23:14:24
I have a couple of maps(GUIs) with "close" buttons. Both GUIs perform the same function when the "close" button is clicked. Is there a way to include both button 1 & button 2 in the same mouseclick function?
Code (ags) Select

function Bclose2_OnClick(GUIControl *control, MouseButton button)
{
  gRansom.Visible=false;
  gratmap.Visible=false;
  gRansom.Visible=false;
  gratmap.Visible=false;
}


I tried the following, but I knew it wouldn't work.
function Bclose2_OnClick || Bclose1_OnClick (GUIControl *control, MouseButton button)

Thanks
Title: Re: GUI Control mouse click question
Post by: Gurok on Thu 06/10/2016 23:28:14
Yes, there's a way. Go to the events tab of the properties for each button and change the OnClick binding of both buttons to the same function. It's a free text field.

(http://i.imgur.com/IsoRyZr.png)

You might even want to call the function Bclose_OnClick, as it applies to both Bclose buttons.

Note: It seems odd to me that you're setting the Visible property of those GUIs twice.
Title: Re: GUI Control mouse click question
Post by: Khris on Fri 07/10/2016 01:02:44
Just for reference, this is the reason why the GUIControl that was clicked is passed into the function.
You can for instance create a keypad GUI with ten buttons and handle all ten of them using a single function. To find out which of the ten buttons was clicked, you simply check control.ID.

Also note that if AGS didn't allow to freely assign handler functions to events, one could still do this:
function closeMaps() {
  gRansom.Visible=false;
  gratmap.Visible=false;
}

function Bclose1_OnClick(GUIControl *control, MouseButton button) { closeMaps(); }
function Bclose2_OnClick(GUIControl *control, MouseButton button) { closeMaps(); }
Title: Re: GUI Control mouse click question
Post by: Cassiebsg on Fri 07/10/2016 14:40:17
Must admit I never thought of Gurok's solution... (roll)

I have however did this:
Code (ags) Select

function something()
{
...
}

function something_else();
{
something();
}


Works just fine, but I'm pretty sure I'll be using Gurok's solution from now on... saves so much code on the script. (laugh)
Title: Re: GUI Control mouse click question
Post by: Newbie Newt on Fri 14/10/2016 23:28:50
Thanks