Tuesday, August 26, 2008

Intro to Torque Moduals and Functions

Ok, so now you know about variables. But where do you put them? I suppose you could enter them in through the console window but that wouldn't be very efficient and you couldn't exactly create a game that way.

Well, to get your commands and information into Torque you need to create Modules. If you look through the folders under the Example directory you should see a bunch of files with .cs at the end. These are your modules and they are what is going to make your game YOUR game.

main.cs is the first torque script file that the engine will try to load. This file will be in the same folder as the executable. Go ahead and open it up and take a look around.

Most modules will use a header that describes what the file is:

//main.cs
//Main module for the Torque Game Engine

After that, the module will be broken up into functions. Functions are where the real work occurs inside of the program and look like this:

function Take10Damage()
{
//commands, variables, etc go here
}

You will notice that there is no ";" at the end of these lines. It is not required and will give you a syntax error when it is run in Torque. After the function is declared, a "{" marks the beginning of the function and a "}" marks the end of the function.

So why use a function at all? Why not put everything into one big file? Because functions can be made very flexible and multi-purpose. Lets say that our player is hit with a rock and then falls off a cliff. If both are supposed to cause 10 damage to the player then a single function can be created and called by both to decrease the players health.

So you may be asking yourself how do you get values from one function to another. Well, one way is to use Global Variables (ones that start with $). Another way is to pass an argument. The () at the end of the function declaration is not just for looks. You can use it to pass any amount of useful data into the function. Lets take a look at the following module:

//takedamage.cs
//reduces health after being hit or falling

function healthtracker()//main function doesn't need an arguement
{
%playerHP = 100; //initializes player health at 100

echo ("Starting Player HP ="@%playerHP); //prints the players current health to the console

%playerHP = take10dmg (%playerHP); //calls the function "take10dmg" and passes the players current health as an argument
echo("Player HP after fall ="@%playerHP);//current health to console

%playerHP = take10dmg (%playerHP); //calls the function "take10dmg" and passes the players current health as an argument
echo("Player HP after hit ="@%playerHP);//current health to console
}

function take10dmg (%playerHP) //create the new function. Can use the same variable since it is local
{
%playerHP = %playerHP-10; //reduce HP by the stated amount
return %playerHP; //passes the arguement back to the calling function
}

I am sure you can imagine the flexibility of a tool like functions and passing arguments between them. I know this seems like a lot of messing around after all, where are all the cool graphics and effects? Don't worry, those will come in time. For now we have to lay the ground work for where we are going to end up. And that is hopefully with a fun, playable game.

No comments: