- Home /
Buffering button inputs
How can I buffer button in put in Unity? Say I have 3 buttons on screen; attack, run and hide. When the user press [attack], then [run] then [hide] buttons, I'd like to call attack()
, then run(
) finally hide()
with yield WaitForTime(Random.Range(1.0f, 5.0f));
in between. I thought I could use coroutine, but it doesn't seem that way.
Could someone give an insight?
Answer by sfc.itzhak · Jul 15, 2013 at 07:14 AM
you can always create and mangeing array that will keep the action and will excute them in thier order, somthing like this : //remamber that when you add another action you need to invoke the PreformActions
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ActionsManger : MonoBehaviour {
public bool isDoingActions = false;
public List<string> ActionsToDO = new List<string>();
void Start () {
ActionsToDO.Add("attack");
ActionsToDO.Add("hide");
ActionsToDO.Add("run");
StartCoroutine(PreformActions());
}
IEnumerator PreformActions()
{
if (!isDoingActions)
{
isDoingActions = true;
while (ActionsToDO.Count > 0)
{
Invoke(ActionsToDO[0],0);
ActionsToDO.RemoveAt(0);
yield return new WaitForSeconds(Random.Range(1.0f, 5.0f));
}
isDoingActions = false;
}
}
void attack()
{
Debug.Log("i am attacking");
}
void hide()
{
Debug.Log("i am hiding");
}
void run()
{
Debug.Log("i am running");
}
}
good luck ,
sfc
Aha!StartCoroutine and List! StartCoroutine starts a function that checks the length of the array. When the array is greater than 0, invoke the function in the List. However, your algorithm always removes the first entry of list, not the rest. Could you explain?
when you take the first [0] entry you you remove it from the list so i remove 1 then 2 becomes 1 , 3 becomes 2 ans so. so this way you take out every function of the list.