Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Sirpunchula on Mon 19/10/2009 01:06:38

Title: Unhandled events? [resolved]
Post by: Sirpunchula on Mon 19/10/2009 01:06:38
I'm trying to set up events in which a default message appears when I interact with something with no default event listed. The manual is telling me to use a "what" and "type" value, but I don't know how it works, and I can't find an example in any script I already have. I know that I'm supposed to use this command:

unhandled_event (int what, int type)

but I don't know how to implement it or where I need to place the display message. Could anyone clarify this for me?
Title: Re: Unhandled events?
Post by: Khris on Mon 19/10/2009 01:32:14
Put this in the global script:

function unhandled_event(int what, int type) {
  if (what == 1) {
    if (type == 1) {
      ...
    }
    else if (type == 2) {
      ...
    }
  ...
}


(In the case of an unhandled event, AGS looks for the presence of the function and if it exists, calls e.g. unhandled_event(2, 3); as if you had typed that yourself in a script.)
Title: Re: Unhandled events?
Post by: Sirpunchula on Mon 19/10/2009 01:57:46
I think I have the first part covered, but I don't know what I need to put in the "..." after else if (type ==2) Can you clarify that for me, please?
Title: Re: Unhandled events? [problem still not resolved]
Post by: monkey0506 on Mon 19/10/2009 06:13:23
The manual gives a list of the values (http://www.americangirlscouts.org/agswiki/Predefined_global_script_functions#unhandled_event) that will be passed to unhandled_event.
     WHAT TYPE Description
      1    1   Look at hotspot
      1    2   Interact with hotspot
      1    3   Use inventory on hotspot
      1    4   Talk to hotspot
      1    7   Pick up hotspot
      1    8   Cursor Mode 8 on hotspot
      1    9   Cursor Mode 9 on hotspot
      2    0   Look at object
      2    1   Interact with object
      2    2   Talk to object
      2    3   Use inventory on object
      2    5   Pick up object
      2    6   Cursor Mode 8 on object
      2    7   Cursor Mode 9 on object
      3    0   Look at character
      3    1   Interact with character
      3    2   Speak to character
      3    3   Use inventory on character
      3    5   Pick up character
      3    6   Cursor Mode 8 on character
      3    7   Cursor Mode 9 on character
      4    1   Look at nothing (ie. no hotspot)
      4    2   Interact with nothing
      4    3   Use inventory with nothing
      4    4   Talk to nothing
      5    0   Look at inventory
      5    1   Interact with inventory (currently not possible)
      5    2   Speak to inventory
      5    3   Use an inventory item on another
      5    4   Other click on inventory
So what you put in place of the "..." would be determined by how you want to handle that particular event. For example for "what == 1" and "type == 2" the above chart tells us that the player attempted to interact with a hotspot that does not have an interaction defined for eModeInteract. So you might then use something such as:

    player.Say("I can't use that!");

In place of the "...". Again, it's all up to you what happens when the player attempts an interaction that isn't handled. You can display a message like I showed, or do nothing, or anything in-between. It depends on your preference, style of your game, etc.
Title: Re: Unhandled events? [problem not yet resolved]
Post by: Sirpunchula on Mon 19/10/2009 23:00:24
This seems to work, but every time I insert this code:

function unhandled_event(int what, int type) {
  if (what == 1) {
    if (type == 1) {
      Display("displaymessage");
    }

(Code entered as is)

The game refuses to run because of an open function missing a bracket, but the given line is at -10. I try to insert the bracket wherever I can, but the error either returns the same or says that I need to remove the bracket. What am I doing wrong?
Title: Re: Unhandled events? [problem not yet resolved]
Post by: Crimson Wizard on Mon 19/10/2009 23:07:28
Quote from: Sirpunchula on Mon 19/10/2009 23:00:24
This seems to work, but every time I insert this code:

function unhandled_event(int what, int type) {
 if (what == 1) {
   if (type == 1) {
     Display("displaymessage");
   }

(Code entered as is)

The game refuses to run because of an open function missing a bracket, but the given line is at -10. I try to insert the bracket wherever I can, but the error either returns the same or says that I need to remove the bracket. What am I doing wrong?



function unhandled_event(int what, int type) {
 if (what == 1) {
   if (type == 1) {
     Display("displaymessage");
   }
 }
}


You shouldn't insert brackets "wherever you can" ;). Simply check that for every opened bracket there's a corresponding pair.
Try to think of it as it is a box that need to be closed from two sides. { closes it at left and } closes it at right. Some boxes are inside other boxes, and ofcourse those inside should be closed inside, not outside of the larger box.
Title: Re: Unhandled events? [resolved!]
Post by: Sirpunchula on Mon 19/10/2009 23:13:44
Thanks, I finally got it working.

Edit: I thought I was in the clear, but I'm having another complication. I'm trying to use more than one unhandled event with this code:


function unhandled_event(int what, int type) {
  if (what == 2) {
    if (type == 0) {
      Display("It looks fine.");
    }
  }

else

  if (what == 2){
    if (type == 1){
      Display ("You have more important things to worry about at the moment.");
    }
  }
}


There are no compiling errors here, but when I attempt to interact with an unhandled object (as defined after the "else"), I don't receive a message; instead, nothing happens. Am I supposed to do this differently?
Title: Re: Unhandled events? [not resolved, sorry]
Post by: Lufia on Tue 20/10/2009 00:17:02
Your code reads as:
function unhandled_event(int what, int type) {
  if (what == 2) { // If "what" is 2
  }
  else { // If "what" is anything other that 2
    if (what == 2){ // If "what" is 2
    }
  }
}

Yeah, I can see how that's not working...

Khris gave you the correct structure earlier.

if (what == 1) {
  if (type==1) {
  }
  else if (type==2) {
  }
  // ...
else if (what==2) {
  if (type==1) {
  }
  else if (type==2) {
  }
  // ...
}
// ...
Title: Re: Unhandled events? [not resolved, sorry]
Post by: RickJ on Tue 20/10/2009 00:44:33
Lufia has identified your problem while I was typing the code below.  It's a skeleton of the entire function.  You just need to fill in the blanks as to what happens in each of the conditions.


Function unhandled_event(int what, int type) {

   //  Unhandled Hotspot
   if (what == 1) {         // Hotspot     
      if (type==1) {         // 1    1   Look at hotspot
      }
      else if (type==2) {  //  1    2   Interact with hotspot
      }
      else if (type==3) {  //  1    3   Use inventory on hotspot
      }
      else if (type==4) {  //  1    4   Talk to hotspot
      }
      else if (type==7) {  //  1    7   Pick up hotspot
      }
      else if (type==8) {  //  1    8   Cursor Mode 8 on hotspot
      }
      else if (type==9) {  //  1    9   Cursor Mode 9 on hotspot
      }
      else  {                     //  Undefined
      }
   }
     
   //  Unhandled Object
   else if (what== 2) {   
      //
      if (type==0) {         //  2    0   Look at object
         Display("It looks fine.");
      }
      else if (type==1) {  //  2    1   Interact with object
          Display ("You have more important things to worry about at the moment.");
      }
      else if (type==2) {  //  2    2   Talk to object
      }
      else if (type==3) {  //  2    3   Use inventory on object
      }
      else if (type==5) {  //  2    5   Pick up object
      }
      else if (type==6) {  //  2    6   Cursor Mode 8 on object
      }
      else if (type==7) {  //  2    7   Cursor Mode 9 on object
      }
      else  {                     //  Undefined
      }
   }

   //  Unhandled Character
   else if (what==3)  {   
      else if (type==0) {  //  3    0   Look at character
      }
      else if (type==1) {  //  3    1   Interact with character
      }
      else if (type==2) {  //  3    2   Speak to character
      }
      else if (type==3) {  //  3    3   Use inventory on character
      }
      else if (type==5) {  //  3    5   Pick up character
      }
      else if (type==6) {  //  3    6   Cursor Mode 8 on character
      }
      else if (type==7) {  //  3    7   Cursor Mode 9 on character
      }
      else  {                     //  Undefined
      }
   }
     
   // Unhandled Nothing
   else if (what==4) {
      if (type==1) {          //  4    1   Look at nothing (ie. no hotspot)
      }
      else if (type==2) {  //  4    2   Interact with nothing
      }
      else if (type==3) {  //  4    3   Use inventory with nothing
      }
      else if (type==4) {  //  4    4   Talk to nothing
      }
      else  {                     //  Undefined
      }     
   }
     
   // Unhandled Inventory
   else if (what==5) {
      else if (type==0) {  //  5    0   Look at inventory
      }
      else if (type==1) {  //  5    1   Interact with inventory (currently not possible)
      }
      else if (type==2) {  //  5    2   Speak to inventory
      }
      else if (type==3) {  //  5    3   Use an inventory item on another
      }
      else if (type==4) {  //  5    4   Other click on inventory
      }
      else  {                     //  Undefined
      }     
   }

  // Unhandled Undefined
   else {
      // This isn't supposed to happen
   }
}

Title: Re: Unhandled events? [not resolved, sorry]
Post by: Sirpunchula on Tue 20/10/2009 04:37:23
Quote from: RickJ
Lufia has identified your problem while I was typing the code below.  It's a skeleton of the entire function.  You just need to fill in the blanks as to what happens in each of the conditions.

The first part is fine, but I get an unexpected parser error at "else" at the character events:


//  Unhandled Character
   else if (what==3)  {   
   //
      else if (type==0) {  //  3    0   Look at character (The error is on this line.)
      Display("Someone is standing here.");
      }
      else if (type==1) {  //  3    1   Interact with character
      Display("Poking a stranger is likely to put you in a bad situation.");
      }
      else if (type==2) {  //  3    2   Speak to character
      Display("You get no response."
      }
      else if (type==3) {  //  3    3   Use inventory on character
      Display("Your generous offer is not accepted.");
      }
      else if (type==5) {  //  3    5   Pick up character
      }
      else if (type==6) {  //  3    6   Cursor Mode 8 on character
      }
      else if (type==7) {  //  3    7   Cursor Mode 9 on character
      }
      else  {                     //  Undefined
      }
   }


Does anyone know how I can fix that?
Title: Re: Unhandled events? [not resolved, sorry]
Post by: ThreeOhFour on Tue 20/10/2009 04:50:03
Quote from: Sirpunchula on Tue 20/10/2009 04:37:23
Quote from: RickJ
Lufia has identified your problem while I was typing the code below.  It's a skeleton of the entire function.  You just need to fill in the blanks as to what happens in each of the conditions.

The first part is fine, but I get an unexpected parser error at "else" at the character events:


//  Unhandled Character
   else if (what==3)  {   
   //
      else if (type==0) {  //  3    0   Look at character (The error is on this line.)
      Display("Someone is standing here.");
      }
      else if (type==1) {  //  3    1   Interact with character
      Display("Poking a stranger is likely to put you in a bad situation.");
      }
      else if (type==2) {  //  3    2   Speak to character
      Display("You get no response.");
      }
      else if (type==3) {  //  3    3   Use inventory on character
      Display("Your generous offer is not accepted.");
      }
      else if (type==5) {  //  3    5   Pick up character
      }
      else if (type==6) {  //  3    6   Cursor Mode 8 on character
      }
      else if (type==7) {  //  3    7   Cursor Mode 9 on character
      }
      else  {                     //  Undefined
      }
   }


Does anyone know how I can fix that?

Fixed. Keep an eye out to make sure everything is closed properly with brackets and semicolons :)
Title: Re: Unhandled events? [not resolved, sorry]
Post by: Ryan Timothy B on Tue 20/10/2009 05:24:10
You have an else in the beginning of the structure.
This should fix your issue.  Although, I can't see the missing Brace that Ben was mentioning.  Or I'm blind. :P

//[..]
//  Unhandled Character
   else if (what==3)  {   
   //
      if (type==0) {  //  3    0   Look at character (The error is on this line.)
      Display("Someone is standing here.");
      }
      else if (type==1) {  //  3    1   Interact with character
      Display("Poking a stranger is likely to put you in a bad situation.");
      }
      else if (type==2) {  //  3    2   Speak to character
      Display("You get no response."
      }
      else if (type==3) {  //  3    3   Use inventory on character
      Display("Your generous offer is not accepted.");
      }
      else if (type==5) {  //  3    5   Pick up character
      }
      else if (type==6) {  //  3    6   Cursor Mode 8 on character
      }
      else if (type==7) {  //  3    7   Cursor Mode 9 on character
      }
      else  {                     //  Undefined
      }
   }
Title: Re: Unhandled events? [not resolved, sorry]
Post by: RickJ on Tue 20/10/2009 07:37:08
I just checked myself and the Unhandled Inventory has the same problem.  The  first few lines should be changed to this

   // Unhandled Inventory
   else if (what==5) {
       if (type==0) {       //  5    0   Look at inventory
      }


Also save yourself a lot of headaches in the future by properely indenting the code.  Your Display statements are inside a code  block delineated by the characters { and } so the should be indented and not aligned with the enclosing brackets.  Here is an example of how to do it.

   //  Unhandled Character
   else if (what==3)  {   

      if (type==0) {  //  3    0   Look at character (The error is on this line.)
         Display("Someone is standing here.");
      }
      else if (type==1) {  //  3    1   Interact with character
         Display("Poking a stranger is likely to put you in a bad situation.");
      }
      else if (type==2) {  //  3    2   Speak to character
         Display("You get no response."
      }
      else if (type==3) {  //  3    3   Use inventory on character
         Display("Your generous offer is not accepted.");
      }
      else if (type==5) {  //  3    5   Pick up character
      }
      else if (type==6) {  //  3    6   Cursor Mode 8 on character
      }
      else if (type==7) {  //  3    7   Cursor Mode 9 on character
      }
      else  {                     //  Undefined
      }
   }

Title: Re: Unhandled events? [not resolved, sorry]
Post by: ThreeOhFour on Tue 20/10/2009 08:06:04
Still missing that closing bracket and semicolon in that script under type==2, Rick  ;D
Title: Re: Unhandled events? [not resolved, sorry]
Post by: RickJ on Tue 20/10/2009 08:32:58
Thanks Ben,  your eyes are better than mine  cause I can't spot it.   ;D
Title: Re: Unhandled events? [not resolved, sorry]
Post by: ThreeOhFour on Tue 20/10/2009 09:12:44
      else if (type==2) {  //  3    2   Speak to character
         Display("You get no response."
      }


Should be

      else if (type==2) {  //  3    2   Speak to character
         Display("You get no response.");
      }
Title: Re: Unhandled events? [not resolved, sorry]
Post by: Khris on Tue 20/10/2009 12:16:26
I don't want to confuse anybody, but maybe this is a good time to point out that a) the elses are superfluous here since what and type aren't changing and b) so are the brackets around a single command.

Here's a short version of the character block:

 //  Unhandled Character
 if (what==3) {

   //  3    0   Look at character
   if (type==0) Display("Someone is standing here.");

   //  3    1   Interact with character
   if (type==1) Display("Poking a stranger is likely to put you in a bad situation.");

   //  3    2   Speak to character
   if (type==2) Display("You get no response.");

   //  3    3   Use inventory on character
   if (type==3) Display("Your generous offer is not accepted.");

   ...
 }


Edit:
added missing );
Title: Re: Unhandled events? [not resolved, sorry]
Post by: Crimson Wizard on Tue 20/10/2009 13:53:28
*cough* AGS really could benefit from switch command  ::)
Title: Re: Unhandled events? [not resolved, sorry]
Post by: monkey0506 on Tue 20/10/2009 18:51:35
Quote from: Khris on Tue 20/10/2009 12:16:26a) the elses are superfluous here since what and type aren't changing

Not entirely so. The engine can skip over the rest of an if-else if-else block at the first condition found to be true. Consider:

if (a == 1) {
}
if (a == 2) {
}
if (a == 3) {
}
else {
}


Here there are two important notes to take: 1) the engine must parse every condition regardless of whether it has already found the one that is true, and 2) the else-clause only applies to the final if, making an "else" to the entire block impossible with this route.

Now let's consider the alternative:

if (a == 1) {
}
else if (a == 2) {
}
else if (a == 3) {
}
else {
}


Here the engine will only have to parse the conditions until one is found to be true. If a is set to 1 then the entire rest of the block will be skipped over. Only if a is not 1 then the engine must check if a is 2, and so forth. Also since all the statements are grouped together this allows us to implement an else-clause which applies to the whole block, not just the final if.

The only instance in which it would actually be beneficial to omit the elses is if the value of the variable(s)/state of the condition is changing so that you could then perform multiple checks within the same loop/function call.

Generally speaking though the differences wouldn't be game-impacting. However if you for example are calling a function as part of the condition (which in turn may call other functions, and so forth) then the differences could be hugely impacting. So although in this case they aren't strictly "necessary" there's not any benefit to omitting them either except cutting 5 characters from each subsequent clause. There is a notable drawback however in that it would then make it impossible to offer a generic else to the entire block.

Quote from: Crimson Wizard on Tue 20/10/2009 13:53:28*cough* AGS really could benefit from switch command  ::)

I never really saw the major, humongous differences that it would honestly make. I understand how they're used, no problem. But effectively they're not doing or offering anything that couldn't be accomplished with an if-else block. I'm not against having one implemented, but I would rate it as extremely low-priority (especially in comparison to some of the other pending requests/features/suggestions).
Title: Re: Unhandled events? [not resolved, sorry]
Post by: Khris on Tue 20/10/2009 19:21:28
I know all that.
The emphasis was supposed to be on the "here" part of the sentence.
Both what and type will only take one of a small set of discreet values, and speed really isn't an issue here at all.

Of course I don't (and didn't) recommend this approach as being better in general.
Title: Re: Unhandled events? [not resolved, sorry]
Post by: Crimson Wizard on Tue 20/10/2009 19:29:00
Quote from: monkey_05_06 on Tue 20/10/2009 18:51:35
Quote from: Crimson Wizard on Tue 20/10/2009 13:53:28*cough* AGS really could benefit from switch command  ::)

I never really saw the major, humongous differences that it would honestly make. I understand how they're used, no problem. But effectively they're not doing or offering anything that couldn't be accomplished with an if-else block.
They are offering more simple syntax in terms of typing and reviewing the code.   ;D
No, I am not complaining, that was just a reaction from seeing all those giant if/else structures posted above ;)
Title: Re: Unhandled events? [resolved]
Post by: rpeeke19 on Tue 12/08/2025 17:32:33
As a beginner I miss the eveents bar below the main panel. I am trying version 4 AGS Is it possible to get the bar back please? IT worked a few yras back, but now it's missing.
Title: Re: Unhandled events? [resolved]
Post by: Crimson Wizard on Tue 12/08/2025 22:55:51
Quote from: rpeeke19 on Tue 12/08/2025 17:32:33As a beginner I miss the eveents bar below the main panel. I am trying version 4 AGS Is it possible to get the bar back please? IT worked a few yras back, but now it's missing.

Please do not necropost, this forum thread was made in 2009, 15+ years ago! It's proper to open a new thread instead.

In regards to your question, what do you call a "events bar" exactly? Is it the list of events for an object, like on this screenshot?
https://adventuregamestudio.github.io/ags-manual/images/acintro3_02.png

Do you see the Properties panel at all? If not, then try one of the following:
1. Go to Windows menu and make sure that Properties is checked.
2. If it's still not visible, try doing Windows -> Layout -> Reset to defaults.