[Main page] [Overview]   /lpc /BASIC /chapter5
LPC Basics
Written by Descartes of Borg
24 april 1993
CHAPTER 5
Variable Handling
By now you should be able to code basic objects by inheriting standard
mudlib objects and making certain function calls. You should understand
that inheritance allows you to use functions defined in other objects
without having to redefine them yourself. You should also know what
it takes to declare your own functions. This chapter will teach you
about the basic elements of LPC which will allow you to define your
own linearly executed functions (meaning executed in the order written).
Basically, any sort of programming comes down to the manipulation of
variables. You use LPC instructions to somehow make checks on and
change variable values, then act upon the results of value checks
From the chapter on data types, you should know that you must declare
any variable before you make use of it. In LPC, you can use any
sequence of alphanumeric characters to name variable as well as characters
like _ and -. LPC is case sensitive, so this is different from THIS or
This.
Like in most programming languages, LPC variables may either be global
or local. Global variables are ones which are availble to any function
in the scope of the object in which it is declared. Scope means simply
whether or not you allow objects which inherit your object to use the
variable. Since we are mostly dealing with final products here, the
scope of an LPC global variable is all functions in the file after its
point of declaration.
Local variables, however, are available only to the function in which
they are declared. Note the following two bits of code:
int x;
int query_x() { return x; }
void set_x(int y) { x = y; }
--- cut here ---
void set_x(int y) {
int x;
x = y;
write("x set to "+x+"\n");
}
--- cut here ---
Note that in the first example, x is declared outside of any functions,
and is therefore available to all functions which follow its declaration.
In that example, x is global. In the second example, x is declared inside
the function set_x(), and is thus local to that function. If you tried
putting in the query_x() function like it is written in the first example,
you would get an error when you tried loading the file into memory
(the example would read something like "Undefined identifier x in line...".
or something similar).
You should only use global variables sparingly. A global variable
resides in memory the whole time the object is loaded. Local
variables, on the other hand, are erased from memory when the function is
no longer being executed. Thus, you should only have as global
variables things the driver needs to know at all times. Everything else
should be local.
Now that you know how to declare variables and where to declare them,
you need to know how to assign and change the values of those variables.
Of course, unless your mind has been warped by Pascal, the statement:
x = 5;
is probably self explanatory. It assigns the value 5 to the variable
x. Soemthing that is not quite so self evident, however, is that
x=5 itself is an expression what has a value
The following expression:
x = (x=5)+1;
actually makes sense. First, the expression in pararentheses is
evaluated, setting the value of x to 5. But the expression x = 5 itself
has a value of 5, which is set inside another expression which adds 1 to
that value. So the value of (x+5)+1 is 6. x in the end is therefore 6.
This may sound silly to be explaining, but there is reason for it all.
In LPC, = is an assignment operator. It's value is the value assigned.
But if you wish to compare two values without assigning anything to either,
you need to use the == operator.
The expression (x=5) is equal to 5.
The expression (x==5) has a value of 1 if x is equal to 5, 0 otherwise.
In LPC then you have:
= += -=
all of which assign a new value.
x += 5; assigns x its current value plus 5. It is exactly the same as
x = x+5;
++ and -- are incremental assignment operators, meaning they are the
following:
x++;
is the same as:
x += 1;
or x = x + 1;
If you want to evaluate the value of an incremental expression, remeber
the following:
x = 5;
(x++) has a value of 5
(++x) has a value of 6
SUMMARY:
Now you know how to declare variables and set and change their values.
Once familiar with your driver's efuns, you can use them to display
your variables and do other such things. What still has not been
covered is how to evaluate the values the variables hold and
alter the flow of the execution of the function based on those values
(for example, you may want some lines to execute if x equals 5, but
not executed otherwise). The special function init() also needs
to be dealt with so that you can give players new commands they may
issue in order to get functions in your objects called.