Search the Commands

Home  All  a  b  c  d  e  f  g  h  i  j  k  l  m  n  o  p  q  r  s  t  u  v  w  x  y  z 

Details for command if:
 
* if (<condition>) <statement>
 
This is the basic conditional statement command, and just about the only one
available in this scripting language.

The condition can be any expression. All expressions resulting in a non-zero
value will be considered True, including negative values. All expressions
resulting in a zero are false.

If the expression results in True, the statement will be executed. If it isn't
true, nothing happens and we move on to the next line of the script.

if (1) mes "This will always print.";
if (0) mes "And this will never print.";
if (5) mes "This will also always print.";
if (-1) mes "Funny as it is, this will also print just fine.";

For more information on conditional operators see the operators section above.
Anything that is returned by a function can be used in a condition check without
bothering to store it in a specific variable:

if (strcharinfo(0)=="Daniel Jackson") mes "It is true, you are Daniel!";

More examples of using the 'if' command in the real world:

Example 1:

set @var1,1;
input @var2;
if(@var1==@var2) goto L_Same;
mes "Sorry that is wrong";
close;
L_Same:
close;

Example 2:

set @var1,1;
input @var2;
if(@var1!=@var2) mes "Sorry that is wrong";
close;

(Notice examples 1 and 2 have the same effect.)

Example 3:

set @var1,@var1+1;
mes "[Forgetfull Man]";
if (@var==1) mes "This is the first time you have talked to me";
if (@var==2) mes "This is the second time you have talked to me";
if (@var==3) mes "This is the third time you have talked to me";
if (@var==4) mes "This is the forth time you have talked to me, but I think I am getting amnesia, I have forgoten about you";
if (@var==4) set @var,0;
close;

Example 4:

mes "[Quest Person]";
if(countitem(512)>=1) goto L_GiveApple;
// The number 512 was found from item_db, it is the item number for the Apple.
mes "Can you please bring me an apple?";
close;
L_GiveApple:
mes "Oh an apple, I didnt want it, I just wanted to see one";
close;

Example 5:

mes "[Person Checker]";
if($name$!=null) goto L_Check;
mes "Please tell me someones name";
next;
input $name$;
set $name2$,strcharinfo(0);
mes "[Person Checker]";
mes "Thank you";
L_Check:
if($name$==strcharinfo(0) ) goto L_SameName;
mes "[Person Checker]";
mes "You are not the person that " +$name2$+ " mentioned";
L_End:
set $name$,null;
set $name2$,null;
close;
L_SameName:
mes "[Person Checker]";
mes "You are the person that " +$name2$+ " just mentioned";
mes "nice to meet you";
goto L_End;

See 'strcharinfo' for explanation of what this function does.

Example 6: Using complex conditions.

mes "[Multi Checker]";
if( (@queststarted==1) && (countitem(512)>=5) ) goto L_MultiCheck;
// Only if the quest has been started AND You have 5 apples will it goto "L_MultiCheck"
mes "Please get me 5 apples";
set @queststarted,1;
close;
L_MultiCheck:
mes "[Multi Checker]";
mes "Well done you have started the quest of got me 5 apples";
mes "Thank you";
set @queststarted,0;
delitem 512,5;
close;

With the Advanced scripting engine, we got nested if's. That is:

if (<condition>)
dothis;
else
dothat;

If the condition doesn't meet, it'll do the action following the else.
We can also group several actions depending on a condition, the following way:

if (<condition)
{
dothis1;
dothis2;
dothis3;
} else {
dothat1;
dothat2;
dothat3;
dothat4;
}

Remember that if you plan to do several actions upon the condition being false, and
you forget to use the curlies (the { } ), the second action will be executed regardless
the output of the condition, unless of course, you stop the execution of the script if the
condition is true (that is, in the first grouping using a return; , and end; or a close; )

Also, you can have multiple conditions nested or chained, and don't worry about limits as to
how many nested if you can have, there is no spoon ;)

...
if (<condition 1>)
dothis;
else if (<condition 2>)
{
dotheother;
do that;
end;
} else
do this;
...

Valid XHTML 1.0 Strict Valid CSS!