Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Help understanding global enums

Discussion in 'Editor & General Support' started by Fenwat, Aug 13, 2023.

  1. Fenwat

    Fenwat

    Joined:
    Aug 13, 2023
    Posts:
    3
    I have a functioning script that manages my player's weapon moveset and animations using enums. I am trying to separate this one script into two, partially to have cleaner more manageable scripts and partly to try to understand global enums. After separating my script into two I have a MovesetManager script that uses temporary user inputs to swap between EMPTY, SHORT_SWORD, and SHORT_SPEAR:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5.  
    6. public enum MovesetStates
    7.     {
    8.         EMPTY,
    9.         SHORT_SWORD,
    10.         SHORT_SPEAR
    11.     }
    12.  
    13.  
    14. public class MovesetManager : MonoBehaviour
    15. {
    16.     public MovesetStates CurrentMovesetState
    17.     {
    18.         set
    19.         {
    20.             if(stateLock == false)
    21.             {
    22.                 currentMovesetState = value;
    23.  
    24.                 switch(currentMovesetState)
    25.                 {
    26.                     case MovesetStates.EMPTY:
    27.                         break;
    28.                     case MovesetStates.SHORT_SWORD:
    29.                         break;
    30.                     case MovesetStates.SHORT_SPEAR:
    31.                         break;
    32.                 }
    33.             }
    34.         }
    35.         get
    36.         {
    37.             return currentMovesetState;
    38.         }
    39.     }
    40.  
    41.     public PlayerInputActions playerInputActions;
    42.     public MovesetStates currentMovesetState;
    43.     bool stateLock = false;
    44.  
    45.     public void Awake()
    46.     {
    47.         playerInputActions = new PlayerInputActions();
    48.     }
    49.  
    50.     public void OnEnable()
    51.     {
    52.         playerInputActions.Player.Unequip.performed += DoUnequip;
    53.         playerInputActions.Player.Unequip.Enable();
    54.         playerInputActions.Player.EquipShortSword.performed += DoEquipShortSword;
    55.         playerInputActions.Player.EquipShortSword.Enable();
    56.         playerInputActions.Player.EquipShortSpear.performed += DoEquipShortSpear;
    57.         playerInputActions.Player.EquipShortSpear.Enable();
    58.     }
    59.  
    60.     public void OnDisable()
    61.     {
    62.         playerInputActions.Player.Unequip.Disable();
    63.         playerInputActions.Player.EquipShortSword.Disable();
    64.         playerInputActions.Player.EquipShortSpear.Disable();
    65.     }
    66.  
    67.  
    68.     public void DoUnequip(InputAction.CallbackContext obj)
    69.     {
    70.         CurrentMovesetState = MovesetStates.EMPTY;
    71.     }
    72.    
    73.     public void DoEquipShortSword(InputAction.CallbackContext obj)
    74.     {
    75.         CurrentMovesetState = MovesetStates.SHORT_SWORD;
    76.     }
    77.    
    78.     public void DoEquipShortSpear(InputAction.CallbackContext obj)
    79.     {
    80.         CurrentMovesetState = MovesetStates.SHORT_SPEAR;
    81.     }
    82. }
    I also have an AnimationManager script that is supposed to take the currentMovesetState and assign a string to movesetName in order to slot it into the switch statement (I think that is what its called). Here is the AnimationManager:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public enum AnimationStates
    6. {
    7.     IDLE,
    8.     RUN,
    9.     ROLL
    10. }
    11. public class AnimationManager : MonoBehaviour
    12. {
    13.    public AnimationStates CurrentAnimationState
    14.     {
    15.         set
    16.         {
    17.             if(stateLock == false)
    18.             {
    19.                 currentAnimationState =  value;
    20.                 switch(currentAnimationState)
    21.                 {
    22.                     case AnimationStates.IDLE:
    23.                         animator.Play($"{movesetName}_idleFront");
    24.                         break;
    25.                     case AnimationStates.RUN:
    26.                         animator.Play($"{movesetName}_runFront");
    27.                         break;
    28.                     case AnimationStates.ROLL:
    29.                         animator.Play($"{movesetName}_rollFront");
    30.                         break;
    31.                 }
    32.             }
    33.         }
    34.         get
    35.         {
    36.             return currentAnimationState;
    37.         }
    38.     }
    39.  
    40.     SpriteRenderer spriteRenderer;
    41.     Animator animator;
    42.     public AnimationStates currentAnimationState;
    43.     public MovesetStates currentMovesetState;
    44.    
    45.     public string movesetName;
    46.     bool stateLock = false;
    47.  
    48.     public void Start()
    49.     {
    50.         animator = GetComponent<Animator>();
    51.         spriteRenderer = GetComponent<SpriteRenderer>();
    52.     }
    53.  
    54.     public void Update()
    55.     {
    56.         if (currentMovesetState == MovesetStates.EMPTY)
    57.         {
    58.             movesetName = "unequipped";
    59.             CurrentAnimationState = currentAnimationState;
    60.         }
    61.         else if (currentMovesetState == MovesetStates.SHORT_SWORD)
    62.         {
    63.             movesetName = "shortSword";
    64.             CurrentAnimationState = currentAnimationState;
    65.         }
    66.         else if (currentMovesetState == MovesetStates.SHORT_SPEAR)
    67.         {
    68.             movesetName = "shortSpear";
    69.             CurrentAnimationState = currentAnimationState;
    70.         }
    71.         else
    72.         {
    73.             movesetName = "unequipped";
    74.             CurrentAnimationState = currentAnimationState;
    75.         }
    76.     }
    77. }
    78.  
    The inspector shows the "public MovesetStates currentMovesetState;" drop down menu in both new scripts, but when I run the game and use the three InputActions, only the MovesetManager's dropdown updates. I assume there is some fundamental part of global enums I am misunderstanding, but I cant seem to figure out why. Any help would be appreciated!

    Also, I am new to Unity and coding in general, so any feedback on how to improve my code structure or naming conventions is also appreciated!
     
  2. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,326
    You have two separate variables in two separate scripts that just happen to have the same name and type. What makes you think that updating one also update the other?
     
  3. Fenwat

    Fenwat

    Joined:
    Aug 13, 2023
    Posts:
    3
    Like I said, I'm new to this so I apologize if I misunderstood. I read somewhere that if the Enum was placed outside of the class it could be accessed from other scripts.
     
  4. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,326
    Ah, ok.
    In this case the enum is just this part here:

    public enum MovesetStates
    {
    EMPTY,
    SHORT_SWORD,
    SHORT_SPEAR
    }

    So any script has access to MovesetStates.EMPTY, MovesetStates.SHORT_SWORD, etc. and also any script can have members that are of type MovesetStates. You can think of an enum as being similar to a "type".

    But your variable called currentMovesetState is not global so if you want to access it in the animation manager, then you need a reference to the MoveSetManager.
     
  5. Fenwat

    Fenwat

    Joined:
    Aug 13, 2023
    Posts:
    3
    Ok that makes sense. Thanks! I was able to getting it working as intended.
     
  6. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    521
    There are a number of "things" going on which (I think) you should take a moment to reflect on. First enum definitions should typically be singular so rename them MovesetState and AnimationState.

    This code in the setter doesn't do anything. If you intend to add something soon keep but otherwise it switches for no reason.

    Code (CSharp):
    1.  
    2. switch(currentMovesetState)
    3. {
    4.     case MovesetStates.EMPTY:
    5.         break;
    6.     case MovesetStates.SHORT_SWORD:
    7.         break;
    8.     case MovesetStates.SHORT_SPEAR:
    9.         break;
    10. }
    11.  
    You have a property named stateLock which appears to always be false. Again if you intend to use it fine otherwise it seems meaningless.

    In your AnimationManager you have a public getter/setter named CurrentAnimationState. It not only works as a setter but you are playing an animation in there as well. Bad habit to do anything but modify the backing field from a setter. Anything you call can throw an exception and nobody expects that when setting the state of something.

    The following should look "wildly verbose" to you. The CurrentAnimationState is set to the currentAnimationState in all cases, no need to repeat it 4 times. A method named GetMoveSetName could return the correct string and this all reduces to 2 lines. But if we dig a bit it appears you are setting the movesetName for one purpose to determine which animation to play. So you wouldn't need the string and a method would simply return the animation name to play instead.

    Might be a good idea to attend to a bit of clean up while the code is still manageable.

    Code (CSharp):
    1. public void Update()
    2.     {
    3.         if (currentMovesetState == MovesetStates.EMPTY)
    4.         {
    5.             movesetName = "unequipped";
    6.             CurrentAnimationState = currentAnimationState;
    7.         }
    8.         else if (currentMovesetState == MovesetStates.SHORT_SWORD)
    9.         {
    10.             movesetName = "shortSword";
    11.             CurrentAnimationState = currentAnimationState;
    12.         }
    13.         else if (currentMovesetState == MovesetStates.SHORT_SPEAR)
    14.         {
    15.             movesetName = "shortSpear";
    16.             CurrentAnimationState = currentAnimationState;
    17.         }
    18.         else
    19.         {
    20.             movesetName = "unequipped";
    21.             CurrentAnimationState = currentAnimationState;
    22.         }
    23.     }
    24.