Scripting question. (player not to find key until they've tried to open the door

Started by yourpinkfloralhaze, Fri 04/09/2020 00:05:15

Previous topic - Next topic

yourpinkfloralhaze

Hi,

I'm in my first room on my first game, I've set up the script so the player has to unlock the front door. I just want to know how to run the script so the hotspot where the key is hidden displays a different message and doesn't provide the player with a key until they have tried to open the front door first.

Many thanks,
for any help.

Khris

You are looking for variables. All you need is boolean room variables, like this:

Code: ags
// just put them in the room script, further up than the functions using them
bool hasCheckedDoor; // initial value is false
bool doorUnlocked;

function oDoor_Interact() {
  hasCheckedDoor = true;
  if (doorUnlocked) player.ChangeRoom(2, ...);
  else Display("It seems to be locked.");
}

function oDoor_Useinv() {
  if (player.ActiveInventory != iKey) Display("That doesn't work.");
  else {
    if (doorUnlocked) player.Say("I don't want to lock it again, I need to get out of here.");
    else {
      doorUnlocked = true;
      Display("The key fits, and you unlock the door.");
    }
  }
}

function hGlint_Look() {
  if (hasCheckedDoor) {
    player.Say("Maybe the key fell between the floor boards? Better check it out.");
    ...
  }
  else player.Say("Nah, I have to get out of here!");
}

Reiter

Khris's solution is undoubtedly the most elegant, but you can also use variables as counters. There is a nice example in the electric AGS manual (in the AGS program folder, by default), under Contents â†' Scripting â†' Scripting Language â†' Scripting Tutorial part 1, under Variables. It is well worth looking up and experimenting with.

Put briefly, you can make a Variable. This is effectively a way to tell the game to keep track of certain things. In this case, if the player has tried the door before trying the door mat, for example. Variables start at zero, by default, but you can change this if you wish.
It is a good idea to practice at them, since there is a lot of things you do in AGS by setting and checking variables.

Making a variable is easy. In the room script, write:

int FrontDoorCounter;

This makes a tidy little variable, set at zero as default. We can tell the door mat hotspot what to do based on the variable number.

For this example, we shall say that:

At 0, the player have not yet interacted with the door.

At 1, the player have tried the door, but does not have the key yet.

At 2, the player has gotten the key, and the door mat is no longer of interest.

At 3, the player has unlocked the door. They can now go to the next room.

Let us look at the door, first. In the Interact event, put:

if (FrontDoorCounter == 0)
{
Display (“You try the door, but it is firmly locked”);
cPlayer.Say (“Blast!”);
FrontDoorCounter = 1;
}

So, when the player interact with the door for the first time, they are told it is locked, while the front door counter is set from zero to one.

If (FrontDoorCounter == 1)
{
Display (“You need a key to open the door.);
}
if (FrontDoorCounter == 2)
Display (“It looks like the key that you found would fit this door.”);
}
if (FrontDoorCounter == 3)
{
cPlayer.Say (“Co-ee! Anyone home?”);
cPlayer.ChangeRoom (etc)
}
}

Very simple. We unlock the first condition by interacting with the door, the second by clicking the door mat, and the third by unlocking the door. The third makes the door hotspot change us to the next room.


Of course, the Unlock Door with Key is a different event. In it, remember to write

FrontDoorCounter = 3;

to advance the variable to the third setting.

Now, then, to the interaction events with the door mat hotspot.

if (FrontDoorCounter == 0)
{
Display (“What would you want with that? It is hideous.”);
}
if (FrontDoorCounter == 1)
{
Display (“You peek under the door mat, and find a key.”);
cPlayer.Say (“The oldest trick in the book!”);
cPlayer.AddInventory (iFrontDoorKey);
FrontDoorCounter = 2;
}
if (FrontDoorCounter == 2)
{
Display (“You got what you wanted from it.”);
}
if (FrontDoorCounter == 3)
{
Display (“You consider putting the key back, but decide to keep it. Just in case.”);
}
}

I do recommend reading the section mentioned in the manual, as it gives a much more detailed explanation as to how variables work, and how you use them.

As noted before, I believe Khris's method to be much more elegant, but this is a very basic method of working with variables.

yourpinkfloralhaze

Thank you so much for this,
It works :)

I read the manual and plan to use variables a lot, I can think of numerous things they'd be useful for.

SMF spam blocked by CleanTalk