Skip to content
Emik edited this page Aug 31, 2021 · 24 revisions

Introduction

This wiki covers the basics of this library. If you need a detailed reference, refer to the exhaustive documentation.

It also assumes you have already installed the library, if not, refer to the installation instructions.

Before starting, make sure that your classes are using this library. Write this line above your classes alongside your other usings.

using KeepCoding;

Refer to the part documents to get yourself started.


Example Code

By the end of this wiki, you will fully understand the following code...

using KeepCoding;
using KModkit;
using System.Linq;
using UnityEngine;

public class Foo : ModuleScript
{
    internal bool IsCorrect { get { return TimeLeft % 10 != Get<KMBombInfo>().GetSerialNumberDigits().Last(); } }

    [SerializeField]
    internal KMSelectable egg;

    private float Next { get { return Random.Range(0, 1f); } }

    private void Start()
    {
        egg.Assign(onInteract: () =>
        {
            ButtonEffect(egg, 1, Sound.ButtonPress);

            if (!IsCorrect)
            {
                Strike("egg was pressed wrong :(");
                return;
            }

            egg.GetComponent<RigidBody>.velocity = new Vector3(Next, Next, Next);

            PlaySound(Sound.CorrectChime);
            Solve("egg was pressed right :)");
        });
    }
}
public class TPFoo : TPScript<Foo>
{
    public override IEnumerator ForceSolve()
    {
        yield return YieldUntil(true, () => Module.IsCorrect);
        Module.egg.OnInteract();
    }

    public override IEnumerator Process(string command)
    {
        string[] split = command.Split();

        if (!IsMatch(split[0], "press"))
            yield break;

        yield return PressCommand(command);
    }

    private IEnumerator PressCommand(string command)
    {
        // Return something to indicate that the command is valid.
        yield return null;

        // We need to see if there are a correct number of parameters.
        if (split.Length != 2)
        {
            yield return SendToChatError(split.Length == 1 ? "You must specify an argument!" : "Too many arguments!");
            yield break;
        }

        // The parameter can only be a character long.
        if (split[1].Length != 1)
        {
            yield return SendToChatError("The parameter is too long!");
            yield break;
        }

        yield return ParseInput(command);
    }
    
    private IEnumerator ParseInput(string command)
    {
        // The parameter can only be a character long.
        int i;

        if (!int.TryParse(split[1][0].ToString(), int i))
        {
            yield return SendToChatError("The parameter is not a number!");
            yield break;
        }

        // This waits until the user has their digit as the last timer.
        yield return new WaitUntil(() => i == Module.TimeLeft);
    
        // Press the egg.
        yield return new[] { Module.egg };
    }
}
Clone this wiki locally