Multiple Keys for a Locked Door.

Moderator: Event DM

Post Reply
User avatar
Jordicus
Team Member; Retired with Honors
Posts: 8042
Joined: Tue Jan 21, 2003 3:46 pm
Location: Whitehall, PA (GMT -4)
Contact:

Multiple Keys for a Locked Door.

Post by Jordicus » Tue Jun 24, 2003 4:21 pm

With the help of Silk and Myk D'vor, I was able to get this idea to work correctly.

If you mark a door as needing a specific key to unlock, you can normally only supply it 1 key tag to be used. I needed to have multiple tags, so finally got this work-around functional

If you have a Door tag as "Jord_Door_1", you need to place a generic trigger in front of it named "Jord_Door_1_trig". The door is marked as locked and also marked as needing a specific key to open. The trigger's tag must correctly match the Door's tag with the additional ending for the script to work properly.

On the trigger's OnEnter event add this script:

Code: Select all

int HasItem(object oPlayer, string sItemTag) 
{ 
    int bReturnValue = FALSE; 
    object oItem = GetItemPossessedBy(oPlayer, sItemTag); 
    if (GetIsObjectValid(oItem)==TRUE){ 
        bReturnValue = TRUE; 
    } 
    return bReturnValue; 
} 

void main() 
{ 
    object oPC = GetEnteringObject(); 
    string sTrig = GetTag(OBJECT_SELF); 
    int iTrig = GetStringLength(sTrig) - 5; 
    string sDoor = GetStringLeft(sTrig, iTrig); 
    object oDoor = GetObjectByTag(sDoor); 

    string sItem1 = "XYZ1";  // tag of first key
    string sItem2 = "XYZ2";  // tag of second key
    string sItem3 = "XYZ3";  // tag of third key
    string sItem4 = "XYZ4";  // tag of fourth key
    string sItem5 = "XYZ5";  // tag of fifth key

    if (HasItem(oPC, sItem1)|HasItem(oPC, sItem2)|HasItem(oPC, sItem3)|HasItem(oPC, sItem4)|HasItem(oPC, sItem5)) 
    { 
        ActionDoCommand(SetLocked(oDoor, FALSE)); 
        ActionOpenDoor(oDoor); 
        DelayCommand(6.0f, ActionCloseDoor(oDoor)); 
        DelayCommand(6.0f, SetLocked(oDoor, TRUE)); 
        SendMessageToPC(oPC, "You may pass"); 
    }else 
    { 
        SendMessageToPC(oPC, "This area is restricted"); 
    } 
} 
Obviously you can add or remove the key tags if you need less keys or more keys.

FYI, in case you were wondering, we found that you cannot apply this to the actual door, because the OnFailedToOpen() event of a door, does not generate either a valid GetLastUsedBy() or GetLastOpenedBy() value. Either of those would have been a much simpler solution.

If anyone sees something that could be optimized more, please let me know. Otherwise, please feel free to use.
User avatar
Aloro
Team Member; Retired with Honors
Posts: 12805
Joined: Sat Dec 28, 2002 5:11 am
Location: Rainbow's End
Contact:

Post by Aloro » Tue Jun 24, 2003 4:33 pm

Use GetClickingObject() in the door's OnFailToOpen to retrieve the PC attempting to open the door. This is the easiest way I have found to have multiple keys for one door.

- Aloro
User avatar
Jordicus
Team Member; Retired with Honors
Posts: 8042
Joined: Tue Jan 21, 2003 3:46 pm
Location: Whitehall, PA (GMT -4)
Contact:

Post by Jordicus » Tue Jun 24, 2003 4:37 pm

GetClicking... ? darn how did I miss that? :P
User avatar
Aloro
Team Member; Retired with Honors
Posts: 12805
Joined: Sat Dec 28, 2002 5:11 am
Location: Rainbow's End
Contact:

Post by Aloro » Tue Jun 24, 2003 4:38 pm

:)
User avatar
Jordicus
Team Member; Retired with Honors
Posts: 8042
Joined: Tue Jan 21, 2003 3:46 pm
Location: Whitehall, PA (GMT -4)
Contact:

Post by Jordicus » Tue Jun 24, 2003 4:42 pm

that would simplify it to this?

Code: Select all

int HasItem(object oPlayer, string sItemTag) 
{ 
    int bReturnValue = FALSE; 
    object oItem = GetItemPossessedBy(oPlayer, sItemTag); 
    if (GetIsObjectValid(oItem)==TRUE){ 
        bReturnValue = TRUE; 
    } 
    return bReturnValue; 
} 

void main() 
{ 
    object oPC = GetClickingObject(); 
    object oDoor = OBJECT_SELF; 

    string sItem1 = "XYZ1";  // tag of first key 
    string sItem2 = "XYZ2";  // tag of second key 
    string sItem3 = "XYZ3";  // tag of third key 
    string sItem4 = "XYZ4";  // tag of fourth key 
    string sItem5 = "XYZ5";  // tag of fifth key 

    if (HasItem(oPC, sItem1)|HasItem(oPC, sItem2)|HasItem(oPC, sItem3)|HasItem(oPC, sItem4)|HasItem(oPC, sItem5)) 
    { 
        ActionDoCommand(SetLocked(oDoor, FALSE)); 
        ActionOpenDoor(oDoor); 
        DelayCommand(6.0f, ActionCloseDoor(oDoor)); 
        DelayCommand(6.0f, SetLocked(oDoor, TRUE)); 
        SendMessageToPC(oPC, "You may pass"); 
    }else 
    { 
        SendMessageToPC(oPC, "This area is restricted"); 
    } 
} 
darn... now I have to wait until I get home to adjust it... even though the automatic doors is sort-of cool..
User avatar
Aloro
Team Member; Retired with Honors
Posts: 12805
Joined: Sat Dec 28, 2002 5:11 am
Location: Rainbow's End
Contact:

Post by Aloro » Tue Jun 24, 2003 4:50 pm

Question: do you ever want the door to be closed and unlocked? Or do you want the door locked every time it is closed? My bias is to put the "autolock" script in the OnClose handler for every door with a lock. That would also shave a few lines off that code. :)

Everything looks good otherwise.

- Aloro
User avatar
Jordicus
Team Member; Retired with Honors
Posts: 8042
Joined: Tue Jan 21, 2003 3:46 pm
Location: Whitehall, PA (GMT -4)
Contact:

Post by Jordicus » Tue Jun 24, 2003 4:52 pm

for this particular instance, the door should always be locked when closed..
User avatar
Aloro
Team Member; Retired with Honors
Posts: 12805
Joined: Sat Dec 28, 2002 5:11 am
Location: Rainbow's End
Contact:

Post by Aloro » Tue Jun 24, 2003 4:53 pm

In that case I'd put "autoclose" in the OnOpen and "autolock" in the OnClose handlers, respectively. This way the door will always close and lock itself, or it can be manually closed and will automatically lock.

- Aloro
User avatar
Jordicus
Team Member; Retired with Honors
Posts: 8042
Joined: Tue Jan 21, 2003 3:46 pm
Location: Whitehall, PA (GMT -4)
Contact:

Post by Jordicus » Tue Jun 24, 2003 4:55 pm

great... now, I just have to wait 5 hours before I get home and make the changes... :?
Post Reply