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.
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 handlersOnBombExploded
andOnBombSolved
. Remove this field and theKMBombInfo
component if your module does not use any of this information. -
_audio
provides an interface to the game's audio system, with methods likePlaySoundAtTransform
. Remove this field and theKMAudio
component if your module does not play sounds, or plays them without using the game's system, such as by using Unity'sAudioSource
component. -
_module
is used to tell the game about the module (we set theModule Type
,Module Display Name
, andRequiresTimerVisibility
fields in the inspector), and to communicate to the game when the module should solve or strike. It has theOnActivate
, which is called when the lights turn on. TheKMBombModule
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.
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;
}
For information about the Twitch Plays part of the template, see the Twitch Plays support section, and see the Twitch Plays Documentation.