Beginner's corner

Top  Previous  Next

Data sticks

 

You've gotta love data sticks! They're nothing more than electronic pieces of paper that give you information throughout the game. While some of them might sound pretty pathetic (like "Today we've lost 2 more men. I'm all alone now!") others can prove to be really useful (like "You are reading this data stick!").

 

aum33_shot1

 

The code in this article can be used for two objects, but you can expand it and use it for an unlimited number of data sticks. First of all we define two strings and a text:

 

string data1_str = "The new access code is 6239. Memorize it!";

string data2_str = "The vents will be shut down tonight.";

 

text data_txt

{

    pos_x = 200;

    pos_y = 20;

    font = std_font;

}

 

Each data stick (can be a model, a sprite, a wmb entity) has this action attached to it:

 

action data_stick

{

    my.ambient = -50;

    my.enable_click = on;

    my.event = show_data;

}

 

The first line of code darkens the model a little; you can use positive or negative values here, depending on the entity used as a data stick. The entity is sensitive to clicking, so press the right mouse button to show the cursor if you are using the templates. Every time we click the data stick, its event function (show_data) will run:

 

function show_data()

{

    if (my.skill1 == 1)

    {

         data_txt.string = data1_str;

         data_txt.visible = on;

         sleep (3);

         data_txt.visible = off;

    }

    if (my.skill1 == 2)

    {

         data_txt.string = data2_str;

         data_txt.visible = on;

         sleep (3);

         data_txt.visible = off;

    }

    ent_remove (me);

}

 

Every data stick has its skill1 set to a different value in Wed. If the entity that was clicked has its skill1 set to 1 (if it is the first data stick), we set data1_str as the string for the text named data_txt, we show the text for 3 seconds and then we hide it. The same thing happens if we click the second data stick (its skill1 = 2). You can add more "if" branches if you need to have more data sticks.

 

The last line of code in the function removes the data stick entity from the level (you've read it, so it can disappear). I'd use data sticks to tell the player the access code to a certain door, the number of enemies that guard a specific military objective, and so on.

 

 

 

Access code

 

Imagine yourself in a situation like this: you have killed dozens of enemies and now you're getting really close to the end of the level. You take a turn and suddenly you find yourself in front of the door that leads to the toxic waste area exit! You try to open the door, but it won't work. But wait! There's a dial pad on the wall nearby! You try the master password (1234) but it isn't working...

 

aum33_shot2

 

string password_str = "6239";

 

I took a good look at the line of code above so now I'm pretty sure that the access code is 6239. Let me click the right mouse button (to show the mouse cursor) and then I will click the dial pad.

 

aum33_shot3

 

The pass was good because the door has opened! Press the asterisk ( * ) key to hide the dial pad and let's take a look at the code.

 

I have used a small sprite for the dial pad that appears on the wall; the action attached to the sprite is really simple:

 

action dial_small

{

    my.oriented = on;

    my.passable = on;

    my.enable_click = on;

    my.event = show_dialpad;

}

 

The sprite is oriented, passable and sensitive to mouse clicks; as soon as we click it, its event function runs:

 

function show_dialpad()

{

    number_of_digits = 0;

    str_cpy (guess_str, ""); // reset the string

    dialpad_pan.visible = on;

}

 

The function above sets number_of_digits to zero, resets guess_str (the string that will hold the pass input by the player) and displays the dialpad_pan panel:

 

panel dialpad_pan

{

    bmap = dialpad_pcx;

    pos_x = 0;

    pos_y = 0;

    layer = 10;

 

    button = 143, 8, one_pcx, one_pcx, one_pcx, check_code, null, null;

    button = 270, 8, two_pcx, two_pcx, two_pcx, check_code, null, null;

    button = 398, 8, three_pcx, three_pcx, three_pcx, check_code, null, null;

    button = 143, 128, four_pcx, four_pcx, four_pcx, check_code, null, null;

    button = 272, 128, five_pcx, five_pcx, five_pcx, check_code, null, null;

    button = 397, 129, six_pcx, six_pcx, six_pcx, check_code, null, null;

    button = 144, 248, seven_pcx, seven_pcx, seven_pcx, check_code, null, null;

    button = 273, 249, eight_pcx, eight_pcx, eight_pcx, check_code, null, null;

    button = 397, 250, nine_pcx, nine_pcx, nine_pcx, check_code, null, null;

    button = 146, 368, zero_pcx, zero_pcx, zero_pcx, check_code, null, null;

    button = 272, 370, asterisk_pcx, asterisk_pcx, asterisk_pcx, check_code, null, null;

 

    flags = overlay, refresh;

}

 

The dial pad consists of a simple panel with 11 buttons. Every time we click a button, function check_code runs and gets the number of the button that was clicked:

 

function check_code(button)

{

    number_of_digits += 1;

    snd_play (dial_wav, 30, 0);

    if (button == 1)

    {

         str_cat (guess_str, "1");

    }

 

    ..............................................

 

    if (button == 9)

    {

         str_cat (guess_str, "9");

    }

    if (button == 10)

    {

         str_cat (guess_str, "0");

    }

    if ((button == 11) || (number_of_digits > 4))

    {

         while (mouse_left == 1) {wait (1);}

         sleep (0.5);

         snd_play (error_wav, 70, 0);

         dialpad_pan.visible = off;

    }

}

 

Every time we press one of the buttons on dialpad_pan, we increment number_of_digits (the number of characters in guess_str). A sound is played and a character ("1", "2", ... , "0") is added to guess_str. If we press the 11th button ( * ) or if the number of digits has exceeded the length of our password (6239 has 4 digits) we wait until the left mouse button is released and then we play an error_wav sound and the hide dialpad_pan.

 

entity* locked_door;

 

action new_door

{

    locked_door = me;

}

The door has a simple action attached to it; we set its name to "locked_door" using a previoulsy defined entity pointer. The last function compares password_str and guess_str in a while loop:

 

starter compare_strings()

{

    while (1)

    {

         if (str_cmp (password_str, guess_str) == 1) // pass ok?

         {

              snd_play (success_wav, 20, 0);

              dialpad_pan.visible = off;

              while (locked_door.pan < 180) // unlock the door!

              {

                   locked_door.pan += 2 * time;

                   wait (1);

              }

              locked_door.pan = 180;

              break; // get out of the while loop

         }

         wait (1);

    }

}

 

It's a starter function, so it will run by itself at game start. If password_str (set to "6239") and guess_str match, we play success_wav, we hide the dial pad panel and then we use a while loop to change the locked_door pan from 90 to 180 degrees. You might need to change 180 to another angle that works ok for your door, depending on its orientation. We set the final pan angle for the door to 180 degrees (could have been set to 180.2 degrees, etc) and then we get out of the while loop using "break". This way we make sure that the door will open only once.

 

That was all! You can use the loop that rotates the door for a sliding door; change the x and / or y of your sliding door inside the while loop and you're ready to go!