Game Development Community

dev|Pro Game Development Curriculum

Scripted Doors

by Mark Holcomb · 12/01/2004 (4:00 pm) · 22 comments

Download Code File

Installing doors allows the user to have scripted doors that will lift up or down
The door has two valid directions that can be assigned as dynamic variables
inside the map editor. The variable should be named 'dir' and the values it
can take are 'up', 'down', 'left', 'right' 'forward' or 'back'. If you do not assign a 'dir' variable the door will default to open upwards.

The door will slide appropriately along it's X,Y or Z axis for each of these commands relative to it's orientation in the world.

Each door has a default height to raise or lower, but you can change the height
each door raises by changing the dynamic variable 'liftheight' in the map editor
- or permanently change the default in the code in the datablock section for the
door.

I have defined three different keys that can be assigned as well.
To use the keys, you need to create a dynamic variable for the door called 'key',
then assign a key name to the value. The default keys are RedKey, BlueKey, and
Yellowkey.

The keys are items that can be placed in the game. They are picked up and added to
the players inventory. You can actually assign any inventory item as a 'key' the
code checks to see if the player has that item on them to see if they can open
the door. - So you're not limited to just the keys - you could restrict people from
entering the door if they weren't carrying a particular weapon that you know they
need to find before proceeding.

Some of you might try to use the doors as a lift. Don't bother unless you only want to
go down on the lift. The collision detection in Torque does not check for when a static
object moves into a player - rather it checks the players and projectiles when they
move. This means that if you stand on top of a door/lift and raise it, you will get stuck
inside the object because the static object moved up past you - and since it moved, there would be no collision detection. However if you use the door/lift to go down, it will
work because as the object drops, the player lands on top of it, which causes the
collision detection to work. I tried using an applyimpulse from the door that would bounce the player upward, causing the collision detection to kick in and keep the player on top of the lift, but it was quirky and did not always work.

I mentioned getting stuck inside the object. It is possible for a door to close on top of you. For the above mentioned reasons. To prevent a player from getting stuck in the door object, I have placed a small routine in the doors oncollision that applies a small impulse to the player object that pushes them back a little bit. That motion of course triggers the on collision again - which will push you out of the door object. (I chose backwards rather than forward to keep people from being able to force their way through doors.)

To make the doors interactive I followed the steps from the Torque Documentation to create an 'interact' key. If you have already implemented this into your code, then skip down to section #2.


Section #1 - Creating an interact key.

The interact key will assign a key that will attempt to 'interact' with any staticobject
that is within a set distance (4units) in front of you. To do this you have to assign a key
to the function that calls the interaction, you need to copy the file interact.cs into your
script directory, and then you need to make sure you load it in game.cs.

I'm going to give you the short version of this, for full details read the online docs from
garagegames at www.garagegames.com/docs/torque/general/ch06s04.php.

1. Back up \client\scripts\default.bind.cs and client\config.cs

2. In \client\scripts\default.bind.cs look for the section labelled: Misc. Player stuff

3. At the end of that section add this line.

moveMap.bindCmd(keyboard, "w", "commandToServer('Interact');", "");

(You can change the "w" to a different key if you want, but be sure that it is not already
assigned to a different task. - The torque tutorial uses "t" - I chose "w")

4. Save your file and open up client\config.cs

5. Go down to the bottom of it and add

moveMap.bindCmd(keyboard, "w", "commandToServer('Interact');", "");

(Remember to change your key to match the one in the steps above if necessary.)

6. Save the file.

7. Copy the file 'interact.cs' from the zip file into your \server\scripts\ directory.
(The code for the file is commented, and comes straight from the above mentioned tutorial.)

8. Open game.cs and add the following line in the onServerCreated function

exec("./interact.cs");

9. Save game.cs


That almost concludes Section #1. I recommend reading the tutorial - it explains in detail what this code does and it helps expand the horizon of what you can do with StaticObjects. You can use them to regen health ala Halflife, spit out cokes ala MaxPayne. There are limitless things, but to use it all you have to do is create a datablock for your item, and add an ::onInteract function to it to do whatever you want the item to do.

Pretty sweet.

(Plus you never have to do this part again, the key and functionality is always there.)

Section #2 - Installing the doors.

1. In your /data/shapes directory create a new directory called door.

2. Copy the following files into the new door directory

bluekey.dts
bluekey.ms3d
bluekeyskin.png
door.dts
door.ms3d
door.mtl
door.obj
doormap.bmp
doorskin.jpg
key.ms3d
key.mtl
key.obj
key.qc
keymap.bmp
redkey.dts
redkey.ms3d
redkeyskin.png
yellowkey.dts
yellowkeyskin.png

The list is kind of long, but I've included the Milkshape files, as well as the .dts files.

3. Copy the following two files into your data\sound directory

door_locked.ogg
door1.ogg

4. Copy the door.cs file into \server\scripts

5. Open \server\script\game.cs and add the line

exec("./door.cs");

to the onServerCreatedFunction

6. Open up player.cs and find the function - datablock PlayerData(PlayerBody)

7. Scroll down to the bottom of it and add the lines:
maxinv[RedKey] =1;
  maxinv[BlueKey] =1;
  maxinv[YellowKey] =1;

Section #3 - Creating and using the doors.

1. To add a door, load up your map of choice.

2. Go into the map editor (F11) and go into the World Editor Creator (F4)

3. Look under Shapes and you should see two new entries 'Keys' and 'Door'

4. Expand Door and you'll see the door shape that can be insered into the map

5. Insert the door and position it where you would like.

6. Press F11 - walk up to your door, press your use key and the door should open.

Section #4 - Customizing your door.

I. - Locking the door.
To make the door locked, go back into the map editor, select your door, then hit F3.
Expand the dynamic variable section. Click the add button and create a new variable
called 'key'. Set the value to key to be 'Redkey'. Click the APPLY button for the
change to take effect.

Add a redkey to the map in the same manner as adding the door.

Go back into the game and try opening the door. Now go pick up the key and try
again.

II - Up and down.
To have your door slide down rather than go up, go into the map editor, select
the door, and press (F3). Expand the dynamic variable section and find the
variable named 'dir'. Change the value of 'dir' to 'down' (w/o the quotes).
Click APPLY, then exit the game editor (F11).

III - Open the door higher.
To have your door open further or less, go into the map editor, select
the door, and press (F3). Expand the dynamic variable section and find the
variable named 'liftheight'. Change the value of 'liftheight' to a different
number, such as 15 or such.

Click APPLY, then exit the game editor (F11).


That pretty much sums it all up. I included all the skins, and the simple shapes that
I used for the doors and keys. I know the art could be better, but I'm a programmer
not an artist. Hopefully this will help some of you who are more artist than programmer
get some doors in your games.

As alway, many thanks to the garagegames community whose bits and pieces of code and wisdom helped me put this together for others. Special thanks to Robert Brower whose resource:

www.garagegames.com/index.php?sec=mg&mod=resource&page=view&qid=4004

gave me the info to get moving in the right direction with this.

*** 12/21/04 - Updated the code to handle left, right, forward, and back directions.

Note: If a door is opening towards the player, the door will pass through the player. This is due to the game engines collision detection scheme as discussed above.
Page«First 1 2 Next»
#21
08/12/2007 (11:04 pm)
hi,I found a bug that if player back to the door ,he could cross the door without open it ,how to deal with it? thx.
PS: it is a nice resouce.
#22
05/24/2008 (8:40 pm)
So anyone get the door swing working yet?
Page«First 1 2 Next»