Game Development Community

Explinations on some scripts

by Jared Reno · in Technical Issues · 07/08/2007 (11:53 pm) · 8 replies

Hey Im trying to learn programming and Ive run into a speed bump. I dont understand a couple of "strings" and was hopeing you could explain them and what some of these operators mean. all this is in TorqueScript

for (%index=0; %index < %numFruitTypes; %index++)

I have no clue what that means. If Idx means index then its refering to the values I initialized earlier in this script. its the last bit with the ++ that throws me off.

echo("Cost of" @%names[%index] @ "@%cost[%index]);

In this one its the @'s and the boxed in index's that confuse me. I just dont understand it.

and now if you would explain these operators

; for that one I get it ends something just dont know what
$
%

oh and one last thing: what is Index? I dont get why I had to add it to the end of all my variables.


Thansk for the help?

#1
07/09/2007 (5:08 am)
Hello Jared,

Check out this link www.garagegames.com/docs/tge/general/ch05s02.html

If you look up "Variables" and "String Operators" there will be some useful information for you.

%variable_name, % means that it is a local variable. A local variable exist only in the scope of the function it is declared within.

$variable_name is a global variable ($), which exist all over your program as long as the program runs.

Regarding the for loop.
%index=0 means start counting from 0.
%index < %numFruitTypes means where to stop the loop (e.g as long as %index is less than..)
%index++ means %index = %index + 1

So you will loop from 0 to %numFruitTypes-1, %index will go from 0,1,2,3.. %numFruitTypes - 1

Note that %index is just a variable name you choose, it could be %i, %k, %x, %counter or something else.

The @ operator concatenates two strings, e.g "hello" @ "world" = "helloworld"

%names[] is an array, check the link above for explanation. %names[%index] is just to get what is in the array on a certain position, you can write %names[0]. %names[1], %names[x..] as well.

Regards,
Christopher.
#2
07/09/2007 (5:17 am)
Jared here's the answers for you:

1. for (%index=0; %index < %numFruitTypes; %index++)
This is a known as a for loop and allows the code to go round in a loop a set number of times and execute the statements between the curly brackets {} there are three parts to the for loop
a) Initalizer (%index = 0) - this just sets the variable %index to zero, we could just as easily start at 1, 5, or whatever value you need to.
b) Condition (%index < %numFruitTypes) - we don't want to go round in the loop forever or we'd get stuck, and so you have your condition to tell the program when to exit the loop, in this instance we have said exit when %index is greater than %numFruitTypes... for example if we had 10 fruit types we'd loop around 10 times.
c) Increment (%index++) - if we went round the loop every time with the same index value then the condition we specified above would never be true, and so we need to modify the value of %index, the syntax used here was %index++ which is a way of saying add 1 to %index and is the same as saying - %index = %index + 1, similarly you could use %index-- to subtract 1 from it and count down.

There is nothing special about the name "%index" you could just as easily use %x, %fred, %anything_you_like, when writing a for loop you need a variable that you can use for the three parts.

For an example the following piece of script will start at 10 and count down to zero displaying the results in the console window
for (%x = 10; %x >= 0; %x--)
{
    echo(%x);
}

2. The @ symbol is the way in Torquescript that you add two strings together, for numbers you're used to using the + symbol it's the same thing but for strings. For example:

%string1 = "String 1";
%string2 = "String 2";
%string3 = %string1 @ %string2;
echo(%string3);
This would print "String 1String 2" to the console window.

3. The boxed [%index] is a way of accessing a specific item in an array, think of arrays as pigeon wholes in a post office/mail room/office - we're used to putting labels on the pigeon whole perhaps you name or department to know which letters to post into which pigeon whole, with computers we use numbers to address these pigeon holes. For example an array of fruit might look like this:
+--------+
0 | Apple  |
  +--------+
1 | Pear   |
  +--------+
2 | Banana |
  +--------+
3 | Orange |
  +--------+

and if we typed echo(%fruitnames[1]); it would print Pear to the console. So if you look at your example where it said %names[%index] what it is saying is access the %names array and get item in pigeon hole with the number %index (we saw before in my explanation of the for loop that %index was initialized at zero and then incremented.

4. echo("Cost of" @%names[%index] @ "@%cost[%index]);
Hopefully you should be able to figure out now what this is doing? if not read over items 2 and 3 again.

5. The three symbols you weren't sure of:
a) The ";" symbol is used to denote the end of a statement in Torquescript much the same as when you write English you should end your sentence with a full stop. Where programming differs from English is that when you write english you can be lazy and miss out full stops or indeed punctuation altogether, the same is not true for programming - if you miss out your semi-colon then you will get an error or you'll find your code doing something it's not meant to. I will guarentee you that you'll have this happen, everyone always misses them from time to time and it's one of the first things to check when you get an error. Learning where to use them is a case of practice because they're not used at the end of a loop statement for example, so if you're not sure do a search through the code for something similar and see where they are used.

b) The "$" and "%" symbols are used before variable names to specify the scope of that variable, a dollar $ to say it's global variable and percent "%" to say it's local. So what's the difference? Well a global variable is easy, if you create a variable called $my_global then that variable is available to be used in every other script in your game, every function, everywhere. A local variable say %my_local only exists within that block of code and when the block finishes the variable doesn't exist either, in basic terms think of a block of code as being what is between two curly brackets {}. So using an example of our for loop that counted down from 10;

for (%x = 10; %x >= 0; %x--)
{
    echo(%x);
}

echo(%x);     <----- here I am outside the curly brackets and so local variable %x doesn't exist anymore

The same is true for functions:
function do_something()
{
    %my_local = "Fred";       // create a local variable, remember it only exists between the curly brackets
    $my_global = "Harry";    // create a global variable.
}

function call_my_other_function()
{
    do_something();    // call the function above to create the variables
    
    echo(%my_local);  // this prints nothing because %my_local doesn't exist here.
    echo($my_global); // this would print Harry because it's a global and exists everywhere.
}


Hope that helps you understand some more, if you're new to programming these concepts can sometimes take a little while to sink in but keep at it and I can assure you they do.
#3
07/09/2007 (5:44 am)
Ok, I'll start simple first.
; is used to tell the compiler that its reached the end of that line. It like a period for computers.
$ is to tell torque that this variable is a global variable, exp: $Num = 3; $Num can now be accessed from anywhere.
% tells torque that this variable is a local variable and is not accessible from anywhere but its own function.

the "for" loop, is for looping a piece of code a certain number of times, in this case "%numFruitTypes" many times, example:
for (%sumNum = 0; %sumNum < 5; %sumNum++)
{
   echo("for loop has looped");
}
That should echo "for loop has looped" to the console 6 times.

Now theres also "%names[%index]" and "%cost[%index]", witch are arrays, example:
%names[0] = "Apple";
%names[1] = "Grape";
%names[2] = "Pineapple";

echo(%names[1]);
Will echo "Grape" to the console.

@ is a way of attaching text to other stuff, example:
echo("How are you?"); //echo "How are you?"
echo("How are " @ "you?"); //echo "How are you?"

%num = 1;
echo("%num is: " @ %num); //echo "%num is: 1"
Now we can put it all together.
function getFruitCost()
{
   %numFruitTypes = 3; //a normal local variable

   %names[0] = "Apple";
   %names[1] = "Grape";
   %names[2] = "Pineapple";

   %cost[0] = 1.00;
   %cost[1] = 0.25;
   %cost[2] = 3.00;

   for (%index = 0; %index < %numFruitTypes; %index++)
   {
      echo("Cost of " @ %names[%index] @ ": " @ %cost[%Index]);
   }
}
And thats it. If you put that in you code, you should be able to run torque, open the console, and type "getFruitCost();", and it should show some results like:

Cost of Apple: 1
Cost of Grape: 0.25
Cost of Pinapple: 3

I hope this made some kind of since.

EDIT- Wow I'm a slow typer :-)
#4
07/09/2007 (11:23 am)
I think this thread is a textbook case of how to ask questions, and how they will commonly be answered if asked well :)
#5
07/09/2007 (11:43 am)
Heres what I dont get: if %names[%index] calls up from the names array from what is refered to the pigeon hole index why dont you have anything marked as index caleb? Index is in the loop and nowhere else besides the code block so thats what I dont understand is how it knows to call up specificly what name. and also why is the ; in between the two quotation marks?
#6
07/09/2007 (3:41 pm)
Jared - have you tried running the piece of code to see what the output is? that might help highlight what's going on, the %index in the square brackets [] is just a number so it reads the slot in the array that that number indicates. For example:

%names[0] = "Apple";
%names[1] = "Grape";
%names[2] = "Pineapple";

echo( %names[1] );      // prints Grape to the console window

%index = 1;
echo( %names[%index] );    // also prints Grape to the console

%some_other_variable = 1;
echo( %names[%some_other_variable] ); // again prints grape

If you look closer it's not a semi-colon ";" between the quotes it's a colon ":" and it's only there to be displayed on screen, he could have used anything else there
#7
07/10/2007 (12:12 am)
Thanks. Sorry if I seem slow on the uptake. Im new to this and its really really hard but I like it just cause I actually modified a script on my own and it worked. Its the whole God complex I got going on that keeps me hitting away at this. Also why I love world building.

Edit: And OMG I understand it now. the for loop sets index at 0 and increses it by one till its greater then the numfruit (going back to my example) so that it will run through that list till the condition is false. Then it stops and move on. Got it right? If you have said it before sorry for not giving you guys credit cause its just now sinking in. After awhile and some examples Ill understand this and join the Elite group that is known as Programmers. Thanks so very much!!!!!!!!!!!11

Jared
#8
07/10/2007 (6:36 am)
Yep you've got it Jared and hey don't worry about asking things we were all new at some point, it's the community here on GG are supportive to anyone that's trying to learn and as Stephen pointed out asking questions in the right way will get you the support.

Keep chipping away at topics and picking them up and you'll soon have your elite skills and god-like complex so finely tuned you'll be wiping the floor with all those minions....ahem... sorry getting carried away now. I find programming easy, artwork now that's another story, when I try and draw something a common phrase would be "ahhhh that's nice, what was your son trying to draw?"