Specific question about Open/Close Doors.

Started by Dirty harry, Fri 22/11/2013 13:27:46

Previous topic - Next topic

Dirty harry

Hi for all.
This is my first post here and i hope this is the right place.
I just start with AGS and i'm trying to script a game,i have search all the forum
and didn't find a answer for my specific problem.
I have a hotspot(named:Door) in Room 1 and when i use a key on it,the character go to another room(Room 2):

Here the script:

function Door_UseInv()
{
if(cCharacter.ActiveInventory==iKey)
cCharacter.Walk(262, 160, eBlock, eWalkableAreas);
cCharacter.ChangeRoom(2, 61, 76);
}

OK..it's work fine.The problem is,when i go back to Room 1,i need to do
the same action again to go into Room 2(use the key,etc.)

What i want:
1-Go to the Door in Room 1,the Door is locked.
2-When i use the key on it,the Door is unlocked,go to Room 2.
3-Go Back to Room 1.
4-Go to the Door again,now i don't need to use the key on it,the door is already unlocked.
I just want to use the Interaction click on it to go Room 2 again.
That's it guys,i hope to find a help about this.
(Sorry for my very bad english).
Thank you.


EliasFrost

#1
You could use a trigger that checks whether you have used the key already. Like this (pseudo script)

Code: ags
bool keyused = false;
when interacting:
    if (keyused == true)
    {
        change room
    else
    {
        don't change room
    }

when using key on door:
    remove key from inventory
    set keyused to true
    alternatively change room


This will create a trigger(keyused) that switches to true only after you've used the key on the door, and unless the trigger is true, you can't open the door.

Dirty harry

#2
Er...thanks a lot but,i have a little problem with the process of making a Bool declaration.
I have already try by creating a Bool for this,but when i turn into
the script code and make a "if" declaration,it create a "parse error" on it.
Can you make a step by step about the bool part?
Thanks for the answer!

EliasFrost

#3
A bool is basically a two-value data-type (true or false, 1 or 0, respectively) and the way to declare a bool is simple, just like any other variable:

Code: ags
bool keyused = false;


The parse error means that you've basically made a spelling error, in programming terms, could you include your code?

Khris

This should do it:

Code: ags
bool door_unlocked = false;

function Door_Interact()
{
  cCharacter.Walk(262, 160, eBlock);
  if (door_unlocked) cCharacter.ChangeRoom(2, 61, 76);
  else cCharacter.Say("The door is locked.");
}

function Door_UseInv()
{
  cCharacter.Walk(262, 160, eBlock);

  if(cCharacter.ActiveInventory == iKey)
  {
    if (door_unlocked) cCharacter.Say("I don't want to lock it.");
    else
    {
      cCharacter.Say("The key fits.");
      door_unlocked = true;
      cCharacter.ChangeRoom(2, 61, 76);
    }
  }
  else cCharacter.Say("That doesn't work.");
}


Note that in your original snippet, only the Walk command's execution depended on the ActiveInventory check; to run multiple commands conditionally, put them in curly brackets.

Dirty harry

Just one question:
Where i put this words,"bool door_unlocked = false;"
in the global variables or in the room script?
Sorry for the questions,i'm a real noob in AGS.

EliasFrost

#6
You put it at the top of the same script that your functions are (which is your room script), that way both functions can access it. If you put it in global variables, that makes the variable accesible from all areas of your game, you don't want that unless you need to access the door_unlocked variable from any other script than your room script.

Dirty harry

#7
It works!!!!!!!!!!!!!!!
Thank you guys!!!!!!!!!

SMF spam blocked by CleanTalk