Don't click here unless you want to be banned.

LSL Wiki : LSL101Chapter2

HomePage :: PageIndex :: RecentChanges :: RecentlyCommented :: UserSettings :: You are ia360925.us.archive.org
<Int - Fo - Ch1 - Ch 2 - Ch3 - Ch4 - Ch5 - Ch6 - App1 - App2>

Chapter 2: Syntax Revisited


Every coder has learned to dread these words:

Syntax Error


These words scare all matter of mice and men. They are the scourge of the internet, striking fear into the hearts of millions. And now, you must face them.



Actually, syntax errors in Second Life are very easy to avoid and fix, provided you know what you're doing. The following section will provide a handy review of tips for writing perfect code.


Braces and Parentheses ALWAYS need to be closed.

Think of it as virtual housekeeping. Just as in real life, once you open a door, you must remember to close it so things that go bump in the night don't come inside. Braces are there so that one line of code can refer to several lines of code (In essence, they say "This code belongs to that code.")
Take this code for example:
if (grassIsGreen) 
{
    llSay(0, "Woo-hoo!");
}
The opening brace states that "code after here, until my closing brace, belongs to this if-statement".

Braces are usually (but not required to be) spaced so that each set of braces are the same distance away from the left-margin, like this:
{ // Opening brace A
    { // Opening brace B
        { // Opening brace C
        
        } // Closing brace C
    } // Closing brace B
} // Closing brace A
The Second Life LSL editor automatically indents for you after you type an opening brace :)
Always remember to close all braces and parentheses that you open in your code. If you have three open braces, you must have three closing braces. Period.

For more information, see codeblock and brace.



Remember your semicolon!

Unless a line of code requires braces ({}), chances are it requires a semicolon (;) at the end of it. So put it there, soldier!



Sometimes braces can be omitted.

But these are rare exceptions. If you find a place that should have braces and doesn't in someone's code, look at it very carefully. Chances are there will be visible clues to why that's the case.



Math is very easy.

Arithmetic is very easy in LSL Here are some basic guides.
+ Addition
- Subtraction/Negative sign
/ Division
* Multiplication
% Modulus (Cross-Product)
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
llSin Sine
llCos Cosine
llTan Tangent
llAbs Absolute value
llRound Round to nearest whole value

For more, see math.

Note that some other operations exist. Occasionally, you will see stuff like "++," "/=," etc. I'll explain this later, so don't worry.


Variables must always be declared.

While we have not yet discussed variable declaration, you must declare variables before using them in every piece of code you write.



Double-slashes are comments.

// This is a comment. We use these to leave notes in our code.
// Comments are extremely useful for figuring out what a coder is doing.
// Likewise, they're helpful in letting others know what *you're* doing!

// All you need to write a comment are two slashes before a line.
// You can also "comment out" code if it's not working properly in this manner!



Not everything needs to be inside braces.

As you'll learn later, global variables and other things can be defined without any braces at all!



If it doesn't look right, Ask.

If all else fails, ask another coder. Several Scripting Mentors are available to help. You can also ask for help on the forums or contact me.

<Int - Fo - Ch1 - Ch 2 - Ch3 - Ch4 - Ch5 - Ch6 - App1 - App2>

Homepage | LSL101 | Tutorial | CrashCourse | Glossary
There is one comment on this page. [Display comments/form]