Gw Temp


Tutorial - 'Variables in Javascript' by Ryu

An item about Sphere posted on


Get the basics of variables in Javascript. Javascript is the language Sphere uses to program its games!


Javascript Tutorial ~ [ - ] ~ Ryu

Well, I decided to write a Javascript tutorial for those people who want to use the sphere engine for one reason: I hope to one day achieve a position on the staff page; and 2: I was really bored. You'll have to forgive me for anything you don't get. If you have any questions about this tutorial, you can GWmail me or locate me on IRC. Please keep in mind that this is not the syntax for Sphere, necessarily. Parts of this tutorial may be web specific, but I haven't studied the Sphere syntax for this lanugage to be certain. Anyhow, on to the tutorial.

Note: Please understand that at the end of each line of code, you absolutely MUST put a semi-colon(;)

Section I: Variables and the syntax in which they're used.

This is a relatively simple section, and should be to long for you lazy people out there. Variables are, if you haven't used RM2K before, simply an oobject to store values in. However, the difference between Javascript variables and RM2K variables is that JScript variables can hold strings or numerical values, as well as Boolean(switch, on or off, but true/false in JScript) values. Essentially, you can have the following values work:





but not:

5 "Bleeding amazing, I must say..." false;

because the variable would not know how to print it. Also, Javascript is case sensitive, meaning that HaLo and Halo are two different variables with two different values. This may create a problem if you're not used to case sensitivity, so try your best to keep track of what you are typing.
Also, there are two ways of naming varaibles(official ways, that is): you can type it as: Ryu_Owns or ryuOwns. The first way, you simply use an underscore to seperate words, while the second way the first word is all lowercase, then the beginning of every word after that is capital.
This doesn't mean you can type I_Am_God and iAmGod and have them react as though one another. Now that you know the basic rules of variables, let's see how you declare them:
var isRyuGod = false;
this variable would be considered a boolean, however, you can always do this to change it:
isRyuGod = "True, man, true";
You may notice that var is only used in the declaring statement. That's how this language was designed, simple as that. The unique thing about this lanuage is that you can change a variable type halfway through the script, and JScript won't give a hoot(don't pollute).
That covers this section of the tutorial.

Section II: If... than... Else... and Writing
How many people that are reading this tutorial and have used RM2K know what an if...than...else statement is? All of you. What? You say you don't? Wrong. Fork conditions are the equivalent to these statements. Though the syntax is just a tad bit different, how they are used is not. Well, slightly, in JScript you can test a few more things than with RM2K.
Note: Understand that I will allude to RM2K a lot in all my JScript tutorials. What, you thought this was the only one? No way.

The syntax is as follows:
if(condition) {


condition is replaced with what you are testing, and Else is not necessary, as with Rm2K. { and } are just necessary for any code that uses more than one line(keep in mind that you can use just one line for all your code(but if you do, I'll kill you). The condition is generally in the format of: varName == someValue

== is an Identifier, remember to use this and not just =.

Writing is a relatively simple thing to understand, seeing as it can be easily done with one line of code.

var isRyuGod = false;

document.writeln("Ryu is god...or is he? Saying Ryu is god is a " + isRyuGod + " statement.");

this writes:

Ryu is god...or is he? Saying Ryu is god is a false statement.

Now, do you have all that down? Good, because I have a little homework for you to do.(and to do it, you should learn the prompt() function:

var theVar = prompt("Are you god?","");

That brings up a box asking you to fill in a text box in response to the question"Are you god?". Experiment with it.

here is a page with a small script. Look at it, and try to copy it without looking at the source.