87
« on: January 09, 2008, 04:57:53 PM »
This is a basic tutorial that will:
1) get you started with torque
2) teach you various things that are commonplace in code
Things it will not do:
1) teach you how to make insane mods in a matter of minutes like many people think is possible because they have no knowledge of coding.
A suggestion for those of you with your fingers on ctrl-c and your mouse ready to drag and select:
I heartily suggest that you write out the scripts show rather than copy and pasting, if you cant put foward that effort, you'll never be a scripter.
Lets get started
+--Chapter one: scripting in general--+
Scripts are what make blockland function as a game, scripts handle everything from laying down a brick, to respawning that guy you just killed. Scripts have 4 things(mostly):
logic
objects
functions
variables
All scripts will at least use the first, logic. Regardless of what you think, all scripting contains logic to it. A script without this will not function, or at least, not the way you want.
Objects:
objects are as the name suggests, objects. You can use objects in your scripts, wheher your making a bot, or just modifying a player.
objects can have variables applied to them, see the variables section.
Functions:
functions are the actions of a script, if you only had Logic, Objects, and Variables, you would just be saying "player brick" instead of "player lay a brick", for example.
many functions are prebuilt into torque, but you can of course make your own, so if you have to do something many times, you can stuff it in a function and then just call that every time you need to do it.
Variables:
Variables store data, whether its a number, line of text(string), object, or anything else, variables are your basis for storing info about objects, and for saving preferences and the such.
Objects can have variables applied to them, so you could give each player a variable and then call upon it from the player, to check/store something unique to the object.
+--Chapter 2: applying knowledge--+
Now that you have a background of scripting, lets start with something simple. Open blockland to the title screen, and open the console.
You can enter things directly into the console to test, or just see what happens.
Lets enter something now, say
echo("This was a triumph");
into the console, make sure you included the semi-colon, or else you will get an error. Once you are sure you have it right, press enter.
On the line below your text, This was a triumph should have appeared. Echo is a function that sends text or other data to the console, it is useful for debugging, and we will use it for dealing with strings. "This was a triumph" was our string, it is a line of text, a string of characters, if you would. There are a few things you can do to modify strings. This time, put in
echo("I\'m making a note here:\n huge success.");
I'm making a note here:
huge success
should've appeared.
\' lets us place a ' in the string, remember this because if you simply put it in without the \ it will give you an error, because the compiler thinks that the ' means the string is closed, the text is over, so it gets confused when it sees that you are still going.
\n creates a new line. There are many things \ can do in strings, so remember it.
Now, lets establish a variable. Go ahead and put in
$text = "Aperture science";
Nothing will be spit out at us, but we have set up a variable. Now we can call upon $text...
echo($text);
It said Aperture science! Because we defined the variable, it found what we set it to, and told us that. Lets take a second to see what $ means.
$ defines a global variable, a global variable can be used over again without being set to an object.
% defines a local variable, a local variable is lost after the function ends, unless it is set to an object, in which case it will stay with the object untill the object is deleted.
Now, put in
$text1 = "We do what we must";
$text2 = "Because we can.";
We have set $text1 and $text2, so lets use them.
say
echo($text1 @ $text2);
It will say
We do what we mustBecause we can.
@ is a linking symbol, it puts together variables, but it doesnt add a space, new line, or anything.
Lets give it a space,
echo($text1 SPC $text2);
It will say
We do what we must Because we can.
SPC is a linking word, that adds a space between the two variables.
But what if we want a new line?
echo($text1 @ "\n" @ $text2);
it will say
We do what we must
Because we can.
first, we linked $text1 with the newline symbol, then we linked that with $text2
resulting in echo("We do what we must\nBecause we can.");
Lets try a different kind of variable now.
say
$num = 5;
This will set $num to the number 5, this is a numerical variable, now we can preform mathematical tasks on the number.
say
$num = $num + 1;
in this case, we added 1 to the variable. We set the variable to itself + 1, so its now 6. Another way we could do this is
$num += 1;
+= adds to the variable, so we saved some space.
now say
$num *= 2;
This will multiply the number by 2, resulting in 12 or 14 depending on if you tried the +=.
Lets see our number.
echo($num);
This is good and all, but what if we wanted to double a number a few times over?
That is where while() comes in.
While($t<5){$num*=2;$t++;echo($num);}
We have alot of stuff going on, lets go over it in english
While our variable $t is less than(<) 5
{
multiply $num by 2
add one to $t
tell us what $num is now
}
A while loop continues to do whatever is inside its brackets( { } ) untill its condition is met(in this case, $t has to be 5 or greater because it continues untill $t is not less than 5)
$t++; is a shortcut to adding one to a variable, everything else you should have seen before.
Because $t is undefined, it becomes 0, by the way.
In a normal script you could write it out like
While($t<5)
{
$num*=2;
$t++;
echo($num);
}
but because we are in the console, we have to scrunch everything up onto one line.
Lets try something that we will use in our first real script, say
echo(getrandom(1,30));
Make sure you have the right amount of parenthesis, functions within functions can get confusing.
and it will spit out a number between 1 and 30!
getrandom(min,max) is a function that gives you a random number between the min and max numbers you specify.
+--Chapter 3: My First Script--+
Its time to start your first script, so open up notepad/w/e you use to code.
We are going to make a dice rolling script, so first, lets make the logic.
player says /dice
get a random number between 1 and 6
tell the player
That was easy. Now lets script it.
put in
function servercmddice(%client)
function marks the start of a fuction, servercmddice is the name. If a function is a servercmd, it becomes a / command, so if someone says /dice it will be called. the parenthesis are what the command takes in-- for example, if you say /fetch rky, there would have to be a variable in the parenthesis to hold the name you say, rky. %client in this case is whoever says /dice, if we added another one, it would be what they say after /dice.
{
the bracket marks the start of the function
%num = getrandom(1,6);
Here we are setting the local variable num to a random number between 1 and 6.
We dont need to keep num later, so it is a local variable not applied to an object.
A good habit to get into is to add spaces or an indent here, since this code is inside of the function, it helps to organize because now it doesnt look like a single block of code-- you can clearly see what is inside of what, and it makes things much easier to manage. I suggest a tab or 3 spaces, but 1 space works as well, I wouldnt suggest more than a tab though.
messageclient(%client,'',"You rolled a" SPC %num);
messageclient() lets us send a message to someone, and that person only.
%client is the person who said /dice, '' is something you dont really want to mess with, it deals with more complicated things and is not what we want so we leave it blank, and then we have the string to telll the user.
You rolled a 5
is an example of what it would say, remember, SPC connects two strings or variables into one string with a space seperator, so You rolled a is combined with the value of %num
}
and this bracket ends the function, closing it.
goto save as, scroll down the file type list to all files and save it as DiceScript.cs in your blockland/add-ons folder.
Now if you start a server, check DiceScript, and now you can say /dice if you did it right!
Tutorial in progress-- expect more later