FANDOM


This script allows Global Variables made with AC to be linked with Application and Saved variables made with Bolt.

To use it:

  1. Create a new Global Variable in AC's Variables Manager, and set it's Link to field to Custom Script.
  2. Create a new Application or Saved variable in Bolt's Variables window, and have it match the type made in AC (Float, Boolean, etc)
  3. Paste the code below into a C# script named BoltVariableLink.cs, and attach it to a GameObject in the scene
  4. In the component's Inspector, increase the Size field by 1, and fill in the fields that appear.  The AC Variable ID is the number that appears beside a variable's name in the Variables Manager

The variables will now be linked - so changing its value in one asset will update the value of its associated variable in the other.

BoltVariableLink.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using AC;
using Bolt;

public class BoltVariableLink : MonoBehaviour
{

    [SerializeField] BoltVarLink[] boltVarLinks;


    private void OnEnable ()
    {
        EventManager.OnDownloadVariable += OnDownload;
        EventManager.OnUploadVariable += OnUpload;
    }


    private void OnDisable ()
    {
        EventManager.OnDownloadVariable -= OnDownload;
        EventManager.OnUploadVariable -= OnUpload;
    }


    private void OnDownload (GVar variable)
    {
        foreach (BoltVarLink boltVarLink in boltVarLinks)
        {
            boltVarLink.Download (variable);
        }
    }


    private void OnUpload (GVar variable)
    {
        foreach (BoltVarLink boltVarLink in boltVarLinks)
        {
            boltVarLink.Upload (variable);
        }
    }


    [System.Serializable] private class BoltVarLink
    {

        [SerializeField] private string boltVariableName = "";
        [SerializeField] private BoltVariableType boltVariableType = BoltVariableType.Application;
        private enum BoltVariableType { Application, Saved };
        [SerializeField] private int acVariableID = 0;


        public void Download (GVar variable)
        {
            if (!string.IsNullOrEmpty (boltVariableName) && variable.id == acVariableID)
            {
                if (boltVariableType == BoltVariableType.Application)
                {
                    switch (variable.type)
                    {
                        case VariableType.Integer:
                        case VariableType.PopUp:
                            variable.IntegerValue = (int) Variables.Application.Get (boltVariableName);
                            break;

                        case VariableType.Boolean:
                            variable.BooleanValue = (bool) Variables.Application.Get (boltVariableName);
                            break;
                    
                        case VariableType.Float:
                            variable.FloatValue = (float) Variables.Application.Get (boltVariableName);
                            break;

                        case VariableType.String:
                            variable.TextValue = (string) Variables.Application.Get (boltVariableName);
                            break;

                        case VariableType.Vector3:
                            variable.Vector3Value = (Vector3) Variables.Application.Get (boltVariableName);
                            break;
                    }
                }
                else if (boltVariableType == BoltVariableType.Saved)
                {
                    switch (variable.type)
                    {
                        case VariableType.Integer:
                        case VariableType.PopUp:
                            variable.IntegerValue = (int) Variables.Saved.Get (boltVariableName);
                            break;

                        case VariableType.Boolean:
                            variable.BooleanValue = (bool) Variables.Saved.Get (boltVariableName);
                            break;
                    
                        case VariableType.Float:
                            variable.FloatValue = (float) Variables.Saved.Get (boltVariableName);
                            break;

                        case VariableType.String:
                            variable.TextValue = (string) Variables.Saved.Get (boltVariableName);
                            break;

                        case VariableType.Vector3:
                            variable.Vector3Value = (Vector3) Variables.Saved.Get (boltVariableName);
                            break;
                    }
                }
            }
        }


        public void Upload (GVar variable)
        {
            if (!string.IsNullOrEmpty (boltVariableName) && variable.id == acVariableID)
            {
                if (boltVariableType == BoltVariableType.Application)
                {
                    switch (variable.type)
                    {
                        case VariableType.Integer:
                        case VariableType.PopUp:
                            Variables.Application.Set (boltVariableName, variable.IntegerValue);
                            break;

                        case VariableType.Boolean:
                            Variables.Application.Set (boltVariableName, variable.BooleanValue);
                            break;

                        case VariableType.Float:
                            Variables.Application.Set (boltVariableName, variable.FloatValue);
                            break;

                        case VariableType.String:
                            Variables.Application.Set (boltVariableName, variable.TextValue);
                            break;

                        case VariableType.Vector3:
                            Variables.Application.Set (boltVariableName, variable.Vector3Value);
                            break;
                    }
                }
                else if (boltVariableType == BoltVariableType.Saved)
                {
                    switch (variable.type)
                    {
                        case VariableType.Integer:
                        case VariableType.PopUp:
                            variable.IntegerValue = (int) Variables.Saved.Get (boltVariableName);
                            break;

                        case VariableType.Boolean:
                            variable.BooleanValue = (bool) Variables.Saved.Get (boltVariableName);
                            break;
                    
                        case VariableType.Float:
                            variable.FloatValue = (float) Variables.Saved.Get (boltVariableName);
                            break;

                        case VariableType.String:
                            variable.TextValue = (string) Variables.Saved.Get (boltVariableName);
                            break;

                        case VariableType.Vector3:
                            variable.Vector3Value = (Vector3) Variables.Saved.Get (boltVariableName);
                            break;
                    }
                }
            }
        }

    }

}