I've been working with Unity and noticed something interesting in an example scene. When I press the spacebar, the input manager simply sets a public variable to 'public bool jump = true'. Then, the character checks this variable every update to determine if it should jump. I'm wondering why the Unity dev team opted for this polling method instead of using a callback like 'ExecuteJump'.
1. Is using a callback not more efficient? Why would a team of experienced developers choose polling instead?
2. What's the rationale behind using polling? It seems like callbacks would always be a better option performance-wise. Are there specific cases where polling outperforms callbacks?
3. While I understand that polling can complicate things if the calculations are trivial, could design patterns like the observer pattern help streamline the implementation?
3 Answers
Not everything needs to focus on efficiency. In many cases, keeping the system simple and predictable can lead to better outcomes down the line. Polling is generally lightweight and integrates nicely with the frame-based model of Unity.
Polling is simpler in many scenarios. For one, you know exactly when the polling occurs and the order in which things are checked, especially if you have multiple inputs. With callbacks, it’s a bit messier since states might trigger the callback at different times, requiring you to track various state changes. Also, when polling happens once per update, you can avoid issues where the callback state might be activated multiple times within that frame. If you have a complex state to check, a single centralized polling can be more efficient than multiple callbacks firing off for every action during an update.
In Unity, polling aligns perfectly with the frame-based update system. Since most gameplay mechanics run every frame, checking input states in that cycle is manageable performance-wise—essentially free. Callbacks, while handy for one-off events, can create timing and ordering issues if they fire at the wrong moment. Polling keeps input handling separated from other processes, which is a cleaner approach.

Related Questions
How To: Running Codex CLI on Windows with Azure OpenAI
Set Wordpress Featured Image Using Javascript
How To Fix PHP Random Being The Same
Why no WebP Support with Wordpress
Replace Wordpress Cron With Linux Cron
Customize Yoast Canonical URL Programmatically