Skip to content
Emik edited this page Jun 1, 2021 · 24 revisions

Greetings!


Welcome to the Wiki! The following articles should teach you most of the important things that this library offers.

To use the code from the library, you need to access the namespace from your class:

using KeepCoding;

The chapters are split based on the category:

  1. Introduces all MonoBehaviour scripts.
  2. Introduces all instanced classes.
  3. Introduces all static classes.

Before diving in, remember that the XML documentation provides more specific detail to each method and property, so be sure to read those!

General Overview


You can access it by typing either class name and then CTRL+LEFT CLICK on the class file to go to its metadata. You can click on the plus icon to view more information about each method.

Using this library, there are 2 types of scripts you'll be making as part of the module-creation process. Both classes that you are deriving from are still MonoBehaviours, which means that both script files should be added as components to your module.

ModuleScript


The first one is the module itself, which will be deriving the ModuleScript class.

using KeepCodingAndNobodyExplodes;

public class ExampleModuleScript : ModuleScript
{
    // This is where all of your code relating to the module goes, this includes handling logging, strikes/solves, and so on.
}

TPScript


The second one is for Twitch Plays support, which will be deriving the TPScript class.

using KeepCodingAndNobodyExplodes;

public class ExampleModuleTPScript : TPScript<ExampleModuleScript>
{
    protected override IEnumerator ProcessTwitchCommand(string command)
    {
    }

    protected override IEnumerator TwitchHandleForcedSolve()
    {
    }
}

One of the most important parts about this library is understanding how it is able to access other data types.

The following 3 methods are called repeatedly within ModuleScript, allowing it to access data without you needing to explicitly type anything. In this Wiki, whenever you see that a method or property requires a certain data type, you do not need to declare it as a public field, or pass it in anywhere unless otherwise stated.

As part of the goal to cut down on writing repetitive lines of code, public fields that are attached to the same GameObject can be accessed through this script easily with either Get<T>() or GetAll<T>().

In all of the following methods, you can optionally explicitly allow null to be the return type with the allowNull argument as by default it will throw an error.

Get<T>()


Get<T>() is a method that will return the data type requested, acting as a GetComponent<T>(). The major difference here is that calling this method repeatedly is not inefficient because it will cache the result in a Dictionary<Type, Component[]>. It will still cache every instance of the data type, but only return the first one found.

// This will get the main module selectable.
var selectable = Get<KMSelectable>();

// This will get the exact same selectable by pulling it from a dictionary.
var sameSelectable = Get<KMSelectable>();

GetAll<T>()


GetAll<T>() does the same thing but it grabs all components on the same GameObject, use this whenever you have multiple components that you wish to grab.

// This will get every instance of type "KMAudio" from the current GameObject.
var audio = GetAll<KMAudio>(allowNull: true);

Cache<T>()


Cache<T>() is the method that is being called by Get<T>() and GetAll<T>(), where you need to specify a method to call for caching. This method should be used whenever you need to cache the returned values of expensive methods that return a component array.

// This will get every instance of type "Renderer" from the game object's children.
var audio = Cache<Renderer>(GetComponentsInChildren, allowNull: true);

WARNING: When using GetAll<T>() or Cache<T>(), do not assume order! If you need an array of components in a specific order, use public fields instead!

Clone this wiki locally