Patterns

Debug logs

Messages logged to the DEBUG channel are visible in the CET console by default (without having to enable game logs).
1
LogChannel(n"DEBUG", "something")
Copied!

General purpose hash map

There are several hash map implementations available in the game. Most importantly, inkHashMap (Uint64 -> ref<IScriptable>) and inkWeakHashMap (Uint64 -> wref<IScriptable>). All custom classes extend IScriptable therefore they can be used as values in those hash maps.
1
let map = new inkHashMap();
2
hashMap.Insert(TDBID.ToNumber(t"key1"), MyClass.Create(1));
3
hashMap.Insert(TDBID.ToNumber(t"key2"), MyClass.Create(2));
4
let value: ref<MyClass> = map.Get(TDBID.ToNumber(t"key1")) as MyClass;
Copied!

Safe downcasting

The as operator returns null when a dynamic cast fails. You can use it combined with IsDefined to perform safe downcasts.
1
let manager = employee as Manager;
2
if IsDefined(manager) {
3
// employee is known to be Manager here
4
}
Copied!

Heterogeneous array literals

If you define a function to accept an array of Variant:
1
func AcceptVariants(variants: array<Variant>) {}
Copied!
Then that function can be called with an array literal containing elements of different types:
1
// all elements get implicitly converted to Variant
2
AcceptVariants([1, new Vector2(1, 2), new inkText()]);
3
// you can achieve the same thing with an explicitly typed local
4
let variants: array<Variant> = [1, new Vector2(1, 2), new inkText()];
Copied!

Custom scriptable system (singleton pattern)

You can define a custom scriptable system:
1
module MyMod
2
3
public class MySystem extends ScriptableSystem {
4
private func OnAttach() -> Void {
5
LogChannel(n"DEBUG", "MySystem::OnAttach");
6
}
7
8
private func OnDetach() -> Void {
9
LogChannel(n"DEBUG", "MySystem::OnDetach");
10
}
11
12
public func GetData() -> Float {
13
return GetPlayer(this.GetGameInstance()).GetGunshotRange();
14
}
15
}
Copied!
This scriptable system can then be accessed using the scriptable system container:
1
let container = GameInstance.GetScriptableSystemsContainer(gameInstance);
2
// don't forget the namespace if you're using modules
3
let system = container.Get(n"MyMod.MySystem") as MySystem;
4
5
LogChannel(n"DEBUG", ToString(system.GetData()));
Copied!