Skip to content

Actions and Variables

233012santi edited this page Jul 7, 2023 · 12 revisions

Table of Contents

Actions

'Actions' provide a quick and easy way to execute arbitrary methods with Lunar Mobile Console.
quick actions

Using actions API

You can register/unregister actions using the following API calls:

  • LunarConsole.RegisterAction(name, callback) - registers action with name and callback
  • LunarConsole.UnregisterAction(callback) - unregisters action with specified callback
  • LunarConsole.UnregisterAction(name) - unregisters action with specified name
  • LunarConsole.UnregisterAllActions(target) - unregisters ALL actions with specified target

Example:

using UnityEngine;
using LunarConsolePlugin;

public class TestActions : MonoBehaviour
{
![Screenshot_20230706-172844](https://github.com/SpaceMadness/lunar-unity-console/assets/138212752/d1bea10e-dcd2-4f5a-b771-09a4f3cb2d4f)

    void Start()
    {
        LunarConsole.RegisterAction("My Action 1", Action1);
        LunarConsole.RegisterAction("My Action 2", Action2);
        LunarConsole.RegisterAction("My Action 3", Action3);
    }

    void OnDestroy()
    {
        LunarConsole.UnregisterAllActions(this); // don't forget to unregister!
    }

    void Action1()
    {
        ...
    }

    void Action2()
    {
        ...
    }

    void Action3()
    {
        ...
    }
}

Using LunarConsoleAction component

  • Select a GameObject from your scene and add Lunar Console Action component to it:
  • Use + and - buttons to add and remove delegates:

    The following delegate signatures are supported:
    • public void DoSomething()
    • public void DoSomething(bool value)
    • public void DoSomething(int value)
    • public void DoSomething(float value)
    • public void DoSomething(string value)
    • public void DoSomething(UnityEngine.Object value)
    • public bool MyProperty { get; set; }
    • public int MyProperty { get; set; }
    • public float MyProperty { get; set; }
    • public string MyProperty { get; set; }
    • public UnityEngine.Object MyProperty { get; set; }

Variables

Config variable (cvar) represents a value you can change from the console window while running your game on a mobile device.

variables

Defining variables

It's recommended to keep all your config variables in a single file: Scripts/Variables.cs

using LunarConsolePlugin;

public enum MyEnum
{
    One,
    Two,
    Three
}

[CVarContainer]
public static class Variables
{
    public static readonly CVar myBool = new CVar("My boolean value", true);
    public static readonly CVar myFloat = new CVar("My float value", 3.14f);
    public static readonly CVar myInt = new CVar("My integer value", 10);
    public static readonly CVar myString = new CVar("My string value", "Test");
    public static readonly CEnumVar<MyEnum> myEnum = new CEnumVar<MyEnum>("My enum value", MyEnum.Two);
}

Result:

IMPORTANT: Don't forget to add CVarContainer attribute to the class.

Using Variables

You can use the value of a variable with an implicit casts:

// implicit cast to bool
if (Variables.myBool)
{
    ...
}

// implicit cast to int
int intValue = 10 * Variables.myInt;

// implicit cast to float
float intValue = 2 * Variables.myFloat;

// implicit cast to string
string value = "My value: " + Variables.myString;

Listening For Variable Changes

You can register delegates and get notified whenever the value is changed:

myVar.AddDelegate((value) => {
	Debug.Log("New value is " + value);
});

Don't forget to remove the delegate to avoid memory leaks (would be changed to weak references in the future)

myVar.RemoveDelegates(target);