Skip to content

3D_for_GML:_Upward_sliding_doors

hugh greene edited this page Jun 20, 2022 · 1 revision

1.1 Finding the exit

If you run the gm6 file that came with this tutorial, try to find the exit. The exit is a ladder to your freedom. No, the ladder doesn’t work. Why? Well, because this tutorial is about the upward sliding doors that you will encounter in the game.

1.2 Setting up a door object

This tutorial deals with only one object. Though there are other objects like a ladder, torch, floor, and so on, we will only look at the object named obj_door. In its Create event, the script ini_door is executed:

z=0;
tex=background_get_texture(bac_door);
open=0;                               //door is not open
shut=0;                               //door shut sound

What do these lines do? Let’s take a look. The first line sets the z value (height above the ground) to zero. This means the door will rest firmly on the ground which is at a height of zero. Second line: the texture that will be used is the bac_door_background. Third line: the door will not be open. Fourth line:door will be shut. The shut value will be used in playing a sound. We will see how that works later on in this tutorial.

1.3 Making the door move

In the Step event of our door object, the script mot_door (motion of door) will be executed:

if open=0 then                        //door is closed
{
if z>0 then
{
z-=8;
if z=0 then
{
if shut=0 then
{sound_play(snd_doorShut);sound_stop(snd_doorShift);shut=1;}
}
}
}
else                                  //door is open
{
if z>127 then {open=0;} else {z+=2;}
}

All the door’s movements will be controlled by this script. Let’s take a look at it and try to understand what it does. Basically, it is nothing more than a tree structure with lots of ifs: “if this is true then that happens”. Here’s what is says in a translation: “If the door is closed, check for the following: if the floor is above the ground then slide it down and check if it reached the ground. If it’s on the ground, then check if it is shut. If it is not shut, play the shut sound, stop the shift sound and make the door shut. On the other hand, if the door is open, check if it’s above a certain point and set it to close again. If it’s not above that point, keep sliding it up.” As you can see, this whole script is no more than a logical tree structure that controls the motion of the door. By the way, the sound snd_doorShiftis played when the door is sliding up or down, in other words: shifting.

1.4 Drawing the door

The door is drawn using dra_door:

d3d_draw_block(x-32,y-3,z, x+32,y+3,z+128, tex,2,4);

No big surprises there: a flat block shape with a texture to make it look like a door, drawn at a certain location and height.

1.5 Bumping into doors

When the player bumps into a door (in other words, when the character object collides with a door object), the following code is executed by the script scr_bounce_door (character bounces against the door):

if other.open=0 then                  //door is closed
{
other.open=1;
other.shut=0;
if sound_isplaying(snd_doorShift)=false then
{sound_loop(snd_doorShift);}
if sound_isplaying(snd_doorOpen)=false then
{sound_play(snd_doorOpen);}
if other.z<64 then {move_bounce_all(0);speed*=.75;}
}

The other object is the door that was bounced against. So, let’s see if we can translate like we did before: “If the door is closed then open the door and un-shut it. If the door shifting sound isn’t playing, then play it as a loop. If the door opening sound is not playing, play it. If the door is below a certain point, the player bumps into the door.” Again, this is a logical tree structure. This one makes the player bump into the door if it’s not open, among other things. You can even make hidden doors the player will not be able to see until he bumps into them. They look just like walls. Check out the scripts executed by the hidden door object: obj_door_hidden. A final note: notice how the wall object is set to Solid in the Object Properties, while the door objects are not.

Clone this wiki locally