Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Raider on Mon 01/05/2006 10:54:41

Title: Unhandled event not working
Post by: Raider on Mon 01/05/2006 10:54:41
Okay so I have my unhandled event working for my items that don't 'combine' with other items. If i have two items that combine for example a bottle and cork. They go together to make a plugged bottle. The problem is if I use any other object with either the cork or the bottle nothing is displayed. I have searched the manual and the forums for this solution but can't find it. I hope I have explained in full. How do I get the game to display "I can't use these things together." when say the cork is used with another object.
Title: Re: Unhandled event not working
Post by: Ashen on Mon 01/05/2006 11:58:04
IIRC, unhandled_event won't be called for an interaction that has any conditions - so if there's script for using item 2 on an item, using item 3 doesn't count as 'unhandled'. The (slightly longwinded) solution is to manually tell the script to run unhandled_event when needed, e.g.:

if (player.ActiveInventory == iKey) {
  // Do stuff
}
else if (player.ActiveInventory == iChicken) {
  // Do other stuff
}
else unhandled_event(5,3); // 'Use inv on inv', for any other item.


I don't think there's a simpler way, sorry.
Title: Re: Unhandled event not working
Post by: Raider on Tue 02/05/2006 06:56:24
Thank you
Title: Re: Unhandled event not working
Post by: Raider on Tue 02/05/2006 07:08:59
Ah okay I used that code and it says "i Active inventory is not a member of 'gameCharacter'
was that code that you posted just a sample or what? 
Title: Re: Unhandled event not working
Post by: Gilbert on Tue 02/05/2006 07:32:36
Did you type that in correctly? It should be:

ActiveInventory

with capital A and I but NO space.

If you're not at all sure, just rely on the auto-completion feature of teh script editor.
Title: Re: Unhandled event not working
Post by: Raider on Tue 02/05/2006 08:53:39
No I have copied the code straight from Ashen's post. It does not seem to work and the error keeps coming up. What about using "while" statements?
Title: Re: Unhandled event not working
Post by: Gilbert on Tue 02/05/2006 09:04:06
So, did you check if there's a error in the code? you may also try erasing the "ActiveInventory" part of the code and type in the first few characters (like "activ") to make the editor auto-complete it for you.

Lastly, what version of AGS are you using? The ActiveInventory character property won't work unless you're using V2.7 or later.
Title: Re: Unhandled event not working
Post by: Raider on Tue 02/05/2006 09:06:02
V2.61

okay what about the "GetProperty" statements? And the property box?
Title: Re: Unhandled event not working
Post by: Gilbert on Tue 02/05/2006 10:08:36
If you're using V2.61, try the following instead:

if (character[GetPlayerCharacter()].activeinv == 5) {
Ã,  // Do stuff
}
else if (character[GetPlayerCharacter()].activeinv == 6) {
Ã,  // Do other stuff
}
else unhandled_event(5,3); // 'Use inv on inv', for any other item.

(Substitute, the 5 and 6 in the code with the item numbers of the appropiate items)

Quote from: Rahvin on Tue 02/05/2006 09:06:02
okay what about the "GetProperty" statements? And the property box?

Pardon ???
Title: Re: Unhandled event not working
Post by: Raider on Tue 02/05/2006 10:16:19
Okay thanks heaps.
Ignore what I said, I was wrong.  :P
Title: Re: Unhandled event not working
Post by: Raider on Wed 03/05/2006 05:18:58
I apologise for being annoying...
But how do you go about displaying a message if two items don't work together? ???
Title: Re: Unhandled event not working
Post by: Ashen on Wed 03/05/2006 11:47:52

Display("They don't work together.");

?

How do you mean?
In that manual, look up the game. variables and looks for something like game.inv_activated (I'm fairly certain this was still the same in V2.61, but best to be sure).

Then do something like:

Display ("I can't use %s with %s.", character[GetPlayerCharacter()].activeinv, game.inv_activated);


Or if you meant something else, can you be more specific?
Title: Re: Unhandled event not working
Post by: Raider on Thu 04/05/2006 06:42:43
What I mean is: do I have to enter in a code for each combination of items being used together.
For example if "use" cork with knife, I want it to display "that doesn't work"
Do I have to type in:

Display ("I can't use %s with %s.", character[GetPlayerCharacter()].activeinv, game.inv_activated);\

Then if i "use" the bread with the flag do I have to put that code in again but with different variables?
It's hard to explain  :-\
Title: Re: Unhandled event not working
Post by: Khris on Thu 04/05/2006 08:17:21
Add this in the global script:
function unhandled_event (int what, int type) {
Ã,  if (what==5) {Ã,  Ã,  // inventory interaction
Ã,  Ã,  if (type==3) {Ã,  Ã, // used inv
Ã,  Ã,  Ã,  Display ("I can't use %s with %s.", character[GetPlayerCharacter()].activeinv, game.inv_activated);
Ã,  Ã,  }
Ã,  }
}


Explanation:
After the player uses item A on item B, AGS does the following:

-character[GetPlayerCharacter()].activeinv is already A
-game.inv_activated is set to B
-if it exists, B's use inv on inv-interaction is called, else unhandled_event(5,3) is called
Title: Re: Unhandled event not working
Post by: Raider on Thu 04/05/2006 08:29:39
yes that works to some degree but if I use say an item that has no commands in the interaction window, like a "book" with the cork which does have commands in the interaction window (cork with bottle) then it doesn't count as an unhandled event. When I test my game and i use book on mast it displays nothing but if i use the mast on the book, it displays the message I want.
Is there any way I can display the message "that doesn't work" for every combination of items I have without doing writing a code for each one?
Title: Re: Unhandled event not working
Post by: Ashen on Thu 04/05/2006 10:23:12
Quote from: Ashen on Mon 01/05/2006 11:58:04
IIRC, unhandled_event won't be called for an interaction that has any conditions - so if there's script for using item 2 on an item, using item 3 doesn't count as 'unhandled'. The (slightly longwinded) solution is to manually tell the script to run unhandled_event when needed, e.g.:

(code removed)

I don't think there's a simpler way, sorry.

Does this not work? You don't have to specify the actual interaction (Display ("I can't use %s with %s.", character[GetPlayerCharacter()].activeinv, game.inv_activated);) for each item, but if there's ANY code in the 'Use inventory on' interaction then yes, you do have to write a code for each - but it's just a case of copy-pasting else unhandled_event(5,3); at the bottom.
Title: Re: Unhandled event not working
Post by: Raider on Fri 05/05/2006 05:11:35
No that code that was given did not work.
Neither does (Display ("I can't use %s with %s.", character[GetPlayerCharacter()].activeinv, game.inv_activated);)

Unless I am putting them in the wrong place? Im putting them in the unhandled event section of the global script.
Title: Re: Unhandled event not working
Post by: SSH on Fri 05/05/2006 09:26:35
You will also need to put the code in the ELSE clause of each of your object interactions:

if (used cork with bottle) {
Ã,  // Do something meaningful
} else {
Ã,  Display ("I can't use %s with %s.", character[GetPlayerCharacter()].activeinv, game.inv_activated);
}
Title: Re: Unhandled event not working
Post by: Ashen on Fri 05/05/2006 11:07:29
Sorry, you're right, the code exactly as given won't work. character[GetPlayerCharacter()].activeinv and game.inv_activated are ints not strings, the number of the items used, not their names. Not sure how I missed that one - but it should've given you an error message, rather than just not working. You'll need to look up the usage for GetInvName (I don't have a copy of the 2.61 manual, so I'll have to guess), then try:

string UsedInv, UsedOn;
GetInvName (character[GetPlayerCharacter()].activeinv, UsedInv); // Check the usage before trying this
GetInvName (game.inv_activated, UsedOn); // Or whatever the correct usage is
Display ("I can't use %s with %s.", UsedInv, UsedOn);


Now, if that was the problem you should be sorted - I've used the code more or less as KhrisMUC posted it in the past and it works fine. If not, can you be more specific about what it is/isn't doing that's wrong?

(And you shouldn't really need to put it in the interaction for every item, provided the else unhandled_event (5,3); line is there.)
Title: Re: Unhandled event not working
Post by: Raider on Sat 06/05/2006 00:29:39
No that didn't work either.
What is happening is if I "use" cork on map. It doesn't my character display anything, he doesn't say what I have written in the script. It just goes back to normal playing as if nothing happend.
In all adventure games if you use two items that don't work together the character will say "That doesn't work" (or somthing along those lines)
Now I have put in the codes you suggested and when I use two items together that don't work my character doesn't say anything, nothing happens in the game.
Is that explained well enough?  :-\
Title: Re: Unhandled event not working
Post by: Ashen on Sat 06/05/2006 12:26:05
It's explained OK, but it still doesn't make sense - the code should work, it's worked in the past, and it's strange that it doesn't work for you. Does it work if yu do what SSH suggested, and have the 'You can't use X on Y' message in the item script?

Can you post your code (unhandled_event and an item or two should do)?
Title: Re: Unhandled event not working
Post by: Raider on Sun 07/05/2006 03:33:58
Okay this is my unhandled_event code
function unhandled_event (int what, int type) {
  if (what==5) {    // inventory interaction
    if (type==3) {   // used inv
      Display ("I can't use %s with %s.", character[GetPlayerCharacter()].activeinv, game.inv_activated);
    }
string UsedInv, UsedOn;
GetInvName (character[GetPlayerCharacter()].activeinv, UsedInv); // Check the usage before trying this
GetInvName (game.inv_activated, UsedOn); // Or whatever the correct usage is
Display ("I can't use %s with %s.", UsedInv, UsedOn);
unhandled_event(5,3); // 'Use inv on inv', for any other item.
  }
}



What did you mean about posting the items? Did you mean to post the interactions or just list them or what?
Title: Re: Unhandled event not working
Post by: SSH on Sun 07/05/2006 06:23:52
Unhandled event only runs when there is no interaction, Rahvin. If there is an interaction, you must put an ELSE clause in your if statements to display"That doesn't work" for cases where the wrong object is used. This needs to be done in EVERY inventory item interaction that you have already set up. You don't need to create new interactiosn, though.
Title: Re: Unhandled event not working
Post by: Raider on Sun 07/05/2006 11:28:49
Okay I think I'm beginning to understand now, sorry i've been a bit slow.
Just to make it clear, what do I write exactly and where? Could you just tell me the coding for it and tell me where I put the coding?
Thanks, a lot for your patience guys.
Title: Re: Unhandled event not working
Post by: Ashen on Sun 07/05/2006 11:58:15
The unhandled_event can probably stay where it is, but needs to look like:
function unhandled_event (int what, int type) {
  if (what==5) {    // inventory interaction
    if (type==3) {   // used inv
      string UsedInv, UsedOn;
      GetInvName (character[GetPlayerCharacter()].activeinv, UsedInv); // Check the    usage before trying this
      GetInvName (game.inv_activated, UsedOn); // Or whatever the correct usage is
      Display ("I can't use %s with %s.", UsedInv, UsedOn);
    }
  }
}

(Note: It'll need to be BEFORE the inventory item functions in the Global Script, for the next bit to work.)

Then, at the end of the interaction scripts for items which have can be used with other items add:

else unhandled_event(5,3);

So, if the bottle is item 4 and the cork is item 6, you'd have something like:

  // script for Inventory item 4 (Bottle): Use inventory on this item
if (character[GetPlayerCharacter()].activeinv == 6) { // Use cork on bottle
  // Do stuff
}
else unhandled_event(5,3); // Use anything else on bottle



You can, as SSH says, replace the unhandled_event(5,3); in the item scripts with a Display() command (and the GetInvName commands, if needed). This wouldn't actually be much more work, if you just copy-pasted the same default mesage, and would allow you to tailor some responses to the item clicked on (e.g. Display ("I could use %s, but I don't want to break the bottle", UsedInv) rather than a generic "That doesn't work").