How do I use an Item on a Hotspot or so? [Solved]

Started by Kain_V, Sun 15/03/2009 16:11:39

Previous topic - Next topic

Kain_V

Hi everyone, I just searched manual and forum for this, tried a few things and still came empty handed.... :(
My question (maybe a bit silly though) is this: How do I use an inventory Item on a Hotspot, Object or Character?
Thanks in advance! :D

Khris

Just to be clear: you mean how do you find out about and react to the player using an inventory item on something?
Select the hotspot/object/character and in the events pane add a function to the "use inv on ..." event.

The function is created and inside you can check for the used item, e.g. like this:
Code: ags
function oDesk_UseInv() {
  if (player.ActiveInventory == iKey) {
    if (!desk_unlocked) {
      desk_unlocked = true;
      Display("You unlock the desk using the brass key.");
    }
    else Display("Why would you lock it?");
  }
  else Display(String.Format("You want to put the %s on the desk but reconsider.", player.ActiveInventory.Name));
}

Kain_V

Yes that was what I wanted to know.
Thank you very much KhrisMUC!

I had already tried some things myself but the only thing I did was to mess the whole script file...  ::)
Anyways, thanks again!

Shockbolt

I'm having the same type of problem and I still can't resolve it from the codes above.

I want to learn how to take an item from the inventory and use it to open up or manipulate something on the screen.
When I use Your codes and change the names to what I have on my screen, I still get an error message saying:
room1.asc(44): Error (line 44): undefined symbol 'Keyhole1_unlocked'

I have a silver key - iKey in my inventory and a hotspot - hKeyhole1 that I want to use the key to open up, then be able to either search what's inside or have the character climb inside/be transferred to another screen by choice of entering the hole.

Here's what I wrote instead fo what You wrote in the code above:

function hKeyhole1_UseInv() {
  if (player.ActiveInventory == iKey) {
    if (!Keyhole1_unlocked) {
      Keyhole1_unlocked = true;
      Display("You unlock the rock using the silver key.");
    }
    else Display("Why would you lock it?");
  }
  else Display(String.Format("You want to put the %s on the rock but reconsider.", player.ActiveInventory.Name));



OneDollar

An undefined symbol error means that you are trying to change, or find, the value of a variable without first telling AGS that the variable actually exists. In this case the lines...
Code: ags

if (!desk_unlocked) {
  desk_unlocked = true;

mean "If the variable desk_unlocked is set to false, then set the variable desk_unlocked to true. If you've not defined this variable first (with a line saying something like bool desk_unlocked = false; at the top of the function or script, AGS will give you an undefined symbol error.

(As a side note, writing if (!desk_unlocked) { is a perfectly valid bit of code, but you could also write it if (desk_unlocked == false) {)

If you're still having trouble understanding the code KhrisMUC wrote I can only suggest that you go through both the main tutorial and especially the scripting tutorials in the manual (which you should have done anyway). Once you understand what each line does you'll be able to follow the logic to personalise the code for your own use.

SMF spam blocked by CleanTalk