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

2D Roguelike Tutorial; Player is able to move once during setup of the game board

Discussion in 'Scripting' started by steadmuffin, Jun 17, 2016.

  1. steadmuffin

    steadmuffin

    Joined:
    Jun 1, 2016
    Posts:
    49
    The player is able to move once when the loading screen is up and the game board is setting up. I don't want my player to move at all during setup. According to the tutorial, the player shouldn't be able to move at all during set up when doingSetup is true. Also, I'm not sure if this problem is related, but the game-over dying sound doesn't happen exactly when the player dies: it happens when I attempt to move after he dies.

    I tried playing arround with the if statement and tried:


    Code (CSharp):
    1. if (playersTurn || enemiesMoving || !doingSetup)
    2.             return;
    When I try the code above, the player is able to move more than once during the loading screen when the game board is setting up, but after the set up is finished, the player is no longer able to move.

    Would someone please figure out why the player is able to move while the game board is setting up?


    Original code below:
    Code (CSharp):
    1.  
    2. public class GameManager : MonoBehaviour
    3. {
    4.     public float levelStartDelay = 2f;
    5.     public float turnDelay = .1f;
    6.     public static GameManager instance = null;
    7.     public BoardManager boardScript;
    8.     //public Player player;
    9.     public int playerFoodPoints = 100;
    10.     [HideInInspector] public bool playersTurn = true;
    11.  
    12.     private Text levelText;
    13.     private GameObject levelImage;
    14.     private int level = 1;
    15.     private List<Enemy> enemies;
    16.     private bool enemiesMoving;
    17.     private bool doingSetup;
    18.  
    19.     // Use this for initialization
    20.     void Awake ()
    21.     {
    22.         if (instance == null)
    23.             instance = this;
    24.         else if (instance != this)
    25.             Destroy (gameObject);
    26.  
    27.         DontDestroyOnLoad (gameObject);
    28.         enemies = new List<Enemy> ();
    29.         boardScript = GetComponent<BoardManager> ();
    30.         InitGame ();
    31.     }
    32.  
    33.     private void OnLevelWasLoaded (int index)
    34.     {
    35.         level++;
    36.  
    37.         InitGame();
    38.     }
    39.  
    40.     void InitGame()
    41.     {
    42.         doingSetup = true;
    43.  
    44.         levelImage = GameObject.Find ("LevelImage");
    45.         levelText = GameObject.Find ("LevelText").GetComponent<Text> ();
    46.         levelText.text = "Day " + level;
    47.         levelImage.SetActive (true);
    48.         Invoke ("HideLevelImage", levelStartDelay);
    49.  
    50.         enemies.Clear ();
    51.         boardScript.SetupScene (level);
    52.         //Instantiate (player);
    53.     }
    54.  
    55.     private void HideLevelImage()
    56.     {
    57.         levelImage.SetActive (false);
    58.         doingSetup = false;
    59.     }
    60.  
    61.     public void GameOver()
    62.     {
    63.         levelText.text = "After " + level + " days, you starved";
    64.         levelImage.SetActive (true);
    65.         enabled = false;
    66.     }
    67.    
    68.     void Update ()
    69.     {
    70.         if (playersTurn || enemiesMoving || doingSetup) // HERE IS ORIGINAL THE TEST FOR "doingSetup"
    71.             return;
    72.         StartCoroutine (MoveEnemies ());
    73.     }
    74.  
    75.     public void AddEnemyToList(Enemy script)
    76.     {
    77.         enemies.Add (script);
    78.     }
    79.  
    80.     IEnumerator MoveEnemies()
    81.     {
    82.         enemiesMoving = true;
    83.         yield return new WaitForSeconds(turnDelay);
    84.         if (enemies.Count == 0)
    85.         {
    86.             yield return new WaitForSeconds (turnDelay);
    87.         }
    88.  
    89.         for (int i = 0; i < enemies.Count; i++)
    90.         {
    91.             enemies [i].MoveEnemy ();
    92.             yield return new WaitForSeconds (enemies [i].moveTime);
    93.         }
    94.  
    95.         playersTurn = true;
    96.         enemiesMoving = false;
    97.     }
    98. }
    99.  
     
  2. steadmuffin

    steadmuffin

    Joined:
    Jun 1, 2016
    Posts:
    49
    Code (CSharp):
    1. if (playersTurn || enemiesMoving || !doingSetup)
    2.             return;
    When I code above, I can move as many times as I want when the board is setting up but the interesting thing is after the board sets up, I can move one time and then I cannot move.

    Also, I noticed that after the enemy kills me, I can also move only once(dead player is not supposed to move). I suspect that these bugs are related.

    It is interesting how when I desire the player to not be able to move, he is able to move one time and then his moves are disabled.
     
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,370
    To make it more interesting, you should attach the debugger to see what values you have for those booleans you're checking.

    Alternatively, spit the true/false values out to Debug.Log() and figure out what is going on!

    I like this type of thing, use it ALL the time for pieces of code that don't lend themselves to being breakpointed every frame.

    Code (csharp):
    1. Debug.Log( System.String.Format( "playersTurn={0}  enemiesMoving={1}  doingSetup={2}",
    2. playersTurn, enemiesMoving, doingSetup));
     
  4. steadmuffin

    steadmuffin

    Joined:
    Jun 1, 2016
    Posts:
    49
    @Kurt Dekker Thanks for the idea. So I checked the inspector and used the Debug.Log to look at the playersTurn Boolean. When the game is setting up, the playersTurn is true. When I try moving the player, the player moves and the playersTurn becomes false and am no longer able to move, hence I can move once during setup.

    I am trying to figure a way out to either disable the player or make playersTurn false when doingSetup is true.