Author Topic: Script Mentor  (Read 1428 times)

Alright, i just want to know a good chunk about scripting. If there are any good scripters out there who want to share there knowledge, please write up a tutorial on what many things in a script mean, what they do, and how to use them, an example script, etc.

Thank You! (If this should be in help then just please move it without complaints)

**Do not tell me to look at scripts and find out how to on my own or i will kick your ass

http://returntoblockland.com/wiki/index.php?title=Introduction_to_Scripting
Quote
This guide has not been completed. It still requires some formatting.
Blockland was created using the Torque Game Engine which is coded in C++ and has its own scripting language, TorqueScript, which is loosely based on C# and very similar to other languages such as PHP.
Contents [hide]
1 Code Comments
2 Variables
2.1 Local Variables
2.2 Global Variables
3 Datatypes
3.1 Difference Between Literal and Tagged
3.2 Booleans
4 Operators
4.1 Assignment Operators
4.2 String Operators
4.3 Escape Sequences
4.4 Mathematical Operators
4.5 Logical Operators
4.6 Bitwise Operators
5 Functions
5.1 Defining Functions
5.2 Using Functions
6 Conditional Control Structures
7 For Loops
8 Arrays
[edit] Code Comments

Comments are prefixed with two forward slashes. These come in useful to explain how a portion of code works, etc. A few weeks later you may come back and not know what you were trying to do because of the lack of comments.
Example 1. Comment example A
function theCow()
{
    //Ok, I think i'll set the noise variable to the sound a cow makes
    %noise = "moo";
    //now i'll print that to the screen!
    echo("goes "@%noise);
}
Alternatively, you can use a /* to comment an entire block of code, like so:
Example 2. Comment example B
function theCow()
{
    /*
    Ok, I think i'll set the noise variable to the sound a cow makes
    and then i'll print that to the screen!
    */
    %noise = "moo";
    echo("goes "@%noise);
}
[edit] Variables

Variables are extremely important to programming. Their basic purpose is to store information.
Variable identifiers or names have some specific rules. Firstly, they can only contain alphanumeric characters and underscores, but they can't start with a number. Unlike other languages like Visual Basic and C++, variables do not need to be declared before use.
[edit] Local Variables
Local variables are prefixed with %.
Example 1. Local variable example
%name = "Blockhead";
TorqueScript uses scopes. That means once it leaves it current context (such as when the variable is used within a function), it will be destroyed and will no longer be accessible.
Example 2. Local variable scope example
function testMe()
{
   %name = "Blockhead";
}
function ohMyTestAgain()
{
   echo(%name);
   // It will echo out an empty string because the
   // variable name is local and disappeared after
   // the function testMe ended
}
testMe();
ohMyTestAgain();
echo( %name ); // It will be empty once again
[edit] Global Variables
Global variables are prefixed with $.
Example 1. Global variable example
$name = "Blockhead";
Scopes for global variables do not exist - they are global and can be accessed anywhere.
Example 2. Global variable scope example
function testMe()
{
   $name = "Blockhead";
}
function ohMyTestAgain()
{
   echo( $name );
   // Prints Blockhead
}
testMe();
ohMyTestAgain();
echo( $name ); // Prints Blockhead
[edit] Datatypes

TorqueScript has a variety of datatypes. These are the types of variables that are stored. These are:
   * Numeric
         o Integer (i.e. 123)
         o Floating Point (i.e. 1.234)
         o Scientific Notation (i.e. 1234e-3)
         o Hexadecimal (i.e. 0xc001)
   * Strings
         o literal string (i.e. "abcd")
         o tagged string (i.e. 'abcd')
   * Booleans
   * Arrays
   * Vectors
   * Matrices
Datatypes in TorqueScript are very loose. What does it mean? Look at the example below.
Example 1. Datatypes
$a = "4";
$b = 4;
// $a and $b are equal in TorqueScript even though
// $a is a string and $b is a number
[edit] Difference Between Literal and Tagged
To make the protocol between servers and clients efficient, strings that are "tagged" are assigned a numeric value internally. For the first time this string is sent across a network, the literal value will be sent. For subsequent sends, its numeric value will be sent instead.
Literal strings are not tagged. They are sent literal and are printed literal.
If you do use tagged strings and you receive one, you must detag it before printing it. Note that you can only detag a string that you received not created on the same client.
Example 1. Tagged vs Literal
// Execute this in console to see
$a = "This is a regular string";
$b = 'This is a tagged string';
echo( " Regular string: " $a );
echo( "  Tagged string: " $b );
[edit] Booleans
Booleans are practically yes or no except represented as true or false.
True is also equal to the number 1 in TorqueScript and false is also equal to the number 0.
Example 1. Boolean example
$large = true;
$dumb = false;
[edit] Operators

Operators do something to variables or compare.
Note: These article uses conditional structures (if...) as examples. They will be discussed in another article.
[edit] Assignment Operators
This assigns a variable a value.
= assigns a value
Example 1. Assignment operators example
%a = "hi!"; // %a is now hi!
%a = 3; // %a is now 3
[edit] String Operators
These operators affect strings by putting them together.
@ concatenates two strings (sticks two strings together to form one) TAB concatenation with a tab SPC concatenation with a space NL concatenation with a newline
Example 1. Concatenation example
%a = "hello ";
%b = "you!";
echo( %a @ %b ); // Echos hello you!
echo( "how do" @ TAB @ "you do?" );
echo( "how do" @ SPC @ "you do?" );
echo( "how do" @ NL @ "you do?" );
[edit] Escape Sequences
These are special characters you can put in strings. \n newline \r carriage return \t tab \c0...\c9 colorize subsequent text (see far below) \cr reset to default color \cp push current color on color stack \co pop color from color stack \xhh two digit hex value ASCII code \\ backslash
Example 1. Escape example
echo( "How do you do?\n\c2NOT GOOD!" );
For the colors, they are: \c0 WHITE - Default Color. \c2 ORANGE - Client Messaging. \c3 PUKE GREEN - General Messages. \c4 RED - Server Messages/Warnings. \c5 GREY - Free Color. \c6 BLUE - Game Mode Colors.
[edit] Mathematical Operators
These do math operations on numbers/numeric variables. + adds two numbers - subtracts two numbers
multiplies two numbers
/ divides two numbers % (modulus) gets integer remaining by dividing by a number += adds 2 numbers and assigns result to first -= subtracts 2 numbers and assigns result to first
= multiply 2 numbers and assigns result to first
/= divides 2 numbers and assigns result to first %= completes modulus of 2 numbers and assigns result to first ++ increments variable -- decrements variable
Example 1. Mathematical operators example
%a = 4;
%b = 2;
%c = %a / %b; // %c is 2
%a += 2; // %a is now 6
%b--; // %b is now 1
Comparative Operators
These compare two values. == both values are equal != both values are not equal < first value is less than > first value is greater than <= first value is less than or equal >= first value is greater than or equal $= both strings are equal !$= both strings are not equal
Example 1. Comparative operators example
%a = 5;
%b = 10;
%c = 5;
%d = "Hi!";
%e = "No!";
if( %a == %c ) echo( "true!" ); // echoes
if( %b > %c ) echo( "true!" ); // echoes
if( %b <= %a ) echo( "true!" ); // doesn't echo
if( %d !$= %e ) echo( "true!" ); // echoes
[edit] Logical Operators
Just logic! ! evaluates as opposite (reverses) && both values must be true || one value must be true
Example 1. Logical operators example
%a = true;
%b = false;
%c = true;
if( !%b == true ) echo( "true!" ); // echoes
if( %b && %c ) echo( "true!" ); // doesn't echo
if( %b || %c ) echo( "true!" ); // echoes
[edit] Bitwise Operators
If you are new to programming and do not understand the following, skip it for now. ~ (bitwise NOT) flips the bits of its operand | (bitwise OR) returns a one in a bit if bits of either operand is one & (bitwise AND) returns a one in each bit position if bits of both operands are ones ^ (bitwise XOR) returns a one in a bit position if bits of one but not both operands are one << (left shift) shifts its first operand in binary representation the number of bits to the left specified in the second operand, shifting in zeros from the right >> (sign-propagating right shift) shifts the first operand in binary representation the number of bits to the right specified in the second operand, discarding bits shifted off != performs a bitwise OR and assigns the result to the first operand &= performs a bitwise AND and assigns the result to the first operand ^= performs a bitwise XOR and assigns the result to the first operand <<= performs a left shift and assigns the result to the first operand >>= performs a sign-propagating right shift and assigns the result to the first operand
[edit] Functions

Functions allow code to be reused. Copying the same code in 20 different places will get annoying, bloat your script and be a huge pain to manage.
It's a code block that can be called from different places. It additionally can accept arguments (information for the function to process) and return a value.
[edit] Defining Functions
Example 1. Basic function
function helloWorld()
{
   echo( "Hello world!" );
}
The above function is very simple but it's reusuable. We'll now show you a function that accepts arguments.
Example 2. Function with arguments
function printName( %name )
{
   echo( "Hello" SPC %name SPC "!" );
}
The variable name is passed to the function and it echoes a string with the argument in it.
Example 3. More complex function
function buildxyz( %x, %y, %z, %num )
{
   for( %i = 0; %i < %num; %i++ )
   {
       commandToServer( 'plantBrick' );
       commandToServer( 'shiftBrick', %y, -%x, %z );
   }
}
The above function builds an object a certain way. As you see, it takes 4 arguments and uses them.
Example 4. Function that returns
function addThree( %num )
{
   %num = %num + 3;
   return %num;
}
This function returns a value.
[edit] Using Functions
This is easy!
Example 1. Calling a function example
helloWorld();
printName( "Blockhead" );
buildxyz( 4, 5, 3, 100 );
$count = addThree( 5 ); // $count is 8 now
[edit] Conditional Control Structures

Conditional structures are very vital! They check whether a or many conditions are met and will execute code as needed. This is where logic and comparative operators come in especially!
Some example uses of this would be:
   * Checking whether a certain user is an admin
   * Checking how many inventory slots are used
   * Checking if a password is right
There are many other uses as well! Let's continue.
Let's take the following if loop.
Example 1. Simple conditional example
%a = 10;
if( %a == 10 )
{
   echo( "true!" );
}
It checks whether the variable %a equals to 10.
Let's now introduce the else.
Example 2. Conditional example with else
%a = 11;
if( %a == 10 )
{
   echo( "true!" );
}
else
{
   echo( "not true!" );
}
Oh no! %a does not equal 10 this time. What happens? Because the condition isn't true, it defaults to the else block and echos "not true!".
Now, let's move one level up and use else if's.
Example 3. Conditional example with else if
%a = 11;
if( %a == 10 )
{
   echo( "it is 10!" );
}
else if( %a == 11 )
{
   echo( "it is 11!" );
}
else
{
   echo( "not true!" );
}
In the example, it would echo "it is 11!" because the first condition is not met so it checks the next one. You can have multiple else if blocks. You do not have to have the else block either. If you use both else if's and else's, if none of the other conditions are met, it will default to the else block.
Now, additionally you can combine conditions without using nested conditional blocks (one in another). Take a look at the example below.
Example 4. Conditional example with multiple conditions
%a = 11;
if( %a == 10 || %a == 11 )
{
   echo( "it is 10 or 11!" );
}
Other ways to group conditions can be read about in logical operators.
Last of this lesson is: these blocks do not have to have the braces ({ and }).
Example 5. Conditional example without braces
%a = 11;
if( %a == 10 )
    echo( "it is 10!" );
else if( %a == 11 )
    echo( "it is 11!" );
else
    echo( "not true!" );
This only works if you only have one instruction (in this case, echo) after the block. The following will not work as expected:
Example 6. Conditional example without braces incorrectly
%a = 11;
if( %a == 10 )
    echo( "it is 10!" );
    echo( "true!" );
There are other operators than the ==. Check out comparative operators. Do remember that are 2 different operators for comparing whether strings are equal.
[edit] For Loops

For loops start with a variable set at certain value (usually number) and reiterates through a loop increasing/changing the value every time until a condition is no longer met.
This is especially useful to do something repetitive.
Example 1. Numeric for loop
for( %i = 0; %i < 10; %i++ )
{
    echo( "Hmm..." @ %i );
    // Note that you can use the variable %i
}
In the above code, the variable %i is set at 0. Everytime the loop reiterates, it increases the the %i variable by 1 (that's what ++ after a variable does; see operators). The loop will loop itself until the variable %i is no longer less than 10 (it will stop at 9). "Hmm..." (without the quotes) will be echoed 10 times.
The basic structure consists of three parts: the initial action, the condition that must met at all times for the loop to continue, and the action to do after one iteration.
Last of this lesson is: these blocks do not have to have the braces ({ and }).
Example 2. For loop example without braces
for( %i = 0; %i < 10; %i++ )
   echo( "Hmm..." @ %i );
This only works if you only have one instruction (in this case, echo) after the block. The following will not work as expected:
Example 3. For loop example without braces incorrectly
for( %i = 0; %i < 10; %i++ )
   echo( "Hmm..." @ %i );
   echo( "echo this 10 times!" );
[edit] Arrays

Arrays store a group of information together.
A few uses of arrays include (as examples):
   * Storing a group of information about a player
   * Storing a group of information about a server
For arrays, each array element is identified by a number and/or a string which is called a key. The array element then has the actual value. For purposes of simplicity, the following examples use a numeric key: 0, 1, 2, etc. Let's take an example such as the following.
Figure 1.
cheese apples bananas
An array could store all those values in one variable. This array is one dimensional because there is only one column of data.
Example 1. Specifying Figure 1 in an array
$food[0] = "cheese";
$food[1] = "apples";
$food[2] = "bananas";
Beside just being limited to 1 column of data, we can have multidimensional arrays. Let's take a look at Figure 2.
Figure 2.
0 blockland, half-life 2
1 chess, checkers
2 mario

Example 2. Specifying Figure 2 in an array
$games[0,0] = "blockland";
$games[0,1] = "half-life 2";
$games[1,0] = "chess";
$games[1,1] = "checkers";
$games[2,0] = "mario";
Arrays serve as great tools to store related information together.
Unlike most programming languages though, TorqueScript allows you to specify your arrays different ways. Take a look at some of the following.
Example 3. Referencing an array
// One dimension
echo( $food[0] );
echo( $food0 );
// Multidimensional
echo( $games[0_0] );
echo( $games[1_0] );
echo( $games[2,0] );
echo( $games0_0 );
echo( $games1_0 );
echo( $games2_0 );
Because you can specify array elements by putting a number right after the variable name, this may get confusing when you use variable names with a number at the end.
Additionally, as previously mentioned, keys can be a string.
Example 4. Key as string
// One dimension
echo( $food["bad"] = "radish" );
echo( $food["good"] = "cheese" );

**Do not tell me to look at scripts and find out how to on my own or i will kick your ass
Asking to be spoonfed knowledge is just handicapped, I think you should rethink how you're going to go about this.

-snip-
Thank you

Edit: Helped a lot in understanding some of the more "in detail" parts of scripting, but the only problem is im missing some basic information, like in this example script:

function theCow()
{
    //Ok, I think i'll set the noise variable to the sound a cow makes
    %noise = "moo";
    //now i'll print that to the screen!
    echo("goes "@%noise);
}


Bold: I have no idea what the forget that means
« Last Edit: February 05, 2009, 10:00:38 AM by Snip3r »

Thank you

Edit: Helped a lot in understanding some of the more "in detail" parts of scripting, but the only problem is im missing some basic information, like in this example script:

function theCow()
{
    //Ok, I think i'll set the noise variable to the sound a cow makes
    %noise = "moo";
    //now i'll print that to the screen!
    echo("goes "@%noise);
}


Bold: I have no idea what the forget that means

Quit being such an ignorant bitch.  Why people think they can't learn things for themselves is beyond me.

http://en.wikipedia.org/wiki/Echo_(command)


**Do not tell me to look at scripts and find out how to on my own or i will kick your ass
Maybe learn some basic manners first.

What I did was look at basic scripts, mess around with them, and basically lurk around Coding Help. When you see that almost every line ends with a ; you'll start to figure out that it means the end of a line.

When you see that almost every line ends with a ; you'll start to figure out that it means the end of a line.

You made my day.

Falcon is right, I looked at pickles scripts and I learned quite abit.

Quit being such an ignorant bitch.  Why people think they can't learn things for themselves is beyond me.

http://en.wikipedia.org/wiki/Echo_(command)
Thank you, Trader. I will be a bitch all i want, you cant stop me.

I will be a bitch all i want, you cant stop me.
We can't stop you from being an utter idiot, but we can stop you from learning, apparently.

We can't stop you from being an utter idiot, but we can stop you from learning, apparently.

Haha, wow.  Nice ownage.

Damn, this kid is such a whiny bitch.

Quote from: Snip3r
What the forget is this curly brace?! WHAT THE HELL IS IT DOING!?!?! TELL ME OR I KICK YOUR ASS

Edit: I have decided that you're destined to be a failure at everything you set out to accomplish.
« Last Edit: February 06, 2009, 04:34:33 AM by Ephialtes »