Game Development Community

dev|Pro Game Development Curriculum

Bridge/door code with a switch

by D Fasel · 09/15/2010 (1:23 pm) · 7 comments

//Bridge/door code

// this switch open or close the bridge
// you have to make a switch-model with a open and close animation
// this model must have a Collision box, where the player does a collision for operate the switch

// the switch datablock
datablock StaticShapeData(Bridgeswitch)
{
category = "Switch";
shapeFile = "art/shapes/YOURSWITCH.dts"; //use your animated switch model
position = "0 0 0";
Scale="1 1 1";
emap = true;
receiveSunLight = "1";
};

// the switch collision function
function Bridgeswitch::onCollision(%this,%switch,%col)
{
if (%col.isbot == true) return;
if (!%switch.open)
{
%this.schedule(100, open, %switch,%col);
}
if (%switch.open)
{
%this.schedule(100, close, %switch,%col);
}
}

// the switch and bridge open function
function Bridgeswitch::open(%this,%switch,%col)
{
$Bridgeswitch = 1;
%switch.open=true;
%switch.setthreaddir(0,"true");
%switch.playthread(0,"open");
$BRIDGE.setthreaddir(0,"false");
$BRIDGE.playthread(0,"open");
return;
}

// the switch and bridge close function
function Bridgeswitch::close(%this,%switch,%col)
{
$Bridgeswitch = 0;
%switch.open=false;
%switch.setthreaddir(0,"false");
%switch.playthread(0,"close");
$BRIDGE.setthreaddir(0,"false");
$BRIDGE.playthread(0,"close");
return;
}

function Bridgeswitch::onSearch(%this,%obj,%source)
{
%this.oncollision(%obj,%source);
}


// the bridge
// the bridge need a bridge model with only one collision box around the bridge
// the bridge also needs a open and close animation, don't forget the collision box to animate too, or your player falling down
// only make the animation part of the model and use a 2. model for the static details of the bridge

//this is the datablock of the animated bridge, not the static parts
datablock StaticShapeData(Bridge){
category = "Bridge";
shapeFile = "art/shapes/YOURBRIDGE.dts"; //use your animated bridge model
position = "0 0 0";
Scale="1 1 1";
emap = true;
receiveSunLight = "1";
};

//this function is used for the first time, so your bridge is close at start
function Bridge::initialize(%this,%door)
{
$BRIDGE = %door;
%door.setthreaddir(0,"false");
%door.playthread(0,"close");
}

// this function is need for the start
function Bridge::onAdd(%this,%door)
{
//10 Second delay,then close the bridge just used after start the game
%this.schedule(10000, initialize, %door,%col);
}

// end of all bridge code

This here is only code, you have to make 3 models. One model is a animated switch, and
one model is the animated bridge. the 3. model are static elements of the bridge. You
have to make 2 models for the bridge. You can only animate one collisionbox of the
bridge, or it don't works right.
It should works also in multiplay mode. every player can use the switch for close or open the bridge.

I hope this code helps newcommers to make bridges or also doors, using a switch for open
or close the bridge/door.

Here a movie show the bridge at work, and some of my project:)

the bridge is to watch at 1:15

About the author

Working in the team of Winterleaf Entertainment as an Lead Level Designer and Environmental Artist. Selling content and art packs ready for T3D at my shop www.game3d.ch/shop


#1
09/15/2010 (2:59 pm)
Love the video! - NIce resource
#2
09/19/2010 (9:56 am)
First out of house video I've seen of somebody using the Daz3D game ready characters.
Great stuff!
#3
09/19/2010 (10:43 am)
Ty Surge and BrokeAss Games. It is the M4 Epsilon Character, but i did some little changes at the model.
#4
10/03/2010 (11:38 pm)
Looking good.
#5
04/09/2013 (10:28 am)
@D Fasel
1st of all thx the script works great
and helped me alot

i would have a question about it
would it be possible to expand this to work
with a key?

i tried a couple of resources with key to unlock doors
but noone of those is working/ at least not in my case

would u mind takin a look at expanding your bridgeswitch to work with a key?

that would be more then appreciated
thx for considering
#6
04/09/2013 (12:21 pm)
@ Jolinar:

Use some thing like this:

// the switch and bridge open function
function Bridgeswitch::open(%this,%switch,%col)
{

if($yourKEY == false) return;

$Bridgeswitch = 1;
%switch.open=true;
%switch.setthreaddir(0,"true");
%switch.playthread(0,"open");
$BRIDGE.setthreaddir(0,"false");
$BRIDGE.playthread(0,"open");
return;
}

or this or both

// the switch collision function
function Bridgeswitch::onCollision(%this,%switch,%col)
{
if (%col.isbot == true) return;

if($yourKEY == false) return;

if (!%switch.open)
{
%this.schedule(100, open, %switch,%col);
}
if (%switch.open)
{
%this.schedule(100, close, %switch,%col);
}
}

-------------------------------------------------------

if($yourKEY == false) return; this return the function, if $yourKEY is set to false. Just set $yourKEY to true, for use the bridge or the switch.

Note:
This resource here works only for one bridge ! If you use more as one, then make for each bridge a own global variable ($BRIDGE) and data blocks and functions.

You can use the inventory system for the keys.

hope this helps to get keys working with the bridge.


#7
04/09/2013 (1:29 pm)
Hey really appreciate that
and thx for the fast reply