Phantasmal MUD Lib for DGD
|
Phantasmal Site > DGD > Untitled document (index.base.html) > (untitled) 3.2 Basic LPCLPC is similar to the language C, although some differences exist. Experienced coders will find it is basically a simplified C with some new convenient types and new functions. Some inconsistancies exist but, as with C, they're not a problem if you're aware of them. CommentsComments may seem like an odd thing to start with, but they're everywhere so you need to be able to recognize them from the very start. They're also important, so you need to know how to write them from the very start. In some LPC dialects, there are two kinds of comments: <code> // This is a comment stretching to the end of the line. // NOT SUPPORTED BY DGD! <code> /* This is an enclosed comment */ <more code>The first type of comment starts off with the //
characters and then stretches all the way to the end of the line.
If you want more lines of comments, you'll have to start off those
as well with new // characters. This is like C++
single-line comments, and DGD does not support them!
The second type has a definite length. Those comments start with
Please note that the /* A comment /* A nested comment */ the first continues */What will happen is that the comment will end with the first */ found, leaving the text the first continues
*/ to be interpreted as if it was LPC code. That's not valid
LPC code, so instead you'll get an error.
Data TypesAn object holds information in variables. Variables are a sort of virtual container that holds information. It's called a variable because the information is allowed to change later. Most objects process information with functions. Functions can use and return data of various kinds. In principle only one kind of data type is needed, a sort of general container that would hold anything you wanted it to. LPC calls this type 'mixed'. Usually, though, it's much more useful if you can distinguish between different types of information. Knowing what's in a variable can be a very good thing. It greatly improves on the time it takes to write and debug an object. In LPC it is possible to use only data of type 'mixed'. In the first versions of the language, that was the only data type available. With modern LPC, however, it's better to avoid mixed variables when you can. LPC lets you declare variables of these types:
There's also a special value called If you need to know the limits of integers, characters or floating-point numbers, you can check DGD's include/limits.h and include/float.h files. They list limits of the various data types. Bear in mind that DGD can easily be compiled with different integer and floating point limits, so it's good to make your code check the sizes. They may be different next time your program runs! Variable declarationsA variable is a string of letters identifying an information container, a place to store data. The container is given a name consisting of 32 characters or less, starting with a letter. No special character other than the '_' used to separate words is ever used. Variables should always be given names that reflect how they are used. You declare variables like this: <data type> <variable name>, <another variable>, ..., <last variable>; e.g. int counter; float height, weight; mapping age_map; Variables must be declared at the beginning of a block,
immediately after the first Variables are initially set to 0 or to nil, and not necessarily
to the obvious 'empty' values. Mappings, arrays and strings will
all be set to nil and not to Arrays and mappings should be initalized to their empty values
( Function declarationsA function's declaration must state what kind of data type it
returns, if any. A function name is a label much like a variable
name, consisting of 32 characters or less and starting with a
letter. No special characters other than /* * Function name: <Function name> * Description: <What it does > * Arguments: * Returns: <What the function returns> */ <return type> <function name>(<argument list>) { <code expressions> }The beginning comment, while optional, is highly recommended. Even if you don't use precisely this form of comment, you should state what the function does and what it expects to be given as input. Here is an example function: /* * Function name: compute_diam * Description: Compute the diameter of a circle given the * circumference. * Variables: circumference - the circle's circumference * pi - a famous irrational constant * Returns: The circle's diameter */ float compute_diam(float circumference, float pi) { float rval; /* Circumference = pi * diameter, so diameter = circumference / pi */ rval = circumference / pi; return rval; } The argument list is a comma-separated list of data types. It specifies what kinds of data will be sent to the function and assigns names to this data for later use. The data recieved will only be usable inside the function, unless you explicitly send it elsewhere. The function's argument names are valid only within that function itself. (In order to save space and improve on legibility in the text, I won't put a header on every example function). A function that doesn't return anything should be declared as
void write_stuff(string mess) { write_file("/usr/Bob/myfile.txt", mess); } Statements and ExpressionsWe need to define what a statement and what an expression are in order to proceed. Statements
A statement is sort of a full sentence of instructions, made up
from one or more expressions. Statements usually cover no more than
a single line of code. Sometimes it's necessary to break it up
though if it becomes too long simply to improve on legibility -- a
little like avoiding long run-on sentences. For most statements you
simply break the line between two words, but if you are in the
middle of a string you need to add a backslash ( DRIVER->message("This is an example of \ a broken string.\n"); However, breaking a statement with backslash is extremely ugly
and makes the code hard to read. It's usually possible to break the
line naturally at the end of a string, or between two operators of
some kind, or even just split the string in half and add the two
parts together with the DRIVER->message("This is a better way of " + "breaking a string.\n"); Statements in LPC almost always end with ExpressionsAn expression is an instruction or set of instructions that
results in a value. A variable is an expression since it yields its
contents as a result. Function calls are valid expressions. They are written simply as
the name followed by a set of parentheses with the arguments that
the functions uses listed inside. Take the simple function
The block statement There are a lot of statements
such as conditional statements that in certain circumstances
execute only one specified statement and no more. Suppose
you want to have several statements executed and not just one?
Well, there's a special statement called block statement
that will allow you to do that. A block is defined as starting with
a if(my_var < 3) { statement_one(); statement_two(); statement_three(); } The ';' statementAs stated The |