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!