Procedures were introduced to Pfhortran in version 0.4, and allow the scripter to connect scripts to specific game events. Procedures also allow the scripter to have more than one script executing at a single time, which allows for some quite complicated functionality.
All procedures are declared using the _procedure directive. Following the directive, on the same line, the procedure type must be given. Several kinds of procedure types exist, and we'll introduce them in this section. First, let's take a look at an example procedure.
01
02
03
04
05
06
07
08
09
|
_procedure init
add_item extravision_powerup
add_item missile_launcher
add_item missile_launcher_magazine
add_item missile_launcher_magazine
add_item missile_launcher_magazine
end |
Line 1 is the procedure declaration. The procedure type is defined as "init", which means that this particular procedure will be executed before the game begins. Init procedures only execute once at the beginning of a level and can be used to set the level up before play begins. In this example we have decided to deck the player out with the missile launcher, some missiles, and fisheye ("extravision"). Notice that the end instruction on line 9 marks the end of the init procedure. All procedures must have an "end" statement to show where they finish, as the end statement stops the procedure from executing. The init procedure is slightly unique because it only executes once. Other procedures, such as "idle", may execute over and over again.
Let's take a look at a more complicated example.
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
_procedure init
add_item flamethrower
select_weapon flamethrower
end
_procedure got_item
set_life 100
set_oxygen 100
end
_procedure idle
repeat: inflict_damage 10
wait_ticks 20
jump repeat
end |
In this example we have taken the "berserker rage flame script" and made it procedural. By breaking it up into procedures, we have made it easier to read and more powerful. A few minor changes to the functionality of this script have also occurred, so let's go through it step by step.
The first six lines of code are the init procedure. As before, this part of the script will execute before the game actually starts. All the init procedure does in this example is give the player the flamethrower and selects it for him.
Lines 9 - 14 contain a "got_item" procedure. This procedure is executed every time any kind of item is picked up. All this particular one does is set the player life and oxygen to 100 apiece. The end instruction stops the execution of this procedure. The effect of this code is that any time any item is picked up, the players health and oxygen will be reset to 100.
Finally lines 17 to 23 contain the idle procedure. This should look fairly familiar... all we do is damage the player, wait 20 ticks, and repeat. The end statement will never be reached in this procedure, but even if it did the idle procedure would start over right away.
The net effect of this script is that the player must continue to collect items to avoid death. As the player moves around, their life will slowly decrease at a rate of 10 points every 20 ticks. Players can quickly restore their life by picking something up, at which point the got_item procedure will reset their life to 100. This script would also prove quite annoying if used in the game, but it illustrates how procedures can be used together to create complex functionality. There are many more procedure types besides idle, init, and got_item; for a complete list see the _procedure information in the Pfhortran Directives section.
|