-=[ LPC Basics ]=-
Control statements:
LPC has control statements the same as C. These include;
if-else, switch, for-loop, while-loop, and the loop
controls break, and continue; and the function control
return.
-=[ Selection statements ]=-
LPC supports two selection statements: if, and switch.
if:
if(expression) {
statement1;
}
else {
statement2;
}
The conditional expression can be anything. C only
evaluates 'expression' as false if it is 0. LPC allows an
if-else-if ladder.
if(expression1) {
statement1;
}
else if(expression2) {
statement2;
}
else {
statement3;
}
Example:
/* write the players inventory, if this_player() is a
wiz, then show invisible objects */
#define WIZ 21
void inventory(object player) {
object *inv;
int i;
inv = all_inventory(player);
for(i = 0; i < sizeof(inv); i++) {
if(inv[i]->short()) {
write((string)inv[i]->short()+"\n");
}
else if(this_player()->query_level() >= WIZ) {
write("*"+file_name(inv[i])+"\n");
}
}
}
'Switch' used for selection:
switch(expression) {
case constant1:
statement1;
break;
case constant2:
statement2;
break;
case constant3:
statement3;
break;
default:
statementdefault;
}
Switch is a multi-branch selection statement. If there is
a match between the expression and 'constant'. It will
execute the statement. If there is a default it will do
the default statement if there are no other matches.
-=[ Iteration statements ]=-
The main iteration statements used are the while, and for
loops.
for:
for(initalization;condition;increment) {
statement;
}
while:
while(expression) {
statement;
}
Note: Unlike programmes compiled on your own machine, LPC
will only allow evaluations of about 50 loops. After this
you will get Too Long Evaluation errors. I think this was
design so that you didn't get infinite loops, or long
evaluations that lag, or crash the mud.
Jump statements:
return:
return expression;
return causes the function to jump out of the current
function (routine). It may or may not have an expression.
break:
break;
terminates the loop that the break is currently in.
continue:
continue;
continue forces the next iteration of a loop skipping
code that follows it.
Operators:
Operators allow processing of variables, and functions.
Operator Action
assignment:
= assign to rvalue, lvalue
arithmatic:
- subtraction, unary minus
+ addition, add strings
* multiplication
/ (integer) division
% modulus division
-- decrement
++ increment
-= x -= 5; is equivelant to x = x - 5;
+= x += 5; is equivalent to x = x + 5;
relational:
> greater than
>= greater than or equal to
< less than
<= less than or equal to
== equal to
!= not equal to
logical:
&& AND
|| OR
! NOT
ternary:
variable = (expr1) ? expr2 : expr3;
is equivalent to,
if(expr1)
variable = expr2;
else
variable = expr3;
Example:
void set_msgin(string str) { msgin = (str) ? str
: "arrives";
}
Misc:
-> arrow, "object -> fn()" is equivalent to
"call_other(object,"fn")"
[] square bracket, s[4], 5th element of array s
:: scope symbol, call the function directly from the
inherited object.
Example:
void init() {
::init();
add_action("jump","jump");
}