Unhandled events? [resolved]

Started by Sirpunchula, Mon 19/10/2009 01:06:38

Previous topic - Next topic

Sirpunchula

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:

Code: ags
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?
Pardon my deadpan.

Khris

Put this in the global script:

Code: ags
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.)

Sirpunchula

#2
I think I have the first part covered, but I don't know what I need to put in the "..." after
Code: ags
else if (type ==2)
Can you clarify that for me, please?
Pardon my deadpan.

monkey0506

The manual gives a list of the values 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:

Code: ags
    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.

Sirpunchula

This seems to work, but every time I insert this code:

Code: ags
 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?
Pardon my deadpan.

Crimson Wizard

#5
Quote from: Sirpunchula on Mon 19/10/2009 23:00:24
This seems to work, but every time I insert this code:

Code: ags
 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?



Code: ags
 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.

Sirpunchula

#6
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:

Code: ags

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?
Pardon my deadpan.

Lufia

Your code reads as:
Code: ags
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.

Code: ags
if (what == 1) {
  if (type==1) {
  }
  else if (type==2) {
  }
  // ...
else if (what==2) {
  if (type==1) {
  }
  else if (type==2) {
  }
  // ...
}
// ...

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.

Code: ags

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
   }
}


Sirpunchula

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:

Code: ags

 //  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?
Pardon my deadpan.

ThreeOhFour

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:

Code: ags

 //  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 :)

Ryan Timothy B

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

Code: ags
 //[..]
//  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
      }
   }

RickJ

I just checked myself and the Unhandled Inventory has the same problem.  The  first few lines should be changed to this
Code: ags

   // 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.
Code: ags

   //  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
      }
   }


ThreeOhFour

Still missing that closing bracket and semicolon in that script under type==2, Rick  ;D

RickJ

Thanks Ben,  your eyes are better than mine  cause I can't spot it.   ;D

ThreeOhFour

Code: ags
      else if (type==2) {  //  3    2   Speak to character
         Display("You get no response."
      }


Should be

Code: ags
      else if (type==2) {  //  3    2   Speak to character
         Display("You get no response.");
      }

Khris

#16
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:

Code: ags
  //  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 );

Crimson Wizard

*cough* AGS really could benefit from switch command  ::)

monkey0506

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:

Code: ags
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:

Code: ags
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).

Khris

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.

SMF spam blocked by CleanTalk