Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Cogliostro on Wed 30/04/2014 15:34:03

Title: use one function for 10 objects, with different results based on ID #
Post by: Cogliostro on Wed 30/04/2014 15:34:03
Okay, the idea is this, when you click on an object it calls a function and generates different results based on the ID of the object. 

IN THIS CASE, I have 10 buttons (all objects) on screen, when you click it, a different object briefly becomes transparent.  (i.e. Click on Object[10] and Object[0] briefly appears.)

Is there a more elegant way to do this - presumably using EXTENDER FUNCTIONS - or am I reduced

Code (ags) Select

function Syn (short value) {
  object[value].TweenTransparency(0.5, 0, eEaseInTween, eBlockTween);
  object[value].TweenTransparency(0.5, 100, eEaseOutTween, eBlockTween);
}

function oBut0_AnyClick() //Object 10;
{
  Syn(0);
}

function oBut1_AnyClick() //Object 11;
{
  Syn(1);
}

//et cetera...


- Cogliostro
Title: Re: Same function for 10 objects, just different #s
Post by: JSH on Wed 30/04/2014 15:49:19
You can override on_mouse_click() in room scripts so something like this should work. ClaimEvent() will prevent any other script files from receiving the click event.

Code (AGS) Select

function on_mouse_click(MouseButton button)
{
  if(button == eMouseLeft)
  {
    Object* pObj = Object.GetAtScreenXY(mouse.x, mouse.y);,
    if(pObj != null)
    {
      Syn(pObj.ID);
      ClaimEvent();
    }
  }
}
Title: Re: Same function for 10 objects, just different #s
Post by: Cogliostro on Wed 30/04/2014 17:46:29
JSH -

It worked like a charm, thanks!

- Cogliostro