TUTORIAL 5.1 – Scripting a light

Now that we know how to creates models, levels, materials and particle effects lets try and learn how to make some interesting things happen in a level using some scripts.

For this tutorial you can use any level you want as long as you know it's working and has a functioning script file. If you created the level in tutorial 2 you can use that and be sure to have a good foundation for this tutorial.

So lets start with having a simple empty room with a single light in the center, rename the light to ”light”. Make sure you have a _start_ location in one corner for the player to start at. Then create a new cube(0.5m*0.5m*0.5m) in another corner of the room, rename the node to ”_area_script_lighttrigger”.

Now import the referens of your wooden box and place it as you like as long as it's inside the room. If you used the tutorial naming conventions the referens should be named ”_ref_mystuff_woodbox_woodbox1”, if its not make sure you have the reference named “_ref_FILENAME_woodbox1”.

What we will do in this first tutorial is to make it so that the light turns red when you push the box into the corner with the “lighttrigger” area. And when you drag the box out of the corner the light will go back to white.

Export your level and save it as “mystuff_scriptlevel.dae” in your “mystuff” directory.

Now copy the “tut_scriptbase.hps” file from “tutorials/tutorial5” directory to your “mystuff“ directory, rename it “mystuff_scriptlevel.hps”.

Before we continue and add our scripts lets make sure we have everything we need and that it's named correctly. Check the following.

  1. mystuff_scriptlevel.dae
    • One light named “light”
    • One wood box entity named “woodbox1”
    • One area named “lighttrigger”
  2. mystuff_scriptlevel.hps


Test and load the level, make sure you start at the _start location, that there is a light and that you see the wood box in the room.

Now let's open the script file “mystuff_scriptlevel.hps”. As you can see I have written some explanations in it for you. When commenting in a script you use // and /* */. If that was news to you, you really should read some basic C/C++ syntax tutorials first, you do not need to know much but the first couple of chapters in an introduction to C/C++ book is very useful.

The first thing we are going to do is to create the “callback” for when the box enters the “lighttrigger” area. To do this add the following to “OnStart” making it look like this, minus the comment already in the file:

void OnStart()
{
	//---AREA CALLBACKS----
	AddEntityCollideCallback("Enter","ENTITY","AREA_NAME","CALLBACK_NAME");
}	

AddEntityCollideCallback, this creats a callback for when an entity collides with an area. You specify excatly what in the following strings. ”Enter” this can also be ”During” and ”Leave”, for now it should be ”Enter”. ”ENTITY” this says what entity it is, in our case ”woodbox1” since it's when the wood box enters the area we want something to happen. If we, for example, write ”Player” here the player will be the trigger for the area instead. ”AREA_NAME” this we change to ”lighttrigger”, because that's the name of our area to check for collision with an entity. ”CALLBACK_NAME” This will be the name of the actual callback, the funktion that will contain our code for turning the lights red. Let's name it ”TurnLightsRed”.

What will happen is that when the game loads the level it will create a callback that will activate when ”woodbox1” enters the ”lighttrigger” area. We now need to create the callback that will contain the actual events that will occur. Enter the following in the ”CALLBACK FUNCTIONS” area:

//////////////////////
/*CALLBACK FUNCTIONS*/
 
void CALLBACK_NAME(string asParent, string asChild)
{
	FadeLight3D("LIGHT_NAME", R,G,B,A, Radius,Time);
}

CALLBACK_NAME now this should be named ”TurnLightsRed” and in the FadeLight3D we should change LIGHT_NAME to ”light”.

Following there are 6 settings, first the RGB is used to set the color, A is alpha and used to control the amount of specular, Radius is the size of the light and Time is how long it should take to fade.

Change it to

FadeLight3D("light", 1,0,0,1, 6,3);

This will give a red light, with full specular, a radius of 6 and it will take 3 seconds for the light to fade from white to these settings. Save the script file and lets test it ingame. First you need to edit your ”settings.cfg” file in the redist folder. It should load the Map file ”mystuff_scriptlevel.dae” and use the start location you have entered, if you used the tutorial level it should be “location1”.

Start penumbra, move the box from it's start postion to the corner where the “triggerlight” area is. When you do this the lights should fade to red.

Now exit Penumbra and lets continue on and make it so that the lights fade back ones you pull the box out of the area again.

Copy and paste the AddEntityCollideCallback so that you get this in your “OnStart” area:

void OnStart()
{
	//---AREA CALLBACKS----
	AddEntityCollideCallback("Enter","woodbox1","lighttrigger","TurnLightsRed");
	AddEntityCollideCallback("Enter","woodbox1","lighttrigger","TurnLightsRed");
}	

Change the second AddEntityCollideCallback so that it's “Leave” instead of “Enter” and “TurnLightsBack” instead of “TurnLightsRed”. You now have another callback called TurnLightsBack that will be triggered ones the wood box leaves the script area.

To create the callback make a copy of the other callback so that you get this:

//////////////////////
/*CALLBACK FUNCTIONS*/
 
void TurnLightsRed(string asParent, string asChild)
{
	FadeLight3D("light", 1,0,0,1, 6,3);
}
 
void TurnLightsRed(string asParent, string asChild)
{
	FadeLight3D("light", 1,0,0,1, 6,3);
}

The second one should be called ”TurnLightsBack” and the light should still be called ”light” because it's the same light we want to fade back to white. Using these settings:

FadeLight3D("light", 1,1,1,1,8,1.5);

you will get a total white light with full specular, a radius of 8 and a fade time of 1.5 seconds. You can tweak this a little if you want another radius or slightly different color.

Save the file, launch Penumbra and test the level. Can you make the lights fade back and forth by moving the box in and out of the area?

TUTORIAL 5.2 – Scripting several things

Lets move on and try and make this level into a puzzle. What we are going to do is to make it so that the ”gravity” is reversed for the wood box, making the box float up to the roof. We will also add a door with a key pad and the key pad will only work if the box is in the ”lighttrigger” area in the corner. Since the box is up in the roof it does not reach down into the corner therefor we need to make something that will make the ”gravity” right again.

To turn of the reversed ”gravity” we will use a simple switch that when pulled down will turn the gravity back to normal. This will allow theplayer to position the box in the area.

Finally to open the door you will need a code for the key pad. The player will get the code from an audio file that starts to play as soon as he combines two key parts found in the room.

Yes this is not logical at all, but hey it's a tutorial.

Let's start with the gravity! In the script file, at