Elite Square
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Complete Torque-Script guide. [WIP]

3 posters

Go down

Complete Torque-Script guide. [WIP] Empty Complete Torque-Script guide. [WIP]

Post  Honor Wed Jan 02, 2013 5:47 am

The complete Torque-Script guide will teach you the basics of everything, and hopefully more. Please be patient with us, as this is an work-in-progress.

Please note this guide is based around Blockland, so some functions won't work for other computer applications.



TorqueScript is a scripting language used in the Torque Game Engine, which is what blockland uses.
Torque Script is used in many games other games too.

With this guide, you will learn how the basics of this language works, and how to manipulate it.



Lesson A-1 - What is a function?
In this lesson you will learn what a function is, and how to create a function.

First of all, what is a function?
A function is a collection of code that is executed when it is called. More on that in the next lesson.

Here is an example function:
Code:
function blah()
{
    echo("Hello, World!");
    //Stuff goes here
}

Let's dissect this, function means that we're defining a function. Then after that goes the name of that function, in this case, blah. Functions can be overwritten, so you always need to be careful when naming a function. The ()'s is a place for the "Arguments" to go into, but let's not worry about them right now. Now, whatever is between the { and }'s are what is carried out when the function is called.
The third line of that code is an example of a function calling another function, again more on that in the next lesson.
If you see // in the code, that's called a comment. Anything on that line after the two slashes will not be executed, so you can write whatever you want there.




Lesson A-2 - Calling a function
In this lesson you will learn how to call a function.

To call a function means to execute the commands that are listed inside of it.
Calling a function is very easy to do.

Here's our example code:
Code:
function blah()
{
    //Stuff here
}

Now, in order to call that function, you would use:
Code:
blah();

blah is the functions name. The () are where the arguments would go if there were any. More on that in lesson A-5.
Unlike most programming languages, Torque Script is not strict about function arguments, which means that you could, for example, call a function that normally has 10 arguments with no arguments, and it wouldn't give out an error.
The ; (semicolon) at the end of the line means that we're ending a statement, you'll learn what else has to end with one of those throughout the guide.




Lesson A-3 - Variables and Strings
In this lesson you will learn what variables are, and how to correctly use them.

A variable is quite simply a piece of data that is stored, and can be retrieved or changed at any time.
This means that the value will vary. (hence the name variable!)

Here are some examples of how you can define variables:
Code:
%one = 1;
%Hello = "World!";
$WhatAWonderful = %Hello;

You can tell if something is being done with a variable if % or $ is in front of it.
The only difference between the two is that % is a local variable that is only accessible in the function it's being defined in, and $ is a global variable which can be accessed at any time.

The variable %Hello that you saw is a string.
A string is anything that is defined using a defined using quotes (example: "Hello, World!"), or a string function (like getsubstr), and can hold as much information as you want it to.

A Boolean value is a variables that holds true or false value.
Code:

%something = true;
%else = false;

A true value is also equal to 1, and a false is 0. Remember this for lesson A-5.




Lesson A-4 - Function Arguments
In this lesson, you will learn how to use the arguments for a function.

Here is our example function.
Code:
function blah(%blah)
{
    if(%blah >= 3) //>= Means greater than or equal to, more on these later.
    {
          echo("More than 3 :PP"); //echo is a function, and we are inserting a string as an argument.
    }
}

Now, just one look at our example function you might notice a variable in the parenthesis. That is a function's argument, just like any other variable, you can name it anything.

When calling the function blah(); we wouldn't call it that way. Instead, we would call it like blah(2); . Adding the 2 causes the %blah to equal 2, it's like transporting or defining a variables value, to a different function. You can use this in so many ways, which in return can get you what you want for script. Another thing, if we did blah(3); the value of the %blah would make the function echo a message.




Lesson A-5 - Default Functions
In this lesson, you will learn how if operators work.

Now, I know I used an if before in a previous lesson. If you didn't already know what it is and how it works, I will explain it.

if's can be used to check if a variable is equal to [something]. You can use it in many ways, some ways more complicated more than others. I will begin by telling about the operators.
(WIP)

== - Equal to.
!= - Not equal to.
> - More than.
< - Less than.
>= - More or equal to.
<= - Less or equal to.
$= - String is equal to. *Used for strings*
!$= - String is not equal to. *Used for strings*
You can read more about the operators at the bottom of the page, inside the Torque Script Apendex.

As I said, if's can be written in many different wasys, but here is a basic if:
Code:

if(%age == 7)
{
    %rank = "Child"; //We defined it as a string.
}

//Or we can do something like This

if(%a $= "meow")
{
    echo("The cat has meowed!"); //Echoing that the cat has meowed!
}





We are skipping a little for those who are already at a further scripting level. We will continue to work on both sections.




Lesson C-1 - Script Objects
In this lesson I will bring up an important resource, script objects. I will teach you how to use and or utilize it.

A script object is a source that stores information and functions, you probably have seen a few without realizing it. A client and player for example are a form of a script object, %client stores information and functions. It stores the players ID as %client.player and can be accessed when you use %client.player.BLAH .. Not are you forced to use already existing forms of script object, but you can make your own.

Now, there are multiple ways of defining a script object.

Option 1.
Code:

new ScriptObject(Cat)
{
   MeowAmount = 14;
};
We can retrieve the value of MeowAmount by doing, echo(Cat.MeowAmount); . What is weird about this, is that there is no % or $ in-front of it.

Option 2. - My preferred way..
Code:

$Cat = new ScriptObject()
{
   MeowAmount = 8;
};
Retrieving the variable of this would be echo($Cat.MeowAmount); . Not only do you need to use a global variable, but you can also use %.

What is also very useful is to add whole new sections onto client or the player, using..
Code:

function serverCmdRANDOMFUNCTION(%client)
{
   %client.ITEMS = new ScriptObject()
   {
      HealthPotion = 1;
   };
}

As you can see above, I used random names for the function's and stuff, but what you can do with this is your choice. You could even make a variable database to store information.




Lesson C-1 - Sim Objects
The only thing you will learn in this lesson will be the differences between "Script" and "Sim" objects.

Sim Objects operate just like Script Objects. The only difference is that Sim objects delete objects that no longer exist, automatically. This can be used to avoid duplicates of anything. In general, it is more primarily use than Script Objects. (In my opinion)

[WIP]



Here is a complete reference to the default torque functions.


Last edited by Honor on Fri Feb 22, 2013 1:35 am; edited 26 times in total (Reason for editing : Made a bunch of corrections, worded stuff better, not finished)
Honor
Honor
Admin

Posts : 56
Join date : 2012-12-27
Age : 26

https://elitesq.forumotion.com

Back to top Go down

Complete Torque-Script guide. [WIP] Empty Re: Complete Torque-Script guide. [WIP]

Post  Wound Wed Jan 02, 2013 6:58 am

looks good so far, hopefully this can help me and others in the future, keep adding onto it!
Wound
Wound
Blocklandian

Posts : 16
Join date : 2013-01-01
Age : 25
Location : USA

Back to top Go down

Complete Torque-Script guide. [WIP] Empty Re: Complete Torque-Script guide. [WIP]

Post  Honor Wed Jan 02, 2013 7:06 am

Thanks for the support! Will continue to work on it over time. Very Happy
Honor
Honor
Admin

Posts : 56
Join date : 2012-12-27
Age : 26

https://elitesq.forumotion.com

Back to top Go down

Complete Torque-Script guide. [WIP] Empty Re: Complete Torque-Script guide. [WIP]

Post  Meshiest Fri Jan 04, 2013 2:08 am

Parenting

function overWritingFunction(%arguments)
{
echo("Adding something before calling the original function");
Parent::overWritingFunction(%arguments); //Calling the original function
echo("Adding something after adding the original function");
}

Object Functions

function FXdtsBrickData::remove(%this) //%this variable is the object
{
%this.delete(); //Deleting the object
}

Hope this helps Razz

Also:
() - Parentheses
{} - Braces/Curly Braces
[] - Brackets/Square Brackets

Meshiest
Blocklandian

Posts : 68
Join date : 2013-01-02
Age : 26
Location : The Internet

http://marblemodz.forumotion.com

Back to top Go down

Complete Torque-Script guide. [WIP] Empty Re: Complete Torque-Script guide. [WIP]

Post  Honor Fri Jan 04, 2013 7:29 pm

I am planning to do those for later lessons.
Honor
Honor
Admin

Posts : 56
Join date : 2012-12-27
Age : 26

https://elitesq.forumotion.com

Back to top Go down

Complete Torque-Script guide. [WIP] Empty Re: Complete Torque-Script guide. [WIP]

Post  Meshiest Fri Jan 04, 2013 8:25 pm

Honor wrote:I am planning to do those for later lessons.
OKE

Meshiest
Blocklandian

Posts : 68
Join date : 2013-01-02
Age : 26
Location : The Internet

http://marblemodz.forumotion.com

Back to top Go down

Complete Torque-Script guide. [WIP] Empty Re: Complete Torque-Script guide. [WIP]

Post  Honor Sat Jan 05, 2013 2:32 pm

OKE?
Honor
Honor
Admin

Posts : 56
Join date : 2012-12-27
Age : 26

https://elitesq.forumotion.com

Back to top Go down

Complete Torque-Script guide. [WIP] Empty Re: Complete Torque-Script guide. [WIP]

Post  Meshiest Sat Jan 05, 2013 4:38 pm

Honor wrote:OKE?
ok

Meshiest
Blocklandian

Posts : 68
Join date : 2013-01-02
Age : 26
Location : The Internet

http://marblemodz.forumotion.com

Back to top Go down

Complete Torque-Script guide. [WIP] Empty Re: Complete Torque-Script guide. [WIP]

Post  Honor Thu Jan 10, 2013 10:03 pm

ohh, k.
Honor
Honor
Admin

Posts : 56
Join date : 2012-12-27
Age : 26

https://elitesq.forumotion.com

Back to top Go down

Complete Torque-Script guide. [WIP] Empty Re: Complete Torque-Script guide. [WIP]

Post  Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum