Problem with hotspots [SOLVED]

Started by Stridon, Mon 14/09/2009 11:24:06

Previous topic - Next topic

Stridon

Guys, forgive my English. I am Italian. I have a problem when programming with ags. From now explain it briefly. At the beginning of the game if I see a door with the LOOK command should give me a message (The door is locked) but when I use the key on the door I should give the message (The door is open)
The code is as follows:

(function hHotspot3_Look
if (player.ActiveInventory == iKey)
Display ( "Now the door is 'open');
else Display ( "Is 'the door. Now' locked.");
)

The problem is that if I use the key on another thing (I just touch it) I change the message (The door is open).
I need a code to make that if you use the key sull'hotspot (the door) tells me the phrase (The door is open) and did not use the key to everything.
If I was clear please answer because 10 days is that I'm stuck.

Joe

#1
that code should go in UseInv function rather than hHostspot3_Look
Just select your hostpot, click on Events and doble click on use inv.
This lines will be atomaticly generated:

function hHotspot3_UseInv()
{

}

Now you have to fill that function with your code:
function hHotspot3_UseInv()
{
 if (player.ActiveInventory == iKey)
   Display ( "Now the door is 'open');
 else Display ( "Is 'the door. Now' locked.");
}
Copinstar © Oficial Site

Khris

#2
You'll also need a variable to store the door's state.

Code: ags
bool door_unlocked;

function hHotspot3_Look() {  
  Display("It is a door.");
}

function hHotspot3_Interact() {  // hand cursor
  if (door_unlocked) {
    Display("You open the door and walk through.");
    player.ChangeRoom(2);
  }
  else {
    Display("You try to open the door, but it appears to be locked.");
  }
}

function hHotspot3_UseInv() {
  if (player.ActiveInventory == iKey) {
    if (door_unlocked) Display("Why would I lock it?");
    else {
      door_unlocked = true;  // change variable
      Display ("I've unlocked the door.");
    }
  }   // correction, inserted missing bracket
  else Display ("I can't use that on the door.");  // another inv item was used
}


Edit: thanks dkh, code corrected
Yeah, putting brackets is a matter of personal taste, go with whatever is clearer for you.

And, like Joe Carl said, you have to link the Interact function if you didn't already.

Stridon

The code that you gave me has an error. Perhaps you made a mistake in typing. If I rewrite and then everything goes well, I am in your debt for life

Akatosh

What is the error message, stridon?

Stridon

He says PARSE ERROR ELSE final.
I'm talking about the code that gave me Kris.
I copied the same but tells me that error

DoorKnobHandle

Code: ags

function hHotspot3_UseInv()
{
  if (player.ActiveInventory == iKey)
  {
    if (door_unlocked) 
      Display("Why would I lock it?");
    else
    {
      door_unlocked = true;  // change variable
      Display ("I've unlocked the door.");
    }
  }
  else
    Display ("I can't use that on the door.");  // another inv item was used
}


Try this. The version before had wrong brackets. I personally think the way I formatted that makes brackets a lot clearer actually, I'd recommend it.

Joe

Anyway it won't work if you just copy the code... you alzo have to set those functions names in the events panel of your hotspot
Copinstar © Oficial Site

Stridon

Thanks so much. A thousand times thank you. Without you I would still be weird to try to run commands. Now I just wanted someone to tell me briefly what I did with the codes that you gave me

Khris

You know about linking actions to functions and if-else, so there's really not much to add.
I've declared a variable "door_unlocked" of type bool, i.e. it can hold true or false. It there's no initial value stated (e.g. "int bool door_locked = true;") then a bool is false.
I've put the declaration before the functions that will need to access it. That way, the variable keeps its value (even if you leave the room and return).
Then, where necessary, I check/change the value.

SMF spam blocked by CleanTalk