The main script - mariusz-tang/KTaNE-Module-Template GitHub Wiki

The main script contains many useful comments indicating how to use the different methods, as well as links to ModKit and Twitch Plays documentation. I will briefly cover some extra things here.

Fields

There are six fields already declared in the script:

// ! Remove KMBombInfo and KMAudio if not used.
private KMBombInfo _bombInfo;
private KMAudio _audio;
private KMBombModule _module;

// * _isSolved should be kept even if not needed, to help with future souvenir implementation.
private static int s_moduleCount;
private int _moduleId;
private bool _isSolved;

The first three are set to the KMBombInfo, KMAudio, and KMBombModule components on the module prefab, respectively:

  • _bombInfo can be used to query information about the bomb containing the module, such as the edgework, starting time, time left, and the number of strikes. It also has the event handlers OnBombExploded and OnBombSolved. Remove this field and the KMBombInfo component if your module does not use any of this information.
  • _audio provides an interface to the game's audio system, with methods like PlaySoundAtTransform. Remove this field and the KMAudio component if your module does not play sounds, or plays them without using the game's system, such as by using Unity's AudioSource component.
  • _module is used to tell the game about the module (we set the Module Type, Module Display Name, and RequiresTimerVisibility fields in the inspector), and to communicate to the game when the module should solve or strike. It has the OnActivate, which is called when the lights turn on. The KMBombModule component is required for the game to recognize the module as a module.

For more information, see the Community ModKit Documentation.

In addition to these, s_moduleCount and _moduleId are used to differentiate between instances of the module in log files when multiple appear on a bomb or in a session, and _isSolved is used to keep track of the module's solve state. This is useful for disabling buttons after a solve, and it's helpful for future Souvenir implementation.

Methods

The script contains several methods, many of which are empty. You should remove any methods you do not need.

The following methods are called directly by Unity:

// * Awake is called before anything else.
private void Awake() {
    _moduleId = s_moduleCount++;

    _module = GetComponent<KMBombModule>();
    _bombInfo = GetComponent<KMBombInfo>();
    _audio = GetComponent<KMAudio>();

    // * Declare other references here if needed.
}

// * Start is called after Awake has been called on all components in the scene, but before anything else.
// ! Things like querying edgework need to be done after Awake is called, eg. subscribing to ModuleButton.Selectable.OnInteract.
private void Start() { }

// * Update is called every frame. I don't typically use Update in the main script.
// ! Do not do resource-intensive tasks here, they will be called every frame and can slow the game down.
private void Update() { }

// * OnDestroy is called when the module is 'destroyed'.
// * Examples of when this happens include when the bomb explodes, and if the player quits to the office.
// * Sometimes, it helps to run code at these times, for example to stop looping sounds.
private void OnDestroy() { }

There are other game-related events which you may want to define custom handlers for:

// * KMBombModule.OnActivate is called once the lights turn on.
private void Activate() { }

// * These are quite self-explanatory.
private void OnBombExploded() { }
private void OnBombSolved() { }

Subscribe to the events in Awake, for example:

private void Awake() {
    _moduleId = s_moduleCount++;

    _module = GetComponent<KMBombModule>();
    _bombInfo = GetComponent<KMBombInfo>();
    _audio = GetComponent<KMAudio>();

    _module.OnActivate += Activate;
    _bombInfo.OnBombExploded += OnBombExploded;
    _bombInfo.OnBombSolved += OnBombSolved;
}

Twitch Plays

For information about the Twitch Plays part of the template, see the Twitch Plays support section, and see the Twitch Plays Documentation.

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