[Main page] [Overview]   /lpc /BASIC /chapter1
LPC Basics
Written by Descartes of Borg
23 april 1993
CHAPTER 1
The Basic LPC Program
The title of this first chapter is actually poorly named, since one does
not write programs in LPC. As an LPC coder, you instead write *objects*.
What is the difference? Well, for our purposes now, the difference is
in the way a program file is executed as opposed to an object file.
A program file or object file is simply any single file with code in it.
In a program, such as in BASIC programs or C programs, there is a
definite beginning point and ending point for the execution of the program.
Take, for example, the following two programs:
C | BASIC
void main() { | PRINT "Hello mud."
printf("Hello mud.\n"); | END
} |
In BASIC, code is interpreted (except in some newer versions), this means
that the code is being translated into the 0's and 1's the computer
understands as the program is being executed. C, on the other hand,
is a compiled language, meaning that before you run the program, the code
you wrote is turned into language the computer can understand. Obviously,
the advantage of a compiled language is that it runs faster since no
translation is occuring as the program is executed.
When a BASIC program is run, execution of the program begins at the first
line and continues in order from that point onward (except where it
is specifically told to go somewhere else by GOTO, GOSUB, etc). In
C, execution of a program begins with the *function* called main() and
then proceeds in order through the function main() until it either
reached the end of that function, reaches a return instruction, or
reaches a call to exit(). main() may (and most often does) make calls
to other functions in the program.
As you see, programs have a place where they clearly begin when run and
end and are done with until run again. In MUDs however, the program
being run is a C program called the driver. All LPC files created are
objects which are used by the driver during the course of its execution.
A simple LPC room might look something like this:
void create() {
set_property("light", 2);
set("short", "Descartes' simple workroom");
set("long", "You do feel that Descartes makes dull workrooms.\n");
set_exits( ({ "d/standard/square" }), ({ "square" }) );
}
Like main() in C, create() in some drivers or reset() in others is where
execution of any object initially begins. But that is the only similarity.
Neither create() nor reset() need exist in any object. In C, main()
is used to get the program running. With LPC objects however, they do
not "run". What happens is that functions in them get called.
For example, I might want to know for a peer command what the long
description of a room east of me is. The room east of me has not yet
been used. When the command "peer east" is issued by a player,
the code for the command asks the room object to the east what its long
description is. Since it has not been loaded yet into memory (i.e. the
driver does not know it exists), it first must be loaded into memory.
The room the player is in asks for the long of the room, the driver
realizes that there is no such object in the game, it looks for the file
of the object the previous room wants. It finds it and loads it into
memory. At this point, we simply have a blank room. To give creators
an opportunity to set starting values for things in the room, the driver
will at this time call the create() (or reset() in compat mode) function.
Once the driver now is aware of the room object, it can check and see
what the long description of the room is by calling the function
query_long() (long() in compat mode) in the object. It then passes this
information on back to the original room. Note that if the room had
been used before (and therefore was in the driver's memory), it simply
would have called query_long() in the room and returned that value to the
first room. No call to create() would have been made (or necessary).
So, in LPC, create() and reset() are used to initialize each object.
Objects that need no initialization do not need such a function. And
similarly, the object is not done with once create() (or reset()) are
finished being executed.
Now we have reached the point of what this chapter is all about. What
consists a complete LPC object? Well, an LPC object is simply one or
more functions grouped together in a single file. The order in which
the functions are placed relative to one another is irrelevant. In other
words:
void init() { add_action("smile", "smile"); }
void create() { return; }
int smile(string str) { return 0; }
IS EXACTLY THE SAME AS:
void create() { return; }
int smile(string str) { return 0; }
void init() { add_action("smile", "smile"); }
ALSO THE OBJECT:
void nonsense() {}
i a valid object, but probably would not interact properly with other
objects on your mud (it would be invisible, weightless, etc.)
CHAPTER SUMMARY:
There is no beginning point or ending point of LPC code, as LPC is used
in creating objects, not programs. LPC objects consist of one or more
functions whose order in the code is irrelevant. These functions are
used by the driver and other objects in the game to allow the object
to interact with other objects in the game.
A NOTE ON THE FUNCTIONS reset() AND create():
As its name implies, the function reset() is used to reset the room.
In all native muds, this is the sole function of reset(). By resetting
the room, I mean that every so often, the driver calls this function
in objects so that certain things like regenerating monsters, resetting
doors, etc. can happen.
In compat muds or LPMuds of a release earlier than 3.0, reset() also
acts in the same manner as the create() function. In other words,
on creation
In native muds:
the driver calls create()
In compat muds:
the driver calls reset() (It also passes an argument of 0, but that
will be discussed when we get to functions)
And at reset time:
In native muds:
the driver calls reset()
In compat muds:
the driver calls reset() (Here the reset number is passed)
Since an argument is passed to reset() in compat muds, you can tell the
difference between when the function is being used in the sense of create()
and when it is being used as reset(). But this all is for later
discussion.