While trying to figure out how to implement platform agnostic input handling for a platformer, I've discovered the command pattern and I liked the idea so I wanted to take it a step further.
I got the idea of having a Command Queue System (I'm using ECS in my gameplay screen/state) which would expose a simple api for enqueueing commands which other systems could invoke and then the command system would execute those commands within a logic loop derived from Fix your timestep blog post.
While processing and implementing this idea, however, I've encountered a couple of issues where I'm not sure on how to proceed:
- Input processing is currently done with an abstraction over LibGDX's InputProcessor so input processing is event-driven and thus commands are queued as fast as events are emitted. This in turn causes the queue to fill up the command queue with more and more commands(40-50+) waiting for execution which, along with filling up memory, causes longer and longer delays. I've tried to mitigate this by removing oldest commands from the queue while the queue size is > 10 but that made gameplay really choppy since command removal was on the same thread and is blocking everything else until it's done(at least that's what I'm assuming is going on).
- Is a complement to issue 1. wherein if I use a single queue for all commands including all AI NPC's, enemies and the player, I only see massive delays while the systems processes all inbound commands. I could spin up new threads for every entity but then I'm not sure how could I get them to execute within the fixed timestep logic loop, and I'm not particularly fond of having dozens of threads for every single entity.
I really like the idea of having movement and interaction of characters represented as commands but I can't figure out how to implement this without greatly losing precision and performance. Can anyone steer me in the right direction?