Hi, I am creating a game which involves the player planning his actions before initiating them.
I have a smaller “ghost” object which is controlled using basic translate position buttons.
What I am wondering is if there was a way to get my game to remember which buttons where pressed and in what order?
The hopes is that once the player has planned out his string of actions they will press an “Action” button which will perform the actions for the player in the sequence they had chosen but on the real player object, not the ghost object which they used to plan their turn.
I hope that made sense.
You could use a Queue since you probably want to execute the first pressed action first. Then you need to decide in what type you store the inputs (strings, ints etc). Assuming you use int this would mean:
- Button1 = 0
- Button2 = 1
- and so on…
Using C# this would look something like this:
// Initialize the queue at start
Queue<int> userInputs = new Queue<int>();
...
// On user input (nuberOfInput is of type int)
userInputs.Enqueue(nubmerOfInput); // Enqueue(0) for Button1
...
// On "Action"
nextButtonNumber = userInputs.Dequeue();
Note Enqueue()
puts one element in the queue and Dequeue()
removes and returns the first element in the queue so you have to call it repetitive.
Edit Sticking to the queue attempt (maybe not the most elegant):
public class PlayerController : MonoBehaviour
{
private Queue<int> playerInput = new Queue<int>();
public void MovePlayerForward()
{
playerInput.Enqueue(0);
}
public void MovePlayerBackward()
{
playerInput.Enqueue(1);
}
public void MovePlayerLeft()
{
playerInput.Enqueue(2);
}
public void MovePlayerRight()
{
playerInput.Enqueue(3);
}
public void RotateRight()
{
playerInput.Enqueue(4);
}
public void RotateLeft()
{
playerInput.Enqueue(5);
}
public void OnActionButtonPressed()
{
while(playerInput.Count > 0)
{
switch(playerInput.Dequeue())
{
case 0:
transform.Translate (0, 0, 1);
break;
case 1:
transform.Translate (0, 0, -1);
break;
case 2:
transform.Translate (-1, 0, 0);
break;
case 3:
transform.Translate (1, 0, 0);
break;
case 4:
transform.Rotate (0, 45, 0);
break;
case 5:
transform.Rotate (0, -45, 0);
break;
}
}
}
}
You can also put the while in a coroutine or adjust it to fit your needs because now the actions happen pretty fast. Or if you want it turn by turn just remove the while-loop.
In case you also need to record the times between the button presses let me know.