• Register

An extensively updated version of HPL1 engine. Uses industry standard technologies such as OpenGL, OpenAL and Newton Game Dynamics.

Report RSS Tut #5: Scripting a key.

In this tutorial, I teach you how to correctly script a key to work in Amnesia: The Dark Descent.

Posted by on - Basic Client Side Coding

Hi, welcome back.

In this tutorial I'll teach you the basics of scripting a simple key. Now, to start, remember that "DoorSlam" function we made earlier? We're going to edit it a little bit. This time, put

cpp code:
SetSwingDoorLocked("ScareDoor1", true, true);

SetSwingDoorLocked, which as you would think, locks the swing door (or unlocks it).

In the quotes there, we're declaring which door we want locked/unlocked.

true/false would mean locked/unlocked

Your script file should simply look like this:



Now that the door is locked, we can begin working on the key that we want to use.

First off, add a key into your level. I put mine on top of the desk that I placed earlier, with key_study


Now, let's get into the scripting part of it.

Put this in your void OnStart() function:

cpp code:
AddUseItemCallback("", "key_study_1", "ScareDoor1", "KeyOnDoor", true);

The first "" aren't needed, they're just for the internal name. Then, we call what item we're using, on what entity (key on scaredoor1), then, we name the function that it'll direct to, ie KeyOnDoor. So, let's make a quick KeyOnDoor function.

cpp code:
void KeyOnDoor(string &in asParent, string &in asChild, int alState)
{
}

It's as simple as that. Now inside the function, let's make it so the door actually unlocks.

Remember "SetSwingDoorLocked"? And now the difference is just true/false to lock/unlock? Well that's what you're doing here.

cpp code:
void KeyOnDoor(string &in asParent, string &in asChild, int alState)
{
  SetSwingDoorLocked("ScareDoor1", false, true);
}

That's all you have to do for a working unlock system. But, let's put a bit more stuff in it for it to work... better.

When they unlock the door, we want them to be able to hear an unlocking sound so they know that they unlocked it. Simply put;

code:
PlaySoundAtEntity("", "unlock_door", "ScareDoor1", 0, false);

PlaySoundAtEntity, again, "" is just an internal name. The second "" is the sound inside the game files, the third "" is which entity the sound plays at.

Ok, so, we got that. One last thing, though! Let's make it so the key LEAVES the players inventory when they use it. This is a REALLY simple one.


cpp code:
RemoveItem("key_study_1");

I don't have to explain this one, but just incase, I will. RemoveItem, and inside RemoveItem, you specify WHAT item you want to remove. That's really all. So, now, your script file should, in total, look like this.

cpp code:
void OnStart()
{
  AddEntityCollideCallback("Player", "DoorScareScript", "DoorSlam", true, 1);
  AddUseItemCallback("", "key_study_1", "ScareDoor1", "KeyOnDoor", true);
}
void DoorSlam(string &in asParent, string &in asChild, int alState)
{
  SetSwingDoorClosed("ScareDoor1", true, true);
  SetSwingDoorLocked("ScareDoor1", true, true);
}
void KeyOnDoor(string &in asParent, string &in asChild, int alState)
{
  SetSwingDoorLocked("ScareDoor1", false, true);
  PlaySoundAtEntity("", "unlock_door", "ScareDoor1", 0, false);
}

Thanks for reading my fifth tutorial, request anything in the comments below!

Post a comment

Your comment will be anonymous unless you join the community. Or sign in with your social account: