Hello everyone, I’m starting to learn to do some Unity with a simple game where players change turns after selecting a card. When the turn is done, the next player get to choose the card. When the turn goes through 1 to 4 there is no problem, but when the 4th takes a card, the first player chooses automatically its card and passes the turn to the second player.
I’ve being trying to figure out what is happening, but the checking I’ve being doing tells me that the turn is being properly set, but somehow the first player gets to choose a card without pressing any key.
My guess is best to paste here the logic I’ve being doing to explain myself better. Hope the approach isn’t very nonsensical.
I have a Coordinator class that in the Update() uses this method to set the player turn, at the beginning the player turn is set to -1 to be able to choose the starting player at random:
private void SetPlayerTurn()
{
if (playerTurn < 0)
{
playerTurn = Utilities.GiveStartingTurn(numPlayers);
players[playerTurn].GetComponent<Player>().ToggleMyTurn();
}
//Ask if the player has chosen a card
if (players[playerTurn].GetComponent<Player>().TurnDone())
{
// Remove this players turn
players[playerTurn].GetComponent<Player>().ToggleMyTurn();
// Change the turn to the next player
playerTurn++;
// If the last player played, set it to the first one again
playerTurn = playerTurn >= numPlayers ? 0 : playerTurn;
// Give that player its turn
players[playerTurn].GetComponent<Player>().ToggleMyTurn();
}
}
At the Player class, while the turn is set to true, the update is listening to the players input with Input.KeyDown(). From documentation I learned that once pressed it would not return true even if it still pressed in the next frames.
void Update()
{
if (myTurn && !turnPlayed)
{
ChooseCard();
}
}
private void ChooseCard()
{
if (Input.GetKeyDown("1"))
{
turnPlayed = true;
}
}
ToggleTurn just sets if the player can choose a card or not, called when its the player’s turn or when its over.
public void ToggleMyTurn()
{
myTurn = !myTurn;
}
TurnDone is a method that is always being called by the coordinator to check if the player has choosen a card, to skip to the next player.
public bool TurnDone()
{
bool turnPlayedTemp = turnPlayed;
turnPlayed = false;
return turnPlayedTemp;
}
When checking the player turn after choosing a card from 4th to 1st, the playerTurn is set properly to 0, but when it checks if the player has pressed a key, somehow it seems to think that it did, and so the next player takes its turn.
Maybe after posting this question I get what is happening, but I’ve been with this situation for some time.
Thank you very much for your time.