Skip to content

Actions and Variables

Alex Lementuev edited this page Feb 2, 2017 · 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
{
    void Start()
    {
        LunarConsole.RegisterAction("Action-1", Action1);
        LunarConsole.RegisterAction("Action-2", Action2);
        LunarConsole.RegisterAction("Action-3", Action3);
    }

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

    void Action1()
    {
        ...
    }

    void Action2()
    {
        ...
    }

    void Action3()
    {
        ...
    }
}

Using LunarConsoleActions component

  • Select a GameObject from your scene and add Lunar Console Actions component to it:
- Use ```Add``` and ```Remove``` buttons to register and unregister actions: - Use ```Dont Destroy On Load``` checkbox to keep actions between scene loading

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;

[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");
}

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);

Sidebar

Clone this wiki locally