Flik: Sphere Central!
Docs!
 |
Sphere Docs
This is how to repeat certain parts of code, over and over...
Say you want to do something 20 times, but that something doesn't actually change much each time...
An example of this is, showing all the text from an array of text.
You have an array of text strings, defined like so:-
var txt_array = new Array();
txt_array[0] = "Once apon a time...";
txt_array[1] = " In a land far far away.";
txt_array[2] = " A boy met the girls of his dreams...";
txt_array[3] = " This girl was in trouble though"
+ "and needed help.";
txt_array[4] = txt_array[5] = txt_array[6] = "...";
txt_array[7] = " He got ready to start his quest...";
txt_array[8] = " Hoping nothing was going to go wrong...";
txt_array[9] = " This was his destiny!";
Now to me it seems obvious how to draw this to the screen, in succession.
Here we use a simple while loop.
You could do this:-
EvaluateSystemScript("time.js");
var index = 0;
while(index < 10)
{
var txt = txt_array[index];
GetSystemFont().drawText(0,0, txt);
Delay(250);
index = index + 1;
}
Now if you know arrays, this is easy to understand. All you are doing is putting the index'ed element of the array into the txt parameter of the font.drawText method.
However, you could write it out the long way round. But this way saves you all that time and effort.
The while loop has one part:
while (condition)
statement
condition: (repeat the loop until)
e.g. index < 10
statement: some code to be executed.
Note that statement can be zero statements using a semicolon.
One statement or many using brackets.
e.g.
while (condition);
while (condition)
some_code();
while (condition)
{
some_code();
some_other_code();
}
Semicolons go at the end of lines of code, loops are more like the begining of a new paragraph. ;)
Now, the problem with this is using that damn index variable. ;)
Its just a pain to do, so lets use the for loop.
EvaluateSystemScript("time.js");
for(var index = 0; index < 10; ++index)
{
var txt = txt_array[index];
GetSystemFont().drawText(0,0, txt);
FlipScreen();
Delay(250);
}
The for loop has three parts.
for (initiliasation; condition; increment)
statement
initiliasation: (do this at the start of the loop)
e.g. var index = 0
condition: (repeat the loop until)
e.g. index < 10
increment: (do this at the end of each iteration of the loop)
e.g. ++index
Alright, next thing.
Notice that if your condition in the while loop is false it will not run?
var condition = false;
while(condition)
{
}
To make a loop run once no matter what, you can do this:-
Alright, an example of a cool condition that is designed to be ended in the body of the loop.
var done = false;
while(!done)
{
if(some_function() == true)
{
done = true;
}
}
Finally, I think I'll cover break statements.
Sometimes in a loop you want it to end before it runs all the way through and back to the header.
Loops only end when the header is evaluated.
This means that I might have alot of code that gets executed when it shouldn't!
The solution to this problem, is to just break from the loop altogether.
var done = false;
for(var i = 0; i < 30; ++i)
{
if(done == true)
break;
}
This will break from the code.
The other thing is continue statements.
In a loop if you put:-
A little side-note about switch case/break
Each case needs a break unless you want the code to fall through to the next level down.
For example if you had an input and wanted to check for Y or y and N or n, you could do:
switch (input) {
case "y":
case "Y": Yes(); break;
case "n":
case "N": No(); break;
default:
Unknown();
}
The above code calls Yes No or Unknown based on input, which should be "Y" or "n" or such.
Without the break's in the places they are, the code would behave very differently.
It will go back to the header of the loop and evaluate it.
Now you know the basics of loops, breaks and continues. ;)
Thats its folks, soon, I will cover some info on map rendering.
Made By Flik!
|
|