Torquescript Learners Guide
Hello there, chances are you've come to this topic to learn some Torquescript so you can make some basic add-ons for your favorite game, Blockland. Well I'm here to do my absolute best to help you achieve that goal!
Let's begin, shall we?
SyntaxSyntax is sort of like grammar for programming languages, it is the correct way to form and position the lines of code you will be writing, such as always putting a period on the end of a sentence. Torquescript has a very easy to learn, in my opinion, syntax.
[This section will be re-done]
VariablesContrary to most programming languages, Torquescript does not require the usage of actually declaring variables, the first time the variable is used it will automatically be declared, what are variables exactly, might you ask. A variable is a tool used to store information, the types are a string
and to my best belief a byte. A string is a variable that uses letters or a letter and number combination. A
byte is simply a number. To set a variable you use the variable indicator, the variable name, an equals sign, and the value it should be set to, followed by a semicolon. Here are some examples:
$Test = 1;
%Test = "Hello World";
Notice how one indicator is a percent sign and the other is a dollar sign? If you did, good job. The dollar sign represents a global variable, which will stay the same unless changed or the script is no longer being executed. A percent sing indicates a local variable, which will only persist for the duration of the function it is being used in. Also notice the "$Test" variable has no parenthesis, but the "%Test" does, the parenthesis are needed to declare anything that includes non-numerical characters.
FunctionsAlright, so you've made it this far? Good job, now let's learn the heart of any programming, functions. Getting something done. A function is an executable piece of code that can be called anytime to return a value or cause an event to happen. Torque/Blockland already has pre-programmed functions that can be accessed, and modified. But first why don't we learn how to create a function? Here's an example:
function Test_This(%input)
{
return %input;
}
Okay, so let's get to understanding this code now, the "function" part is used to tell the engine that we are creating a new function, we then add a space and decide our name. The name cannot start with anything besides a letter, and the entire name cannot contain spaces, you must use an underscore if you want to create a space. We then use an opening parenthesis to start our arguments, we would put the name of any argument that we need in the function to run it, and seperate two using a comma. After this we start a new line and use an opening bracket, without this you
will get a syntax error, this bracket lets Torque know that you are beginning the function. Inside this bracket we then use another new line and tab, the tab is used anytime after you use a bracket, we will learn more about this later. Then put whatever the code is inside the white space you now have, using a new line anytime you start a new method. Also notice what I've gone ahead and put in the function, "return %input;". Let's break this down. "Return" is used to end the function and return the value specified. If we made %input equal to 1, and called the function, it would return the value 1. Then the semicolon at the end of the line is used at the end of any method. Then we use our closing bracket to end the function.
Now let's call our new function!
echo(Test_This(1));
Using this, you should get the number 1 returned and echoed to you, now, what is echo? Echo is used to write the value inside of the parenthesis inside your console window, accessable by the tilde key. You've made your first working function! Also remember you can change the "1" inside the function calling to anything you want and it will echo it, but if you use anything besides numbers be sure to use parenthesis!
If-Then-Else StatementsNow let's get a little more in-depth. If-Then-Else statements are used to determine if one thing is equal to another, and are extremely useful. I'll be referring to them as if-statements for the sake of my fingers for the next part. If-statements can be used in many ways, it's all based on what you are doing. Let's see some examples:
function Test_This(%input, %check)
{
if(%input $= %check)
{
return "true";
}
else if(%input > %check)
{
return "greater than";
}
else if(%input < %check)
{
return "less than";
}
else
{
return "false";
}
}
Alright, let's get to it. First you will see I started my function as usual, and created two variables to use. Then I use if and an opening parenthesis to indicate me putting the parameters in. I then put my variable name, condition, and the second variable name. If the condition is met, it will execute the code inside the brackets of that if-statement, if not, it will go to the next statement and do the same. The next statement is and "else if", meaning if the past if wasn't true, then see if this is. The last was just an "else" statement, so if no other conditions were met, then the "else" will be called. Now let's see something else:
function Test_This(%input, %check)
{
if(%input > %check || %check > %input)
return 1;
else if(%input $= %check && %check $= %input)
return 2;
}
Now this one is a bit more tight and easy to read, but it doesn't do the same thing as the last. Notice how after the if-statements it doesn't have brackets, if the method you are calling after the if-statement is only one line, you don't need to use brackets. You may also notice the || and the && signs, let's see what these mean. || means "or", if this is true, OR, this is true, then the condition is met. The && means "and", if this is true, AND, this is true, the condition is met.
This is only the very beginning of this resource, it will be expanded upon in the next two days greatly, if you have anything to contribute or want to be put in, please feel free to let me know, and if anyone is trying to learn, but needs one-on-one help, feel free to add me on Steam using the name in my profile