Understanding the Example Script - dorian-K/Elafalter-ExampleMod GitHub Wiki

To fully explain what the Example Script does let me introduce some terminology:

Word Definition
Script A Script is the Code which makes Mods work. As we pack scripts into a single bundled file, we talk about scripts in the singular form even though they often consists of multiple files.
Mod A Mod (also called Module) is a single "unit" of code, designed to do a very specific action.Example: SpeedMod increases the Speed of the player Mods can be enabled and disabled by the user and can register Events, Hooks and their own config values
Event (Mod) Events are actions a mod can "subscribe" to, to react to things like being enabled or disabled. There are also events like tickWorld which are called on all active mods at the same time. We will learn more about events in greater detail later.
Hook (Mod) Hooks can be understood as traps for the Game's code. Example: Say you are interested in the function which looks up the Speed of the Player and decide to "trap" (hook) the function. Once the function is used in the game, our trap is triggered and instead of executing the original code, your code is ran instead. You can use this to your advantage and tell the game your own speed value. In practice, hooking can be a bit more difficult to pull off successfully, and you will learn more about it later.
Property (Mod) Property values are Mod-specific variables that are visible to the user and saved in a config file. They should be considered volatile as they can be changed by the user or another Mod at any time. Example: The SpeedMod has the configuration value "speed" to control the speed of the playerConfiguration Values can be a float (Real Number), integer (Whole Number) or boolean (True or False)
TypeScript TypeScript is a type-safe version of JavaScript which is ultimately compiled into Javascript code by webpack. Using TypeScript is not required (it is translated to vanilla Javascript anyways), but strongly recommended and used in both the Example Script and Core Mod

File Structure

In case you're not familiar with typical nodejs projects, let me quickly explain the files in the example Script.

package.json: Contains information about dependencies and build scripts, it is used by the npm command

package-lock.json: Contains the specific versions of all the dependencies of the project. (It locks the project in place)

tsconfig.json: Information about the project's version of javascript for the TypeScript transpiler.

webpack.config.js: Instructions for webpack on how to correctly bundle our Script into a single File.

The code of the Project resides in the src/ folder, which is what we are going to focus on next.

The Source Code

Open the src/ folder.

Inside you will find our main file, the index.ts file. It is written in TypeScript, as can be seen by the .ts file extension.

There should also be two more directories:

sdk/: This contains the SDK definitions for Elafalter, which may move into a nodejs library in the future. You can generate the index.d.ts file yourself by using the .dump command in Elafalter!

sub/: A subfolder to demonstrate the ability to import more files from the main file.

We will continue by inspecting our main file, index.ts.

From this point on I will assume that you have at least used javascript before and have had some experience with nodejs and typescript. If not, I highly recommend learning at least the basics of these before proceeding.

Using an IDE (i.e. Visual Studio Code with the relevant extensions installed) is also recommended, but not required.

In the first few lines, you will find import statements for a few SDK Objects (Game and ModManager) which you will learn more about in other pages, and an import for the script in the sub/ folder. If you have never seen this in javascript code, this feature is called Javascript Modules and allows you to split up your code into multiple files.

After that there are two types of declarations for Mods: ClassBasedMod and InlineMod. Both are functionally exactly the same, and it's up to you to choose which one to use. I personally prefer the "Inline" style, but you may consider the class based approach to be more organized.

After we have declared our two Mods, we register them to the ModManager using ModManager.registerMod(...);

When we register a Mod, the ModManager will call the constructor of the Mod and, after performing some checks, add the Mod to Elafalter's mod list.

There are only a few requirements for mods to register successfully:

  • Set a unique name once inside the constructor
  • A script can only register a limited number of mods (At the time of writing, this is capped at 100 Mods per Script, but may change in the future)