Reputation: 45
I need a way to check if A UI button is pressed the on click event isn’t really helpful because the Input method (called etch time its the player’s turn) has to return a value back to the main function while loop will completely stop the game and the input should only be accepted when it’s the players turn (when players turn Method is waiting for input) , for these reasons the Unity Event Trigger doesn’t seem like a useable option .all I need is a way to check the state of the button .
Note: that im useing Start() method of an object as my Main method if there should be any problems with that let me know
ALSO NOTE : I’m transferring the game to Unity so I want to change the input+output methods with minimal changes to the code
//TurnInput is an array of bools tracking witch buttons are being pressed
//(9 buttons)
private Block[] PlayerTurn(Block[] grid )
{
TurnNotDone = false;
while (!TurnNotDone)
{
//gets stuck unity crash
//needs to wait until player click on one of the buttons
//(when player click on a button is turn is over and the turn is
//passed to the AI)
}
for (int i = 0; i < 9; i++)
{
if (TurnInput[i]) grid[i] = SetBlock("O");
}
return grid;
}
//trigger by event trigger on button gets an int for the button Index
public void PointerDown (int i)
{
TurnInput[i] = true;
}
//trigger by event trigger on button gets an int for the button Index
public void PointerUp(int i)
{
TurnInput[i] = false;
}
Upvotes: 0
Views: 3374
Reputation: 301
Perhaps you could use coroutines instead of while loop:
minimal example:
public class MinimalExample : MonoBehaviour {
public struct Block {
public bool isOBlock;
}
bool playerTurn;
Block[] grid;
bool[] TurnInput;
// Use this for initialization
void Start () {
grid = new Block[9];
TurnInput = new bool[9];
StartCoroutine (GameLoop());
}
// GameLoop
IEnumerator GameLoop () {
while (true) {
yield return new WaitWhile (() => playerTurn == true);
for (int i = 0; i < 9; i++) {
if (TurnInput[i]) grid[i] = SetBlock("O");
}
Debug.Log ("AI here");
playerTurn = true;
}
}
Block SetBlock(string s) {
var r = new Block ();
r.isOBlock = (s == "O");
return r;
}
//trigger by event trigger on button gets an int for the button Index
public void ButtonClicked (int i) {
TurnInput[i] = true;
playerTurn = false;
}
}
Upvotes: 3