Gw Temp

Menu

Tutorial - 'Control Structures (loops, conditionals) in Sphere' by Guest

An item about RPGMaker 2000 posted on

Blurb

A tutorial on If...else statements, loops and control stuctures in Sphere

Body

I've been keeping my eyes opened and the most undocumented part of Sphere are the if...else statements and loops, control structures. The sound of this words may make beginners shudder, but there's nothing to be afraid of really. These control structures are one of the most important parts of programming. Loops can be found in all C competition programs. Loops are even more important in algorithm programming and are the things that make a programmers life simple. As you should know I do lots of C, VB, C++, Delphi and Pascal (I don't do Java and not much Asm, which I did but gave up because it was too tedious). And of course you may say, what are all this silly stuff, I want to learn Sphere. What I would reply is that Sphere is connected to lots of these languages in terms of syntax and in algorithms. C is a structured language and is simplistic and thus we will relate a lot with it during this course of this Sphere tutorial. The conditionals in Sphere are very much like those in C.

This is a switch statement that works a lot like the if statement and is in fact better that the if statement for large decisions. It work like this.

switch (x) {
case (y): {
}
case (z): {
}
default: {
}
}

This should be simple enough to understand, but without any variables inside, it might be hard. So let me illustrate this better with finding the key pressed.

var keycode;
keycode = GetKey();
switch (keycode) {
case (KEY_ESCAPE): {
Exit();
}
case (KEY_ENTER): {
Next();
}
default: {
Next2();
}
}

Ok, what happens is this. First you get the key entered from the users keyboard by using the GetKey() function and then you store this key in the variable keycode. Next, you use a switch statement to decide what the keycode is. Next you search for KEY_ESCAPE and KEY_ENTER in from the key entered and if the keycode is KEY_ESCAPE then you exit the program and if the keycode is KEY_ENTER then you run the function Next. The next statement is the default statement which tells Sphere that if the key entered is neither KEY_ESCAPE or KEY_ENTER then Sphere should run the function Next2(). The variable keycode must be put in the switch statement to tell Sphere that the variable you want to search is keycode and the case statements tell Sphere what to look for in the variable.

Next, we will look at the if statement which is similar to the switch statement.

var keycode;
keycode = GetKey();
if (keycode == KEY_ESCAPE) {
Exit();
}
if (keycode == KEY_ENTER) {
Next();
}
else {
Next();
}

The if statement is even simpler to understand as it is a lot like the English. just represented here and there by symbols and stuff. Now to break down the code. like the above switch statement, this if statement does the same thing. It gets the users input key and stores it in key code. Then it tells Sphere, if the value keycode is KEY_ESCAPE then, it will exit the program. Otherwise, if the keycode is KEY_ENTER then the Next function is run. the last statement works like the switches default statement. The else statement, runs the function next if the key pressed is not enter or escape. One more thing. When doing control structures, it would be best to indent your code. (Using the TAB button of course). The code within the control should be indented, making the code look neat and easy to debug and easy to understand. One more very important thing is that in C and Sphere, to compare, one must use == and not =. This will be something new to Basic programmers. The use of = in C and Sphere is strictly for assignment like i = 1 and so on. So to compare whether keycode is equivalent to KEY_ESCAPE a Sphere programmer must use the == operator. (by the way, are you freaked out by my language? if you are, just grab any C book and look through it and you should understand me better.)

while (x==2) {
}

Next, one of the most favorite loops used in Sphere and C. (Of course there are only 3 kinds of loops. My personal preference is the for loop since it was the first loop I used and learnt.) The while loop is very simple it loops until the condition is reached, which in this case is x is equivalent to 2. But how do we use this loop when doing our Sphere programming?

var x = 0;
while (x==2) {
GetKey();
x++;
}

I couldn't think of a better example so I just used this lame one, bare with it. This shows the power of a loop. (Which can be destructive too. Do you know that a high speed loop can burn out your processor? These loops are called infinite loops, which obviously, loop forever. Kids, don't try this at home.) Ok, the loops I'll teach you are just basic loops and these won't burn out your processor, so don't worry. Anyway, with today's advanced Pentium technology, it isn't as dangerous as before. Now, back to the stuff, this while loop gets the key the user pressed for 3 times. this is done by using x as a sort of counter and so when x reaches 2 the loop will exit. x is increased each time the loop is ran so from being 0, it will become 1 and then 2 and Sphere will exit the loop. Simple enough. How about doing something more useful like doing something with the data.

var x = 0;
var keycode;
while (x==10) {
keycode = GetKey();
if (keycode == KEY_ESCAPE) {
Exit();
}
x++;
}

This loop, gets 11 keystrokes and then exits the loop, but this loop can also be borken and the program exited when the escape key is pressed anytime in the loop. But this decision making is done in the loop itself. Can store what I loop in in a variable and then use it after I've exited a loop, the answer, sadly, is no. But you can store it in an array of variables. This will be talked about in the tutorial on arrays, but I'll feed your code-hungry minds with some code on arrays in loops first.

var x = 0;
var keycode = new Array(11);
while (x==10) {
keycode[x] = GetKey();
x++;
}

I shan't explain this first so you can try to figure it out on yourself or check out my arrays tutorial. This isn't all, all this does is store all the keys pressed in an array. We need to do another loop to get all these out and scan them.

var x = 0;
var keycode = new Array(11);
while (x==10) {
keycode[x] = GetKey();
x++;
}
x=0;
while (x==10) {
if (keycode[x]==KEY_ESCAPE) {
Exit();
}
x++;
}

Ok, I must admit this is rather useless and can be done with a much simpler method, but what I'm trying to do is illustrate the theory of it all. This reads in the keystrokes into an array and then scans the array and finds all the KEY_ESCAPES and if a KEY_ESCAPE is found the game is exited. This is explained in detail in the array tutorial.

The next kind of loop is the Do While loop, a little like the wile loop but with different priorities. This Do While loop does the certain statement then checks whether the condition is met. Whereas the While loop checks the condition first and then does the statement. This different in priorities is normally not taken into consideration and so I shall not talk much about it but delve straight into the syntax.

do {
GetKey();
} while (x==true);

Simple enough. The do while loop is not so commonly used and is quite like the while loop, so you should just take note of the syntax.

The for loop, a loop which is more difficult to understand as it has all its increments, declarations and conditional all in the same loop. That's why I like it. It looks rather neat, neater than a while loop and functions about the same.

for (start; conditional; update) {
}

This is what is a version of it that is closer to your heart. Notice I like to use i as a variable, i don't know why but I just use i, j and k in loops, maybe because of my C background. Of course, you can use any variable.

for (i=0; i<=10; i++) {
}

This is what it does. First, it sets i to 0 and then it starts looping until the condition is met, which is i is equals to 10 and then it does the increments automatically. Of course you can use the increments i++, ++i, i--, i + 1 and whatever else.

var keycode;
for (i=0;i<=10;i++) {
keycode = GetKey();
if (keycode == KEY_ESCAPE) {
Exit();
}
}

This is the for loop implementation of our earlier while loop and should be easy to understand. First, I declare the keycode to store the name of the key entered. Then i loop it with a for loop, first setting i to 0 and then saying that i will stop only when i is equals to 10. i will be increased each time the loop runs.

That's it for controls. Check out the next few tutorials and find out all about coding in Sphere.
http://spheresuite.sf.net/ is the original source of this tutorial.