Import
With the import keyword, it’s possible to import a script from another file or another mod. This allows you to organize and structure your code in different folders and files.
MathHelper.luna
static class MathHelper {
static function Add(a, b) {
return a + b
}
}
Main.luna
import MathHelper
print(MathHelper.Add(5, 10)) // 15
Alias: as
It’s strongly recommended to always use an as alias when importing scripts because it avoids naming collisions between imports.
MathHelper.luna
static class MathHelper {
static function Add(a, b) {
return a + b
}
}
AnotherHelper.luna
static class MathHelper {
static function Remove(a, b) {
return a - b
}
}
Main.luna
import MathHelper
import AnotherHelper
MathHelper.Add(5, 10) // Error, as MathHelper is defined twice
The correct way of doing it:
Main.luna
import MathHelper as Helper
import AnotherHelper as ExtraHelper
Helper.MathHelper.Add(5, 10) // Uses the Helper.luna its MathHelper
ExtraHelper.MathHelper.Remove(5, 10) // Uses the AnotherHelper.luna its MathHelper
Folders
If the script you want to import is part of another folder, you will need to include the folder name based on from root folder of your mod.
Helpers/MathHelper.luna
static class MathHelper {
static function Add(a, b) {
return a + b
}
}
Core/Calculations.luna
import Helpers/MathHelper
print(MathHelper.Add(5, 10)) // 15
Importing Scripts from other mods
A script from another mod can also be imported. This allows mods to act as modules where their code can be reused within many other mods. This is done with the following syntax MODNAME::SCRIPT
MODNAMEis the name of the modSCRIPTis the name of the script that should be imported
Let’s say there’s a mod called NumberColors which has this script NumberColor.luna
static class NumberColor {
private static Negative = new Color("ff8b00");
private static Positive = new Color("ff556b");
private static Neutral = new Color("eeeeee");
static function GetAbsoluteColor(input: number): Color {
if (input == 0) return Neutral;
if (input > 0) return Positive;
return Negative;
}
}
First you’d need to add the NumberColors mod as a dependency as described [here]. Now that the NumberColors mod is a dependency, we can use it in our own code
import NumberColors::NumberColor as NumberColor
print(NumberColor.GetAbsoluteColor(5)) // ffff6b