-2

I'm wondering if it's possible to store keyboard input history into a array and execute them at a later time? Basically I have a 3D grid game and a character moving around the grid. I want the user to enter all the movement instructions via right down left up keys while the player remains still, and once I press enter the player will begin to execute each instruction that is stored into the array. any suggestions?

Arun Bertil
  • 4,598
  • 4
  • 33
  • 59
  • Yes - follow exactly what you have in the post and it should work. What exact problem you have implementing it? – Alexei Levenkov May 04 '15 at 05:04
  • sorry I wasnt very clear. Ive been looking around the API and couldnt find exactly what i need. like which function should i call to execute each instruction when im looping through the array? – Some_Tasty_Donuts May 04 '15 at 05:54
  • Store entries in a List collection. Use the collection as a FIFO. Add new entries at end of list. Remove entries at item zero. You can also use a StringBuilder which is a type of List. – jdweng May 04 '15 at 06:23

1 Answers1

1

I would recommend using a List<T>

Example:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Training : MonoBehaviour {

public List<KeyCode> previousActions;

void Update(){
    if(Input.GetKeyDown(KeyCode.A)){
        Debug.Log("Do something");
        previousActions.Add(KeyCode.A);
    }else if(Input.GetKeyDown(KeyCode.S)){
        Debug.Log("Do something else");
        previousActions.Add(KeyCode.S);
    //---Check the list--//
    }else if(Input.GetKeyDown(KeyCode.D)){
        Debug.Log("Check the list");
        for(int i = 0;i < previousActions.Count;i++){
            Debug.Log(previousActions[i]);
        }
    }
}
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179