Forum rules - please read before posting.

Custom Action Saves Variables

Hello! I have a custom action with private variables. Whenever I run the action, the variables' values seem to be saved in runtime. For instance, if I change a boolean from its default value of false to true during a run, the next time I run the action, the boolean will still have the value of true by default. How can I prevent this from happening?

Comments

  • edited April 2023

    This is my Code, The variable grabbedName stays true when I run another action of "ActionWaitForAnimation", but it should always be false the first time it runs.

      public class ActionWaitForAnimation : ActionCheck
            {
                public override ActionCategory Category { get { return ActionCategory.Custom; } }
                public override string Title { get { return "Check Animation Playing"; } }
                public override string Description { get { return "Queries if animation is playing"; } }
    
    
    
                public Animator animator;
                public int constantID = 0;
    
                public enum AnimType { ByName, Current }
                public AnimType animType;
    
                public string animationName;
                private string checkName;
    
                public bool isPlayer;
                private bool grabbedName = false;
    
                public override float Run()
                {
                    if (!isRunning)
                    {
                        Debug.Log("Run Grabbed name: " + grabbedName);
                        isRunning = true;
                        return defaultPauseTime / 6f;
                    }
                    else
                    {
                        isRunning = false;
                        return 0f;
                    }
                }
    
                public override void Skip()
                {
                    Run();
                }
    
                public override bool CheckCondition()
                {
                    Debug.Log("!!!!ACTION: Check condition");
                    if (animator == null && isPlayer) animator = KickStarter.player.GetAnimator();
                    if (animator == null) { Debug.LogError("Animator is null"); return true; }
                    checkName = CheckName();
                    return CheckIfRunning();
                }
    
                string CheckName()
                {
                    if (animType == AnimType.ByName) { Debug.Log("By name: " + animationName); return animationName; }
                    if (grabbedName) { Debug.Log("Name already grabbed"); return checkName; }
    
                    var info = animator.GetCurrentAnimatorClipInfo(0);
                    var animation = info[info.Length - 1];
                    string name = animation.clip.name;
                    checkName = name;
                    grabbedName = true;
                    Debug.Log("Grabbed name: " + checkName);
                    return name;
                }
    
                bool CheckIfRunning()
                {
                    var info = animator.GetCurrentAnimatorClipInfo(0);
                    var animation = info[info.Length - 1];
                    var name = animation.clip.name;
                    Debug.Log("Animation Name = " + checkName + "||  Clip Name = " + name);
                    if (name == checkName)
                    {
                        return true;
                    }
                    grabbedName = false;
                    return false;
                }
    
        #if UNITY_EDITOR
    
                public override void ShowGUI(List<ActionParameter> parameters)
                {
                    isPlayer = EditorGUILayout.Toggle("Is Player:", isPlayer);
                    if (!isPlayer)
                    {
                        animator = (Animator)EditorGUILayout.ObjectField("Animator:", animator, typeof(Animator), true);
                        constantID = FieldToID(animator, constantID);
                        animator = IDToField(animator, constantID, true);
                    }
                    animType = (AnimType)EditorGUILayout.EnumPopup("Check type:", animType);
                    if (animType == AnimType.ByName)
                    {
                        animationName = EditorGUILayout.TextField(" name:", animationName);
                    }
                }``
    
  • An Action's Run function will be called before CheckCondition - you can assign the intended default value inside it.

    This function can also be simplified because it doesn't need to run over multiple frames:

    public override float Run()
    {
        grabbedName = false;
        Debug.Log("Run Grabbed name: " + grabbedName);
        return 0f;
    }
    
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.