Global Variables

You can access and change the global variables you have assigned to a DanmakuContext from within your own code. Use the methods GetVariable / SetVariable of the class DanmakuContext to manipulate these values, which you can use in DanmakuJSON's Terms.

Note, that there is the built-in global variable #random which returns a new random value between 0.0 and 1.0 every time it is referenced.

Example

using UnityEngine;
using VirtualDropkick.DanmakuEngine.Unity;

public class MyValueModifier : MonoBehaviour
{
    public float myValue = 4f;

    private DanmakuContext context;
    
    void Awake()
    {
        context = DanmakuController.Instance.GetContext("Enemy");

        // set the value of "#val" to myValue...
        context.SetVariable("val", myValue);
    }

    void Update()
    {
        // increase myValue
        myValue += 0.1f;

        // set the value of "#val" to myValue...
        context.SetVariable("val", myValue);

        // print the value of "#val"...
        Debug.Log("#val = " + context.GetVariable("val"));
    }
}

Adding custom functions

If you have to execute any logic before the value of a variable can be determined, you can register a parameterless method or delegate (with the return type float) to a DanmakuContext by using its RegisterVariableFunction-method.

Example

using UnityEngine;
using VirtualDropkick.DanmakuEngine.Unity;

public class MyVariableHandler : MonoBehaviour
{
    private DanmakuContext context;
    
    void Awake()
    {
        context = DanmakuController.Instance.GetContext("Enemy");
        context.RegisterVariableFunction("pi", MyPiFunction);
    }

    public float MyPiFunction()
    {
        // put your calculations here...
        return Mathf.PI;
    }
}

 


Buy the Danmaku Engine plugin

Leave a comment