Script Classes - Paramecium13/LSN GitHub Wiki

The script object--host interface system is the key abstraction provided by LSN. It allows code in the game engine to cause LSN code to execute and LSN code to execute specified code in the game engine.

A script object is an instance of a script class.

A script class can use the keyword this to refer to the script object that the calling code 'belongs' to. This allows it to pass itself to functions and to methods on other script objects.

A script class can be marked as unique, meaning that there can only ever be one instance (script object) of it in the game. This instance is typically created when a new game is started (or, the first time it is requested in a new game) and is saved into and loaded from the save file. The instance of a unique script class can be referenced in any piece of code by using its type name. Unique script classes are very useful for implementing something there is only one of in a game, such as a specific quest.

Host

Script objects can have a host, which is a host interface (its type is a host interface type). The script class's host can be referenced in its methods and event listeners through the host keyword. This can be done to pass it to functions or methods or to call its methods. The reifier will not allow a script class without a host to do use the host keyword.

Fields

Fields have a name, a type, and a value. Fields can be marked as mutable with the mut keyword, otherwise they are immutable. Fields can only be accessed by their containing script object...

Methods

Methods are like functions but 'belong' to an object. For a piece of code to call a method of a script object, that code needs a reference to a script object of that type. Methods can be marked as 'abstract' or 'virtual'. An abstract method is not implemented in the main body of the script class but it must be implemented by each one of the script object's states. A virtual method is implemented in the script class's main body but its states have the option to override it.

Event Listeners

Event listeners are another type of member in script classes. They look similar to methods in that they have a name, parameters, and code. However, unlike methods they are declared with the keyword on, they cannot have a return type, and they cannot be directly called. Additionally, each event listener's signature (i.e. name and parameters) must match the signature of an event declared in its script class's host. Therefore, script classes without a host cannot have event listeners.

An event listener's code is executed when its object's host fires the event it is listening to.

States

... ... Currently, states can only contain event listeners, overrides of virtual functions, and implementations of abstract functions.