Talk:Linking AC and Bolt variables/@comment-45986276-20200603145421

Updated code with the help of a good friend for those who have issues with a newer version of AC:

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, AC.Variables variables) {    foreach (BoltVarLink boltVarLink in boltVarLinks) {        boltVarLink.Download(variable); } }

private void OnUpload(GVar variable, AC.Variables variables) {    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)Bolt.Variables.Application.Get(boltVariableName); break;

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

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

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

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

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

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

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

case VariableType.Vector3: variable.Vector3Value = (Vector3)Bolt.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: Bolt.Variables.Application.Set(boltVariableName, variable.IntegerValue); break;

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

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

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

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

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

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

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

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

}

}

​​​​​​​