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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Using Enums in if statements in a turn based game.

Discussion in 'Scripting' started by JustaDuck97, May 11, 2020.

  1. JustaDuck97

    JustaDuck97

    Joined:
    Jul 31, 2019
    Posts:
    45
    I am experimenting with the concept of turn based combat and I am using Enums to keep track of the battle states. I need to check the current battle state in a couple if else statements. when I try to do this It usually results in the error: "Cannot Convert GameStateManager.BattleState to Bool" this is an example that results in that error
    Code (CSharp):
    1. if(State =  GameStateManager.BattleState.win)
    2.       {
    3.  
    4.  
    5.       }
    But this isn't always the result I can and have used the game state in an if statement with no error in my player manager in this example:
    Code (CSharp):
    1. public void PlayerAttack1()
    2.     {
    3.         if (GameM.Statemanager.State != GameStateManager.BattleState.PlayerAction)
    4.         {
    5.             return;
    6.         }
    7.         else
    8.         {          
    9.             Attack1();
    10.             GameM.Statemanager.PlayerHasPerformedAction();
    11.         }      
    12.     }
    I'm new to using enums and am very confused. Below is the entirety of the Game State Manager Script:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. public class GameStateManager : MonoBehaviour
    6. {
    7.     public enum BattleState { Start, SpeedRoll,  PlayerAction,  EnemyAction, win, lose}
    8.     public BattleState State;  
    9.  
    10.     private Unit player;
    11.     public BattleHuds playerHud;
    12.     public GameObject playerGO;
    13.     public Transform playerSpawn;
    14.  
    15.     private Unit Enemy;
    16.     public BattleHuds enemyHud;
    17.     public GameObject enemyGO;
    18.     public Transform enemySpawn;
    19.     public Text Dialog;
    20.    
    21.  
    22.     [SerializeField]
    23.     internal GameManager GameM;
    24.  
    25.     void Start()
    26.     {
    27.      State = BattleState.Start;
    28.      StartCoroutine(InstantiateUnits());
    29.                  
    30.     }  
    31.     IEnumerator InstantiateUnits()
    32.     {
    33.      Instantiate(playerGO, playerSpawn);
    34.      Instantiate(enemyGO, enemySpawn);
    35.      player = playerGO.GetComponent<Unit>();
    36.      playerHud.SetUpHUD(player);
    37.      Enemy = enemyGO.GetComponent<Unit>();
    38.      enemyHud.SetUpHUD(Enemy);
    39.      yield return new WaitForSeconds(1);            
    40.      SpeedRoll();    
    41.     }    
    42.  
    43.     void SpeedRoll()
    44.     {
    45.       float playerSpeedRoll = Random.Range(0, player.unitSpeed);
    46.  
    47.       float enemySpeedRoll = Random.Range(0, Enemy.unitSpeed);
    48.  
    49.       if(playerSpeedRoll > enemySpeedRoll)
    50.       {
    51.        State = BattleState.PlayerAction;
    52.             Dialog.text = "Choose An Action";
    53.            
    54.       }
    55.       else
    56.       {
    57.        State = BattleState.EnemyAction;
    58.             Dialog.text = "Enemy turn";
    59.             enemyHud.CheckHP(Enemy);
    60.             GameM.EnemyMan.onEnemyTurn();
    61.  
    62.         }
    63.     }
    64.  
    65.     public void PlayerHasPerformedAction()
    66.     {
    67.         StartCoroutine(PlayerChangingTurn());
    68.         playerHud.SetUpHUD(player);
    69.     }      
    70.  
    71.     public void EnemyHasPerformedAction()
    72.     {
    73.         StartCoroutine(EnemyChangingTurn());
    74.         enemyHud.SetUpHUD(Enemy);
    75.     }
    76.  
    77.     IEnumerator PlayerChangingTurn()
    78.     {
    79.         yield return new WaitForSeconds(1);
    80.         {      
    81.          if(Enemy.unitCurrentHealth <= 0)
    82.          {
    83.              State = BattleState.win;
    84.                 Dialog.text = "You Win!";
    85.                 StartCoroutine(BattleEnd());
    86.          }
    87.          else
    88.          {
    89.            State = BattleState.EnemyAction;
    90.                 Dialog.text = "Enemy turn";
    91.                 enemyHud.CheckHP(Enemy);
    92.                 GameM.EnemyMan.onEnemyTurn();
    93.  
    94.          }
    95.         }
    96.     }
    97.  
    98.     IEnumerator EnemyChangingTurn()
    99.     {
    100.         yield return new WaitForSeconds(1);      
    101.         {
    102.             if (player.unitCurrentHealth <= 0)
    103.             {
    104.                 State = BattleState.lose;
    105.                 StartCoroutine(BattleEnd());
    106.                 Dialog.text = "You lose!";
    107.  
    108.             }
    109.             else
    110.             {   playerHud.CheckHP(player);
    111.                 State = BattleState.PlayerAction;
    112.                 Dialog.text = "Choose An Action";
    113.                
    114.  
    115.             }
    116.         }
    117.     }
    118.  
    119.     IEnumerator BattleEnd()
    120.     {
    121.         yield return new WaitForSeconds(1);
    122.       if(State =  GameStateManager.BattleState.win)
    123.       {
    124.  
    125.       }
    126.       else if(player.unitCurrentHealth <= 0)
    127.       {
    128.  
    129.       }
    130.     }
    131.  
    132.    
    133.    
    134. }
    135.  
    and this is the player manger script that uses the enums successfully:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerManager : MonoBehaviour
    6. {
    7.     [SerializeField]
    8.     internal GameManager GameM;
    9.     GameObject playerGO;
    10.     Unit playerUnit;
    11.  
    12.   public void start()
    13.     {
    14.       playerGO = GameObject.FindGameObjectWithTag("Player");
    15.       playerUnit = playerGO.GetComponent<Unit>();
    16.     }
    17.  
    18.     public void PlayerAttack1()
    19.     {
    20.         if (GameM.Statemanager.State != GameStateManager.BattleState.PlayerAction)
    21.         {
    22.             return;
    23.         }
    24.         else
    25.         {          
    26.             Attack1();
    27.             GameM.Statemanager.PlayerHasPerformedAction();
    28.         }      
    29.     }
    30.  
    31.     public void PlayerAttack2()
    32.     {
    33.         if (GameM.Statemanager.State != GameStateManager.BattleState.PlayerAction)
    34.         {
    35.             return;
    36.         }
    37.         else
    38.         {
    39.             Attack2();
    40.         }
    41.     }
    42.  
    43.     public void Attack1()
    44.     {
    45.         GameM.AttackM.FlameAttack();              
    46.     }
    47.  
    48.     public void Attack2()
    49.     {
    50.         Debug.Log("Player attempting heal");
    51.         GameM.AttackM.WeakHeal(playerUnit);
    52.         Debug.Log("heal Request went through");
    53.     }
    54. }
    55.    
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    The error in your first code snippet is that you used = instead of ==

    == tests whether two things are equal (and returns true or false)

    = makes the thing on the left equal to the thing on the right (and returns that value)
     
  3. JustaDuck97

    JustaDuck97

    Joined:
    Jul 31, 2019
    Posts:
    45
    Oh my god I'm an idiot. I knew that. I've been trying to fix this for hours...