DaVince scripting tutorial/Advanced functionality in functions

From Spheriki

Jump to: navigation, search

Contents

Advanced functionality in conditions

There's much more that you can do with if-statements than you have learnt so far. For example, you can put several conditions on your if-statement at once; you can group related if-statements together for efficiency and readability; you can even leave out the braces around the code to execute in some cases. This chapter will explain you why and how.

Combining several conditions: && and ||

You can combine several conditions - that is, comparisons - to make an overall more complicated if clause. More complicated, but also more precise. For example, (value > 0) can only check if your value is higher than 0, but combined with a (value < 10), we suddenly only have the possibility to be the numbers 1 to 9 to have a condition evaluate to true! This can be very useful, and sometimes even required, to get the desired effect.

The inefficient way
The inefficient way is nesting a condition inside another one.

if (value > 0)
{
  if (value < 10)
  {
    TextBox("The value is somewhere in-between 0 and 10.");
  }
}

You're running two conditions, here. But you're also running two if clauses. This takes up a tiny bit more processing power. The computer gets a tiny bit more to do, because it has to do this:

  • Compare a value,
  • then run the code inside,
  • which again compares a value,
  • then runs the code inside that,
  • and then Sphere has to "end" that block of code.
  • And then another one, because we had two conditions.

This can surely be done more efficiently! Yup, it sure can:

The efficient way

if (value > 0 && value < 10)
{
  TextBox("The value is somewhere in-between 0 and 10.");
}

What Sphere does now:

  • Compare a value,
  • and a second one,
  • evaluate whether the total result is true.
  • If so, run the code inside,
  • and then "end" that block of code.

Actually, I think it might be less steps, because computers have special ways to compare multiple grouped conditions efficiently. Still, one step less than the previous thing with my explanation. The code complexity has decreased, and you have saved some precious processor power, which can then be used towards more useful things, like advanced AI or whatever.

Next to the &&, which is called the AND operator, there is the OR operator. This special operator will evaluate the entire result as true if any of the given conditions is true, no matter if all others are false. This can be useful if you want some common code to run under only some circumstances:

if (cheat_mode || backflip_mode || the_player_kicks_ass)  //If even just one of these three equals true...
{
  DoABackflip();
}

The player might do a backflip if any of the three variables given are true. Doesn't matter if only one of them is true, or all three... As long as something is true, it'll run the code inside the block.


The two types of operators can be combined. You could check if all elements in a set of conditions is true, and then compare that to another conditions that might override it and run the condition anyway. It's best to group these together by using extra parentheses. Example:

if ((master_password_entered && voice_recognition_success && confirmed_twice) || evil_intruder_override)
{
  SelfDestruct(ship);
}

The ship will ONLY self-destruct if the master password was entered, the voice recognition succeeded AND the user confirmed the self-destruct sequence twice. Only then. Oh, or if there's an evil intruder who somehow overrode the system.

Of course, even if you don't use such a complicated combination of options, it's still a lot more convenient than putting a condition alongside a condition inside a condition inside a condition.

Note that it also works with loops.

while (!done && keep_going)  //When done is false AND keep_going is true...
{
  DontGiveUp();
}


Leaving out the braces

This is another exception to the syntax rules in JavaScript, and sometimes a rather handy one: you can choose to leave out the braces. However, you can do this in only ONE case: when there's only one statement to make. The if-statement immediately closes after you put that finishing semicolon after your only line of code that's going to be run in the condition:

if (condition)
  Statement();
if (next_condition)
  OtherStatement();

Of course, you can still format this in any way you like. This can make code much more compact:

if (story == 1) Statement();
if (story == 2) SecondPart();
if (story == 3) ThirdPart();
if (story >= 4) Abort("End of the game!");


Another exception exists where you can use several statements if you like. However, this method is less compatible; a few JavaScript keywords like return and break will NOT work if you do this:

if (condition)
  Statement1(),  //Use a comma, not a semicolon, and you can keep feeding the thing code until a semicolon ; is encountered!
  story++,
  Statement2();  //End of the code to run for this condition.

Note that it also works with loops.

while (keep_going)
  DontGiveUp();
for (var i = 0; i < 1000; i++)
  TextBox("The loop has run " + i + " times now. WILL IT EVER END? Yes, at 999.");


The "else" keyword

You can connect if-statements together if you like. What if there's three related cases? Maybe var story is 1... Maybe it's 2. Maybe it's neither! You can use the else keyword to connect these three different conditions together. This offers two advantages:

  • To people reading the code, it's recognizable as a group of related statements;
  • When one condition evaluates as true, all others will automatically skip, not even bothering to evaluate. This saves processing power (your PC needs to do less).

There are two types of ways to use else:

  • else if - there's a different, related condition. For example, story is 2.
  • Just else - All of the previous related conditions have evaluated to false. For example, story is 6 and there's no else if that checks for it to be 6.

Basically, using just else is like defining code that will run in all other cases. else if can only exist right after an if or another else if. else can only exist once, and that is after all other related conditions.

var story = (any value)
 
if (story == 0)
  RunStorySectionOne();
else if (story == 1)
  RunSecondSection();
else if (story >= 2 && story <= 4)  //If story is 2, 3 or 4
  RunThirdSection();
else    //Story could be 5, or 389, or -300 for all we know
  Abort("That story section does not exist, fool!");
Personal tools