Forum rules - please read before posting.

Change animator parameter to global variable

Apologies if this has been answered multiple times, but scrubbing through the forums I still can't seem to figure out the final steps missing from this documentation on linking animator parameters to variables: https://adventure-creator.fandom.com/wiki/Link_an_Animator_parameter_to_a_Variable

I've seen some posts about reading animator value and that it should use an action list to Variable: Check, but I don't quite understand the opposite. Is there an action list to use to actually set the animator parameter to a global variable? Is there something I'm missing with Variable: Set?

Comments

  • What "final step" is missing in the wiki page? Though, the script you link to syncs an Animator parameter to a Component variable - not a global one.

    This equivalent script will link an Animator parameter to a global variable:

    using UnityEngine;
    using AC;
    
    public class GlobalAnimatorParameterLink : MonoBehaviour
    {
    
        public string sharedVariableName;
        private Animator _animator;
    
    
        private void OnEnable ()
        {
            _animator = GetComponent <Animator>();
            EventManager.OnDownloadVariable += OnDownload;
        }
    
    
        private void OnDisable ()
        {
            EventManager.OnDownloadVariable -= OnDownload;
        }
    
    
        private void OnDownload (GVar variable, Variables variables)
        {
            if (_animator == null || variables != null || string.IsNullOrEmpty (sharedVariableName))
            {
                return;
            }
    
            if (variable.label == sharedVariableName)
            {
                switch (variable.type)
                {
                    case VariableType.Boolean:
                        variable.BooleanValue = _animator.GetBool (sharedVariableName);
                        break;
    
                    case VariableType.Integer:
                        variable.IntegerValue = _animator.GetInteger (sharedVariableName);
                        break;
    
                    case VariableType.Float:
                        variable.FloatValue = _animator.GetFloat (sharedVariableName);
                        break;
    
                    default:
                        break;
                }
            }
        }
    
    }
    

    To use it, attach to your Animator and type in the name of the parameter to read from - which should match the name of the AC Global Variable to link it to.

  • Ah! Egg on my face for looking at the wrong script the whole time. Unfortunately even with the correct script I'm either missing a step or just don't understand how to get the function going. As a test I threw a cycle button in my menu and an animator that just changes the color of a square. Can see in this video that the global variable is changing but the animator INT remains at 0:

    https://drive.google.com/file/d/1KkVQyTVaaRQRJEs9jFmXI8gmmtUYw7o7/view?usp=sharing

  • Your post mentioned you were looking to update a variable based on an Animator parameter value - not the other way around. These are two separate operations - each direction has to be specifically defined.

    This alternative script will do the opposite - update the Animator parameter when the Global Variable is updated:

    using UnityEngine;
    using AC;
    
    public class GlobalAnimatorParameterLink : MonoBehaviour
    {
    
        public string sharedVariableName;
        private Animator _animator;
    
    
        private void OnEnable ()
        {
            _animator = GetComponent <Animator>();
            EventManager.OnUploadVariable += OnUpload;
        }
    
    
        private void OnDisable ()
        {
            EventManager.OnUploadVariable -= OnUpload;
        }
    
    
        private void OnUpload (GVar variable, Variables variables)
        {
            if (_animator == null || variables != null || string.IsNullOrEmpty (sharedVariableName))
            {
                return;
            }
    
            if (variable.label == sharedVariableName)
            {
                switch (variable.type)
                {
                    case VariableType.Boolean:
                        _animator.SetBool (sharedVariableName, variable.BooleanValue);
                        break;
    
                    case VariableType.Integer:
                        _animator.SetInteger (sharedVariableName, variable.IntegerValue);
                        break;
    
                    case VariableType.Float:
                        _animator.SetFloat (sharedVariableName, variable.FloatValue);
                        break;
    
                    default:
                        break;
                }
            }
        }
    
    }
    

    For more on this topic, see the Manual's "Variable linking" chapter.

  • Apologies for the confusion in the original post, my own confusion made that messy, haha. The post title at least had the right idea.

    Script is working whenever the variable is updated, thank you so much. This works for the UI I was thinking of, but there caveat is that the animator and global variable must both reset if the animator is ever disabled, or else they will most likely not match again.

    Say the animator is 4 by default, and the global variable is set to 0 by default. When enabling the animator that value will not match until the global variable changes. Would there be a way to change the script to update always and not just on variable change? That way if I left the menu and came back it would still match?

    Here is another video just to show you what I mean:
    https://drive.google.com/file/d/10yNyT61PnSbrfngeMOYt9ORJ-6QVteNl/view?usp=sharing

  • The example scripts above make use of the variable-linking system, which allows for a two-way connection between a variable and another script - so that changing either side can affect the other.

    If you're simply looking to update an Animator parameter each frame to a variable, you don't need to link it to anything - just read its value in an Update loop:

    using UnityEngine;
    using AC;
    
    public class GlobalAnimatorParameterLink : MonoBehaviour
    {
    
        public string sharedVariableName;
        private Animator _animator;
        private GVar globalVariable;
    
        private void Start ()
        {
            _animator = GetComponent <Animator>();
            globalVariable = GlobalVariables.GetVariable (sharedVariableName);
        }
    
        private void Update ()
        {
            _animator.SetInteger (sharedVariableName, globalVariable.IntegerValue);
        }
    
    }
    
  • Ohhh shoot, that is way more simple, haha. Thank you so much for the help again!

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.