Hpl2:Tutorials:script:levers and secretshelfs

From Frictional Wiki
Jump to navigation Jump to search

Levers that opens secret shelfs

In this tutorial I (xtron) will show you! (the reader) how to make levers that opens secret bookshelfs!.

The Bookshelf

First off you need to create a moveable bookshelf.

Go to Entities(7) > Gameplay > Look for "shelf_secret_door_rot" and place it where ever you want it. Change it's name to your liking.

secondly you need to create a script area that the shelf will rotate around.

Go to Areas(8) > Select "script" and create an area like so:

img6.imageshack.us/img6/4914/shelf1.png

(If that link does not work, try this: puu.sh/9UrRK/721008bb1d.jpg)

When you're done adding that area, name it anything for example: rotate.

Then press on the bookshelf and go to Entity. The last text box named AngularOffsetArea, you'll need to type in the rotate area you

just created like so:

img155.imageshack.us/img155/948/shelf2.png

(If that link does not work, try this: puu.sh/9Us9N/ac50a930f0.jpg)

Now you're done with the bookshelf!. Let's setup the lever.

Adding the Lever

You will be needing a Lever to make the Bookshelf open so go to

Entities > Gameplay > Look for lever_simple01. You can choose any of those levers. Change the Levers name to your liking.

The lever is now DONE!. Easy ayee?. Let's get working on the script then.

The Script

We will first fix the code that is needed in the void OnStart() area.

void OnStart()
{
SetEntityConnectionStateChangeCallback("lever", "func_shelf");
}

Change "lever" to the name of your Lever you created in the previous part.

Now we need to setup the "move-the-bookshelf-when-lever-is-pressed"

Copy this code and insert it anywhere but void OnStart()

void func_shelf(string &in asEntity, int alState)
{
     if (alState == 1)
     {
     SetMoveObjectState("shelf",1.0f);
     PlaySoundAtEntity("", "quest_completed.snt", "shelf_move_1", 0, false);
          return;
     }
}

Change "shelf" to your shelfs name.

The SetEntityConnectionStateChangeCallback will execute SetMoveObjectState and PlaySoundAtEntity when the levers State turned to 1. The lever's starting position is always 0.

This is how the complete script should look like:

void OnStart()
{
SetEntityConnectionStateChangeCallback("lever", "func_shelf");
}

void func_shelf(string &in asEntity, int alState)
{
     if (alState == 1)
     {
     SetMoveObjectState("shelf",1.0f);
     PlaySoundAtEntity("", "quest_completed.snt", "shelf_move_1", 0, false);
          return;
     }
}

More Than One Lever

Simply add another lever through the Level editor and change it's name to your liking.

Add this to void OnStart()

SetLocalVarInt("Var1", 0);

SetEntityConnectionStateChangeCallback("lever_1", "func_shelf_1");

change lever_1 to the second lever you added.

Add this code anyhere but void OnStart()

void func_shelf(string &in asEntity, int alState)
{
     if (alState == 1)
     {
     AddLocalVarInt("Var1", 1);
     func01();
     }
}

void func_shelf_1(string &in asEntity, int alState)
{
     if (alState == 1)
     {
     AddLocalVarInt("Var1", 1);
     func01();
     }
}

void func01()
{
if(GetLocalVarInt("Var1") == 2)
    {
    SetMoveObjectState("shelf",1.0f);
    PlaySoundAtEntity("", "quest_completed.snt", "shelf", 0, false);
    }
}

Var1 will start at the count 0 and everytime you drag a lever the count raises by 1 so when you draged both of the levers the count is at 2.

When the count is 2 func01 gets executed and activates SetMoveObjectState and PlaySoundAtEntity.

The complete script should look like this:

void OnStart()
{
SetLocalVarInt("Var1", 0);
SetEntityConnectionStateChangeCallback("lever", "func_shelf");
SetEntityConnectionStateChangeCallback("lever_1", "func_shelf_1");
}

void func_shelf(string &in asEntity, int alState)
{
     if (alState == 1)
     {
     AddLocalVarInt("Var1", 1);
     func01();
     }
}

void func_shelf_1(string &in asEntity, int alState)
{
     if (alState == 1)
     {
     AddLocalVarInt("Var1", 1);
     func01();
     }
}

void func01()
{
if(GetLocalVarInt("Var1") == 2)
    {
    SetMoveObjectState("shelf",1.0f);
    PlaySoundAtEntity("", "quest_completed.snt", "shelf", 0, false);
    }
}

1 = drag down

-1 = drag up

I hope you find this tutorial usefull. Contact on FG forum if you want any specific tutorial or got any questions.

Thanks for reading.

created by xtron