Host Interfaces - Paramecium13/LSN GitHub Wiki

Host interfaces are bridges between the game engine and LSN. Each instance of a host interface wraps an object in the game engine; methods called on the host interface instance in LSN are passed along to the object in the game engine. Additionally, the object in the game engine (or some other part of the game engine) can send information to LSN by a host interface's event (the implementation of this is left to the game engine). Because host interfaces are tightly coupled to parts of the game engine, the LSN host interface definitions should only be created and modified by or in tandem with the people who develop the corresponding part(s) of the game engine.

While LSN host interface definitions must be created and modified with caution, the definitions and accompanying documentation should be readily visible to scripters so they can write code that correctly interacts with the game engine.

Host Interface Definitions

In contrast to the complexities of properly connecting a host interface to the game engine, the definition of a host interface in LSN is refreshingly simple. A host interface definition consists of a name, zero or more method definitions, and zero or more event definitions. Below is the syntax for a host interface definition.

Host Interface MyHostInterfaceName
{
 
       // Method and Event definitions.

}

The keywords Host and Interface are not case sensitive (but the names of host interfaces and of their events and methods are) and can even be written as one word. The event and method definitions, contained between the curly braces, do not have to be in any sort of order.

Method Definitions

Method definitions in a host interface are the same as function definitions or script class method definitions except that they do not contain a body. This is because the game engine is responsible for implementing the methods. fn MyMethod(x : int, y : int) -> Vector;

The definition of a method consists of the keyword fn, followed by the name of the function, then a set of parenthesis. Inside the parenthesis are the method's parameters, with the name of the parameter being followed by a colon then by its type, and with the parameters being separated by commas (NOT comas). If the method does not have parameters, there should be nothing between the parenthesis. The return type is indicated after the parenthesis, with an arrow symbol, ->, then the return type's name. If there is no return type, the arrow symbol could be followed by a set of empty parenthesis or left out entirely. The definition is ended by a semicolon.

Event Definitions

Event definitions look similar to method definitions except event definitions use the keyword event instead of fn and events cannot have return types.

event DamagedByTrap(trapName : string, damage : double);

When the game engine says that an event has occurred on a host interface, the event listeners of all script objects listening to that event on that host interface will run...

Example

Here is an example host interface definition, with comments documenting the functions, methods, parameters, and return values.

// Represents an actor...
Host Interface IActor
{
    // Get's the actor's health.
    fn GetHP()-> int;
    
    /* Modifies the actor's health by 'amount'. Use positive numbers to heal
    and negative numbers to damage. Returns the actor's hp after the change. */
    fn ModHP(amount : int) -> int;
    
    // Kills the actor
    fn Kill();
    
    // This event fires when the actor dies.
    event Died();        
    
    /* This event fires when the actor is attacked. 'attacker' is the actor
    who attacked this actor, 'damage' is the amount of damage inflicted, 
    and 'fatal' is whether this attack kills the actor. Note: if this
    attack kills the actor, this event fires (and all of its listeners
    must finish) before the 'Died' event fires. */
    event Attacked(attacker : IActor, damage : int, fatal : bool);
}

Connecting A Host Interface to the Game Engine

...

⚠️ **GitHub.com Fallback** ⚠️