LDMud logo: an emblem with three snakes, on the left behind that emblem a cogwheel. In front of both is a sword.

LDMud

Home of LDMud

View the Project on GitHub ldmud/ldmud

Functions

Functions are code blocks that have a name. Every LPC code that wants to be executed must be somewhere within a function. (There is just one exception: the initialization of global variables.)

A function has a name, a return type, arguments (i.e. values given to the function when called) and its code block:

    int get_square(int x)
    {
        return x*x;
    }

In this example the first int denotes the return type. It's followed by the function name and the arguments in parenthesis. After that comes the code block in braces.

To call a function within the object just write the name and the values for its arguments in parenthesis,

    x = get_square(42);

To call a function in another object there is an efun call_other which can be abbreviated with ->.

    x = ob->get_square(42);
    x = call_other(ob, "get_square", 42);

Modifiers

Functions and global variables can have modifiers to change the behaviour of the function resp. variable. The modifier is written just before the return type.

private
This function can only be called from within this program (this file and included files). Calls from other objects or inheriting programs will ignore this function. For global variables this means that inheriting programs can't access the variable.
protected
This function can only be called from within this object. Calls via call_other (from this object or another object) will ignore this function.
static
This function can only be called from within this object, even via call_other. Calls from other objects (via call_other) will ignore this function.
public
The function can be called from within this object or from other objects. This is the default for functions. For global variables it prevents changing the visibility to private or static by inheritance. The variable will stay visible in inheriting programs.
nomask
This function cannot be overriden by inheriting objects. Nor can shadows shadow this function.
deprecated
Issue a warning whenever this function is called or the variable is used.
varargs
When calling this function from within the object arguments can be ommitted. Ommitted arguments will be set to 0. Please note that calls via call_other are always treated as varargs calls.
nosave
Declare that this variable should not be stored with save_object or restored by restore_object

Asynchronous Functions

Functions can be declared with async to be asynchronous. This means the function can suspend its execution at any time and be resumed later on. At each suspension a value can be passed in and/or out.
    async void fun()
    {
        // Do something.

        int a = yield(); // Suspend and get a value from outside.

        // Do something else.

        int b = yield(); // Suspend and get another value.

        return a+b;
    }
To execute such a function a coroutine object must be created. This is similar to calling the function but will not start execution immediately, but only create the coroutine object. With a call to call_coroutine or using await, the execution can be started/continued.
    coroutine cr = fun();

    call_coroutine(cr);                 // Initial start
    call_coroutine(cr, 1);              // Pass 1 into the coroutine
    int result = call_coroutine(cr, 2); // Pass 2 into the coroutine
Coroutines can call other coroutines either with call_coroutine or await. The former suspends execution only till the first suspension of the called coroutine, the later suspends execution until the called coroutine has ended.
    async void fun()
    {
        foreach(int i: 10)
        {
            printf("Counting: %d\n", i);

            // Assuming there is a sleep() coroutine.
            await(sleep(1));
        }
    }