Input in a turn-based game freezing

Trying to set up a turn-based game with a scheduler that selects which entity is allowed to take a turn.

When it’s the player’s turn, I want to be able to keep accepting input (and updating the player character entity and GUIs) until the player enters a commit command, at which point the control should be returned to the scheduler so it can assign the next turn to another entity.

I was having the scheduler call a ‘take turn’ function on the player entity, but I keep implementing infinite loops and so Unity freezes.
I am unsure if what I am trying to do requires coroutines and yield, or if I’m just approaching this in the wrong way.

Could someone please describe a sensible method to achieve this. I’m using C#, if you want to provide any sample code.

EDIT: Added the current code I’m using below. I’d tried some even simpler methods before, but discarded them. I’d prefer if someone could explain where my understanding is failing, rather than the problem with this specific code. The player.Move function is currently just assigning the Vector3 to the player character transform’s position.

PlayerController.cs

using UnityEngine;
using System.Collections;

public class PlayerController : EntityController {

	public float inputDelay = 10.0f;
	public Entity player;
	
	public bool GetPlayerInput() {
		float t = Time.time - inputDelay;

		if (Input.GetKeyUp(KeyCode.Space)) {
			return false;
		}

		if (Time.time > t + inputDelay) {
			t = Time.time;
			player.Move(new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical")));
		}
		return true;
	}
}

Manager.cs

using UnityEngine;
using System.Collections;

public class Manager : MonoBehaviour {

	public PlayerController player;
	bool game = true;

	void Start () {
		while (game == true) {
			game = player.GetPlayerInput();
		}
	}

}

Thanks for any help! :slight_smile:

You must never use an infinite loop thats not in a coroutine and yields. because when this loop enters, there will be no other code running at all anymore in the engine beside of this, if you don’t correctly yield.

I would make it with a State machine pattern.

Like:
First you need to enumerate your states:

enum GameState
{
    PlayerTurn,
    OponentTurn,
    EndOfGame,
    StartOfGame,
    Pause,
    ... add as many as you need
} 

Then later inside your Update loops, you implement this statemachine:

switch(gameState)
{
  case GameState.PlayerTurn:
     ... check for player stuff here 
     ... use functions/methods to be able to reuse the checks for multiple states)
  break;

  case GameState.OponentTurn:

     ... calculate how the oponent moves
     ... show animations of oponents movement
     ... etc.

  break;

  default: // catch any states you havent programmed yet
      Debug.Log("state uknown: " + gameState);
  break;
}

This way the global variable gameState will be telling which functions to call each update/frame and your game does not freeze, it just changes behaviours.