Adventure Game Studio

AGS Support => Advanced Technical Forum => Topic started by: thiezar on Sun 09/10/2011 13:46:56

Title: Global function on event [SOLVED]
Post by: thiezar on Sun 09/10/2011 13:46:56
Ok, here's the problem:
I would like to use a global function when an event occours but the compiler says he can't find the function.
I wrote a "cantTalkToIt()" function in the global script and then I imported it in the global script header. In the event editor of each object i wrote "cantTalkToIt" in the "Talk to object" field but it doesn't work.
I really don't want to write the same function for every object in every room...
Title: Re: Global function on event
Post by: Khris on Sun 09/10/2011 14:55:36
You don't even have to put the function's name into every object's TalkTo event field.

In the global script, find on_mouse_click and in there the eMouseLeft block.

Now put this at the start:

    if (mouse.Mode == eModeTalkto && GetLocationType(mouse.x, mouse.y) == eLocationObject) {
      cantTalkToIt();
      return;
    }


I'm not sure exactly what the problem is with your method; I'd be surprised if one can't link global functions to room events but maybe that's the case.
Title: Re: Global function on event
Post by: thiezar on Tue 11/10/2011 14:46:31
It's a good solution if I would call the function for every object.... but this function will be called for just about half of the objects  :-\
Title: Re: Global function on event
Post by: Khris on Tue 11/10/2011 15:09:16
Then just do what you're supposed to be doing anyway; add this to the global script:

function unhandled_event (int what, int type) {

  if (what == 2) {  // object

    if (type == 2) {  // talk to object

      // code goes here
    }
    ...
  }

  ...
}


Now if the player tries to talk to an object and the respective event field is empty, the unhandled_event function takes over.

For the full documentation, look at the bottom of this manual page: http://www.adventuregamestudio.co.uk/manual/TextScriptEvents.htm
Title: Re: Global function on event
Post by: thiezar on Tue 11/10/2011 15:27:50
Uuuh...cool! I can use this also for the "ItDoesntWork()" function ^_^
Thank you, I didn't know of a funcion for unhandled events.  :)