How to make re-lockable doors & containers

From the Oblivion ConstructionSet Wiki
Jump to navigation Jump to search

Lockable Container/Door Tutorial[edit | edit source]

Ok so lets make a container or a door that can be unlocked and locked back with a key.

First we need to make a key, give it a unique ID (lets say "SecretChestKey"), give it a name, select a NIF file and an icon. We won't need any script for the key.

Then lets make a container. Again give it a ID (in my case "SecretChest"), a name, a NIF and select open and close sounds. Then put in whatever you want to have in this chest. If you want to do this for a door do the same (ID, name, NIF and sounds). Leave the script field empty for now.

Then put the container/door into the world, and doubleclick on it. Go under the lock tab, and select "Locked", then under "Level" choose "Needs a key", and under "Key" select the key you made before (in my case "SecretChestKey").

Ok now we have to write a script:


;First the name of the script
ScriptName SecretChestScript

;Then a reference to the container itself
ref mySelf

;And a short variable
short button


;And, when we activate the chest,
begin OnActivate

   ;Reference mySelf is set to the container
   set mySelf to GetSelf

   ;and the program checks if it is the player that activated it
   if IsActionRef Player == 1

        ;Here the fun begins,.. If the container is locked and the player has 
        ; the key... (GetItemCount returns the number of items(in our case keys) the
        ; player has, so if the number is greater than 0 that means the player has 
        : the key to the container)
	if mySelf.GetLocked ==1 && Player.GetItemCount SecretChestKey > 0

                ;then unlock the container
		mySelf.UnLock

                ;and give a message that the chest has been unlocked
		message " You unlock the chest with a key."

                ;and then open the chest, and let the player see what's inside
		mySelf.Activate Player

        ;and if this is not the case, check if the chest is not locked 
        ; and the player does have the key
	elseif mySelf.GetLocked == 0 && Player.GetItemCount SecretChestKey > 0

                ;then ask the player if it would like to open the container
                ; or lock it (Function GetButtonPressed is later used to return 
                ; the number of the button pressed by the player)
		MessageBox "Would you like to open or lock the chest?", "Open", "Lock", "Cancel"
        
        ;But if the chest is locked and the player doesn't have a key,
	elseif mySelf.GetLocked == 1 && Player.GetItemCount SecretChestKey == 0
                
                ;then tell him he needs a key to unlock
		Message " You need a key to unlock this chest."
	
        ;And if the chest is unlocked and the player doesn't have the key,
        else

                ;then open the chest
   		mySelf.Activate Player
	endif
   endif
end


;and then in gamemode
begin GameMode

        ;get the number of the choice from the previous messagebox and store it in a variable "button"
	set button to GetButtonPressed

        ;if the button has been pressed, then check what was it
	if button > -1

                ;if it was "Open"
		if button == 0

                        ;then open the chest
			mySelf.Activate Player

                ;if it was "Lock"
		elseif button == 1

                        ;then lock the chest with 100(that means, that it needs a key)
			mySelf.Lock 100

                        ;and tell the player it has been locked
			Message " You lock the chest with a key."

                ;if button "Cancel" has been pressed, then
		elseif button == 2

		        ;do nothing

		endif
	endif
end


If you are doing this for the door, you have to do the following:

1. Change this: (GetOpenState is a function that returns the current state of a door, 1 is open, 2 means it is currently opening, 3 is closed and 4 means it is closing. The function returns 0 when the object is not a door)

if IsActionRef Player == 1

With this:

if IsActionRef Player == 1 && mySelf.GetOpenState == 3


2. Change this:

	     Message " You need a key to unlock this chest."
	else
	     mySelf.Activate Player
      endif
   endif
end

With this:

         Message " You need a key to unlock this chest."
      else
         mySelf.Activate Player
      endif
   else
      mySelf.Activate Player
   endif
end

3. And finally change all "chest" with "door" in the messages


If you want a message box to appear when you open a chest/door, instead of just a message,.. Then just change the "message" with "messagebox".

And now that you have the script, open the container you made earlier (from the object window, not the one in the world), and select this script for it.

And thats it.

There is also another simpler way of doing this. It would work differently, so that when you first activate the chest it would unlock itself and open. Then when you activate it the next time it would lock itself. If this is the case just do the following:

1. Delete this line:

short button

2. Change this line:

MessageBox "Would you like to open or lock the chest?", "Open", "Lock", "Cancel"

With these:

mySelf.Lock 100
Message " You lock the chest with a key."

3. Delete these 2 lines:

else
     mySelf.Activate Player

4. And finally delete everything after this line(including this one):

Begin GameMode
.
.
.

If you just want to get the scripts without comments I have all four versions prepared here:

Complex version: Container

Complex version: Door

Simple version: Container

Simple version: Door

So that's it, it worked for me, it should work for you. If you have any questions ask them here. (or email me: matejlenarcic@hotmail.com)

If you spot an error please post here.

--Lenko 05:56, 9 April 2006 (EDT)