• Nothing
  • About
Menu

Asher Einhorn

  • Nothing
  • About
polling-help.png

Event driven systems vs Polling

June 18, 2015

The following is part of an ongoing series of basic tutorials for gameplay scripters.

Event driven systems vs polling

Simply this is the difference between code that checks or ‘polls’ every frame to see if a condition has been met, and code that is called only once, when a condition triggers a function to be executed.

Let’s look at an example of a damage system.

Polling

We run a check, every single frame to see if any bullet’s position overlaps with the player, and if it does we subtract some health.

Event Driven

The function used to subtract health is only ever called when the bullet has hit the player. When the bullet hits them, a check is run to see if the target has health (so for example, is not a wall) and if it does, we call the relevant damage function.

Where possible event driven systems are far better because they reduce function calls drastically, and are far neater. If you find your script has to be called every frame, consider instead how you can change your code to be triggered on an event.

One thing to remember is that even things that seem like they should be checked every frame can often be event driven.

Take the example of a trigger box. You would think as it needs to check for the player consistently, it should be polling the world for them constantly. However, if you think about it, triggers only really need to query for collision on everything that’s moved significantly. If the player is still, it may be that it is unnecessary to check them. A player only trigger can therefore be tied to the players

In Scripting Handbook
← Scripting languages vs programming languagesMessage sending vs function calling →
Twitter
  • RT @CKapmeh: Sunday Capilla San Bernardo / Nicolás Campodonico https://t.co/V56q7uftPf
    May 28, 2017, 10:42 AM
  • Crunch dadaism. Come to @Naughty_Dog and experience the highs of creative culture. https://t.co/9yMPcTdiYA
    May 27, 2017, 9:33 AM
  • Crunch garden #gamedev Avocado, Lemon and Peppers. From right to left. https://t.co/jtZXeWDEgh
    May 27, 2017, 9:31 AM
  • Working with a migraine two days in a row is quite possibly the most unpleasant thing in recent memory.
    May 26, 2017, 3:36 PM
  • Sure is fun to have a night off! #PS4share https://t.co/WMcEMY5yWE
    May 21, 2017, 7:59 PM
  • The EU/US PSN account conflict is an ongoing nightmare. Looks like I'm not playing anything tonight after all.... pain!
    May 21, 2017, 7:49 PM