So I wanted to use UniRx in Unity to try reactive programming. I gave myself a simple task. Have a stream for key presses for W A S D keys, print to the screen whenever any key is pressed, and display a Combi happened
whenever the sequence WASD happens.
The problem is that if the sequence appears EXACTLY at the beginning of the stream then it is not "recognized" and no value gets emitted.
Here is what I mean:
Here W A S D is pressed first.. first combination is not recognized, but all subsequent ones are.
Here I press a different letter at the beginning then do the pattern, everything works as intended.
Here is the code:
public class TestController : MonoBehaviour {
// Use this for initialization
void Start(){
var tick = this.UpdateAsObservable();
var w = tick.Where(_ => Input.GetKeyDown(KeyCode.W)).Select(_ => "W");
var s = tick.Where(_ => Input.GetKeyDown(KeyCode.S)).Select(_ => "S");
var d = tick.Where(_ => Input.GetKeyDown(KeyCode.D)).Select(_ => "D");
var a = tick.Where(_ => Input.GetKeyDown(KeyCode.A)).Select(_ => "A");
w.Subscribe(Debug.Log).AddTo(this);
s.Subscribe(Debug.Log).AddTo(this);
d.Subscribe(Debug.Log).AddTo(this);
a.Subscribe(Debug.Log).AddTo(this);
var keys = Observable.Merge(w, a, s, d);
var Combi = new[]{"W", "A", "S", "D"};
var combiFound = keys.SelectMany(keys.Take(4).Buffer(4))
.Where(list => list.SequenceEqual(Combi));
combiFound.Subscribe(_ => { Debug.LogWarning("Combi Happened!"); }, Debug.LogException).AddTo(this);
}
}
What exactly is happening here?