Frictional Games Forum (read-only)
[REQUEST] How to make several keys in on map - Printable Version

+- Frictional Games Forum (read-only) (https://www.frictionalgames.com/forum)
+-- Forum: Amnesia: The Dark Descent (https://www.frictionalgames.com/forum/forum-6.html)
+--- Forum: Custom Stories, TCs & Mods - Development (https://www.frictionalgames.com/forum/forum-38.html)
+---- Forum: Development Support (https://www.frictionalgames.com/forum/forum-39.html)
+---- Thread: [REQUEST] How to make several keys in on map (/thread-9813.html)



How to make several keys in on map - Khan - 08-17-2011

kk, so I have a key already, but, I dont know to make another key, can anyone help me here? lol. Here is wat my .hps looks like.

////////////////////////////
// Run when entering map
void OnEnter()
{
AddUseItemCallback("", "chamberkey", "chamberdoor", "FUNCTION", true);
AddEntityCollideCallback("Player", "script_slam", "func_slam", true, 1);
AddEntityCollideCallback("Player" , "doorbreak" , "MonsterFunc1" , true , 1);
AddEntityCollideCallback("Player", "doorslamtrigger1", "Func01", true, 1);
}
void FUNCTION(string &in asItem, string &in asEntity)
{
SetSwingDoorLocked("chamberdoor", false, true);
PlaySoundAtEntity("", "unlock_door", "door1", 0, false);
RemoveItem("chamberkey");
SetSwingDoorClosed("doorslam", true, true);
SetSwingDoorClosed("doorslam2", true, true);


As you can see, I have a key already, but I dont know how to make another, I cut off part of the script as to hide the other, surprises, in my custom story. any ideas?


RE: How to make several keys in on map - Your Computer - 08-17-2011

The game relies on the name of these entities. Just change the name for each key and have conditional statements for each key.


RE: How to make several keys in on map - Khan - 08-17-2011

Im a complete nooby to scripting, so, I have no clue half of wat you said, lol ;(


RE: How to make several keys in on map - Your Computer - 08-17-2011

(08-17-2011, 04:27 AM)Khan Wrote: Im a complete nooby to scripting, so, I have no clue half of wat you said, lol ;(

Tongue

As you should already know, AddUseItemCallback() calls a function upon using an item on another entity. In your case this is FUNCTION(). The variables passed to this function contain the names of the entities that were interacted with. In your case, asItem would contain the name of the key; asEntity would contain the name of the door. A conditional statement is a statement that requires its expression (that which is inside its parentheses) to evaluate to true (boolean) before executing the code in the code block below it. The if statement is therefore a conditional statement.

In your FUNCTION() function, you would place conditional statements to check whether or not asItem contains the name of the key used and whether or not asEntity contains the name of the door you want the key to work on. This allows the same function to be used for multiple keys. It would also allow for the same key to be used on multiple doors.

Example:
Code:
void FUNCTION(string &in asItem, string &in asEntity)
{
     if (asItem == "chamberkey" && asEntity == "chamberdoor")
     {
          // unlock door and remove key from inventory
     }

     else if (asItem == "cellarkey" && asEntity == "cellardoor")
     {
          // unlock door and remove key from inventory
     }
}

In the level editor you would name the keys and doors accordingly. If you want any key to be used on any door, then remove the second half of the if statement's expression and don't remove the key from inventory.


RE: How to make several keys in on map - Khan - 08-17-2011

I think I messed up :|

////////////////////////////
// Run when entering map
void OnEnter()
{
AddUseItemCallback("", "chamberkey", "chamberdoor", "FUNCTION", true);
AddUseItemCallback("", "cellerkey", "cellerdoor", "FUNCTION", true);
AddEntityCollideCallback("Player", "script_slam", "func_slam", true, 1);
AddEntityCollideCallback("Player" , "doorbreak" , "MonsterFunc1" , true , 1);
AddEntityCollideCallback("Player", "doorslamtrigger1", "Func01", true, 1);
}
void FUNCTION(string &in "chamberkey", string &in "chamberdoor")
{
SetSwingDoorLocked("chamberdoor", false, true);
PlaySoundAtEntity("", "unlock_door", "door1", 0, false);
RemoveItem("chamberkey");
SetSwingDoorClosed("doorslam", true, true);
SetSwingDoorClosed("doorslam2", true, true);
}
void FUNCTION(string &in "cellerkey", string &in "cellerdoor"
{
SetSwingDoorLocked("cellerdoor", false, true);
PlaySoundAtEntity("", "unlock_door", "door1", 0, false);
RemoveItem("cellarkey);
}

I have like, no idea wat Im doing, so I tried wat I THOUGHT would work, it didint, now my game crashes when I try to start it ;(


RE: How to make several keys in on map - Obliviator27 - 08-17-2011

Code:
////////////////////////////
// Run when entering map
void OnEnter()
{
   AddUseItemCallback("", "chamberkey", "chamberdoor", "FUNCTION1", true);
   AddUseItemCallback("", "cellarkey", "cellerdoor", "FUNCTION2", true);
   AddEntityCollideCallback("Player", "script_slam", "func_slam", true, 1);
   AddEntityCollideCallback("Player" , "doorbreak" , "MonsterFunc1" , true , 1);    
   AddEntityCollideCallback("Player", "doorslamtrigger1", "Func01", true, 1);    
}
void FUNCTION1(string &in asItem, string &in asEntity)
{
    SetSwingDoorLocked("chamberdoor", false, true);
    PlaySoundAtEntity("", "unlock_door", "chamberdoor", 0, false);
    RemoveItem("chamberkey");
    SetSwingDoorClosed("doorslam", true, true);
    SetSwingDoorClosed("doorslam2", true, true);
}
void FUNCTION2(string &in asItem, string &in asEntity)
{
    SetSwingDoorLocked("cellerdoor", false, true);
    PlaySoundAtEntity("", "unlock_door", "cellerdoor", 0, false);
    RemoveItem("cellarkey");
}

void OnEnter()
{
}

void OnLeave
{
}
Right, so this code should work for you. I'll let you know what you missed
1) Your syntax was incorrect for your functions. For AddUseItemCallback, you need to end your callback function with (string &in asItem, string &in asEntity). The wikipage shows the correct syntax for callback functions, as well as timers and the like. They're bolded underneath the function.
2) If you want two separate doors to unlock, you need to name the unlock functions accordingly. I changed it up to Function1 and Function2. Of course, you could also use a subroutine, but I left it simple.
3) You forgot to put closing quotation marks around cellarkey in Function2, as well as spelling it differently.
4) All .hps files need to have a OnEnter and OnLeave function. You can leave these empty if you wish, as they only perform functions when the player enters or leaves the map





RE: How to make several keys in on map - Khan - 08-17-2011

(08-17-2011, 06:49 PM)Obliviator27 Wrote:
Code:
////////////////////////////
// Run when entering map
void OnEnter()
{
   AddUseItemCallback("", "chamberkey", "chamberdoor", "FUNCTION1", true);
   AddUseItemCallback("", "cellarkey", "cellerdoor", "FUNCTION2", true);
   AddEntityCollideCallback("Player", "script_slam", "func_slam", true, 1);
   AddEntityCollideCallback("Player" , "doorbreak" , "MonsterFunc1" , true , 1);    
   AddEntityCollideCallback("Player", "doorslamtrigger1", "Func01", true, 1);    
}
void FUNCTION1(string &in asItem, string &in asEntity)
{
    SetSwingDoorLocked("chamberdoor", false, true);
    PlaySoundAtEntity("", "unlock_door", "chamberdoor", 0, false);
    RemoveItem("chamberkey");
    SetSwingDoorClosed("doorslam", true, true);
    SetSwingDoorClosed("doorslam2", true, true);
}
void FUNCTION2(string &in asItem, string &in asEntity)
{
    SetSwingDoorLocked("cellerdoor", false, true);
    PlaySoundAtEntity("", "unlock_door", "cellerdoor", 0, false);
    RemoveItem("cellarkey");
}

void OnEnter()
{
}

void OnLeave
{
}
Right, so this code should work for you. I'll let you know what you missed
1) Your syntax was incorrect for your functions. For AddUseItemCallback, you need to end your callback function with (string &in asItem, string &in asEntity). The wikipage shows the correct syntax for callback functions, as well as timers and the like. They're bolded underneath the function.
2) If you want two separate doors to unlock, you need to name the unlock functions accordingly. I changed it up to Function1 and Function2. Of course, you could also use a subroutine, but I left it simple.
3) You forgot to put closing quotation marks around cellarkey in Function2, as well as spelling it differently.
4) All .hps files need to have a OnEnter and OnLeave function. You can leave these empty if you wish, as they only perform functions when the player enters or leaves the map

Thanks, but I got it working like 2 hours ago, lol, yea, I figured wat I did wrong, took me FOREVER tinkering with it, and I had some help from other people, and I cut half the .hps file out for my custom story Im making, didint wanna, ruin the surprises ;P