Previous: Global variables. Local variables
Entity skills. Flags.
Acknex uses "skills", which are special, built-in, local variables. Any entity, be it a model, sprite, and so on, has 100 skills; the first 20 skills can be accessed using WED's "Object Properties" window, and the rest of them are available for our C-Script instructions.
If you have used WED before you might have noticed the "Object Properties" window. Select any entity in a level, right click it, choose ""Properties", and then click the "behaviour" tab to see its skills:
I have set SKILL1 to 6 and SKILL2 to 3, but this won't have an impact on the entity unless I read these values using some C-Script code. So what are we waiting for? Start SED and open the script24.wdl file:
Run the script and you will see a good old friend: the rotating planet.
I think that you will agree with me when I tell you that a single line of code needs our attention:
my.pan += my.skill1 * time_step;
The sphere changes its pan angle by adding "my.skill1 * time_step" to it every frame. Curious to see the value set for skill1 in WED? Here's a shot:
This means that we can control the rotation speed without touching the script at all, because we can input values for SKILL1 using WED's "Object Properties" window! Our simple line of code feeds the values set in WED to the engine! Take a look at this line of code:
my.pan += 4 * time_step;
And now take another look at the line of code from our script:
my.pan += my.skill1 * time_step;
What is the difference between them? We have replaced "4" with "my.skill1", which is the first skill of the entity that has the action attached to it. That's all you need to do if you want to use skills in your actions. Now go and set different SKILL1 values in WED, build the level again and see the changes for yourself. Be aware that SKILL1 = 0 will add zero degrees to planet's pan angle every frame, so the planet won't rotate at all.
We can use any other skill (my.skill2... my.skill100) to control the rotation speed, but remember that only the first 20 skills are available in WED's "Object Properties" window; this means that if you choose to use skill30, you won't be able to input values for it in WED! As a general rule, you should use the first 20 skills (or fewer) to input values using WED, you should avoid using skills 21...50 because most of them are used by the template code that comes with A6, and you should use skills 51...100 as local variables in C-Script. If you don't plan to use the ready-to-use / click-games-together template code that comes with A6, you can use all the skills the way you want them.
Why would we want to use skills instead of local variables? First of all... they're built in! Each entity includes 100 skills, so we don't need (nor want) to use additional memory for other local variables. Need an even more important reason? We can use these skills to beautify WED's "Object Properties" window:
This is a shot from a complex driving simulator (not a racing game) developed for one of my customers. You can see that the old SKILL1.... SKILL20 names are gone and WED displays meaningful names now! Do we need something like this? I think that we need it because:
1) We are growing old and our memory isn't that good anymore. A commercial-quality project can take up to 6 (even 24) months to create, and it is better to see something like "Initial_speed" in WED instead of "SKILL3"; this way, we won't need to browse through piles of code in order to see what a certain skill does;
2) Our level designer(s) will be able to create, test and populate the levels with all kinds of monsters without needing complicated charts or our presence. This means that we (the programmers) can go home earlier because the level designers won't need us around :)
How do we redefine the skill names in WED? Start WED, load the work24_2.wmp file, build it and run it using the script24_2.wdl file; you will see the same rotating planet, only a bit darker. Select the planet model, choose "Object Properties" and then click the "behaviour" tab:
Our planet can change its rotation speed and its brightness using its own SKILL1 and SKILL2, which are now known as "Rotation_speed" and "Brightness"! Play with these values a bit; "Brightness" should produce noticeable effects when ranging from -100 to 100. Don't forget to build the level again if you want to see the changes.
Ready for fun? Let's discuss the script24_2.wdl file:
If you are using an old version of the level editor WED (older than 6.26) you will have to add an empty line of code at the end of the script (#32, not displayed in the picture above); otherwise, "uses" will not work correctly and WED will display the old SKILL1... SKILL20 names. That was a rare bug but it is fixed now; don't forget to update your A6 engine on a regular basis - it's free!
Let's get back to our script! We'll walk through all the changes step by step:
define rotation_speed, skill1;
define brightness, skill2;
The two lines above give meaningful names to skill1 and skill2. It's really that simple! Let's assume that you and I would create a game together. I like to use weird names for my variables (in fact, I don't like to do that, but let's assume it for a moment) so one of my var definitions looks like this:
var how_many_times_the_player_lives = 3;
You don't like the name of my variable (who would?) so you would want to change it, but this isn't possible because:
a) I might get angry;
b) The rest of the code will give errors, because I have used "how_many_times_the_player_lives" several times in my code, so you'd have to replace it everywhere.
So what do you do then? You add a simple "define" instruction:
define number_of_lives, how_many_times_the_player_lives;
This way the old variable has got a new name: I can use my old var name, you can use your newly created var name, and everybody is happy. And of course that any of us can change the value of the variable.
So that's how "define" works. Going back to our script, I will be able to use "my.rotation_speed" instead of "my.skill1" from now on, and "my.brightness" instead of "my.skill2". Hold on, there's more!
// uses rotation_speed, brightness
This isn't an ordinary comment; it is placed right above the action definition. This line tells WED that the following action will use the previously defined "rotation_speed" and "brightness", and this makes WED change its SKILL1 to "Rotation_speed" (note the automatic capitalization) and SKILL2 to "Brightness". You can add as many defined names as you want to the comment, as long as you separate them using commas.
Time to examine the full action:
// uses rotation_speed, brightness
action mother_earth_beauty
{
my.ambient = my.brightness;
while (1)
{
my.pan += my.rotation_speed * time_step;
wait (1);
}
}
The sphere will set its ambient to the value that was input using "my.brightness" (my.skill2) in WED and it will rotate with the speed set by "my.rotation_speed" (my.skill1) in WED.
One more thing: you can't use names that include spaces, like "define my health skill3;" or "// uses players armor", but I think that you knew that already. That's all there is to know about these comment tags. There are more of them, and if you are a "click-games-together" person you already know how to use them. You'll see their description in the C-Script manual anyway.
Acknex also uses "flags", which are similar to the light switches from your house. Any entity includes 8 flags which can be set to "on" (checked) or "off" (unchecked) using the same "Object Properties" window in WED.
The picture shows Flag1 and Flag3 set to "on" (checked), the rest of the flags being "off" (unchecked). We can use these flags in C-Script using my.flag1... my.flag8. Start SED and load the script24_3 file:
It's a small script, mainly because we are coming close to the end of the workshop and you must be tired. The action simply checks the status of the first flag for the entity that has the action named "flag_example" attached to it. If the flag is checked in WED ("on") the model becomes transparent. Here's what you will get if you check / uncheck the first flag in WED:
Believe it or not, we can give these flags meaningful names using the same method: "define" a flag and then place the "uses" comment above the action that uses it. You can see some flags with meaningful names in my driving simulator project - here's the same shot again:
We will use many flags and skills in our future workshops; even I don't know how I have managed to get this far with these workshops without using them at all!
Now here's your homework: create an action that "uses" the first three skills in WED, named "Pan_speed", "Tilt_speed" and "Roll_speed" to control the rotation speed of the planet by changing its pan, tilt and roll angles inside a "while(1)" loop. The same action must use its first flag, named "Transparency", to control the transparency of the planet model.
I am attaching a shot from WED's "Object Properties" window to show you how this action is supposed to look like; the values for the first three skills are arbitrary.
Solution: pretend that you wrote the code below:
var video_mode = 7; // 800x600 pixels
var video_depth = 32; // 32 bit mode
string home24_wmb = <home24.wmb>;
define pan_speed, skill1;
define tilt_speed, skill2;
define roll_speed, skill3;
define see_through, flag1;
function main()
{
level_load (home24_wmb);
}
// uses pan_speed, tilt_speed, roll_speed, see_through
action wild_planet
{
if (my.see_through == on) // first flag is checked in WED?
{
my.transparent = on; // then make the sphere
transparent
}
while (1)
{
my.pan += my.pan_speed * time_step;
my.tilt += my.tilt_speed * time_step;
my.roll += my.roll_speed * time_step;
wait (1);
}
}
You can now call yourself a game programmer as you've reached the last line of the last workshop in the Script Tutorial. The last workshop? Well, not really. There's a lot more to learn, like collision detection, movement, and shaders. And there are more workshops. You can find them in the Acknex User Magazine (AUM). The workshop series is continued in AUM 51, where we'll go wild with c_trace...