Events Programming

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By mikedesign

What is Events Programming?

What are you trying to find out with this question? It’s really vague.

exuin | 2021-05-15 17:59

:bust_in_silhouette: Reply From: Lopy

Event programming is a programming method that is not unique to Godot, but Godot does cater to it.

The basic idea is that instead of having a continuous list of instructions, you set up starting points that are triggered when something happens. In Godot, those would be:

  • Time unit (tick) elapsed: overriding _process and _physics_process
  • User input: overriding _input, _unhandled_input, _gui_input and _unhandled_key_input.
  • Object specific: connecting to files_selected on a FileDialog, completed on a GDScriptFunctionState, etc.
  • Following tick: calling a function through call_deferred, or pausing a function until the next frame with a yield(get_tree(), "idle_frame")
  • Etc

The main advantage of this method is that you do not need to periodically check if the thing you are looking for changed, since instead you asked to be informed when it changes. This is especially useful if checking is computationally expensive or cumbersome.

The disadvantage, is that the program becomes more complex, and sometimes harder to debug. Events might happen in an unpredicted order, and it can be difficult to track the path of execution. For instance, print_stack on a function called through call_deferred cannot, by definition, give you the stack of the original function (although you could pass it as an argument with get_stack, always empty outside debug run).

To alleviate this issue, you can try to make sure every starting point leaves the system coherent without relying too much on other ones. State machines can also be used to armor critical parts against unpredicted events.

Further reading: godot signals, user input tutorial, Wikipedia category.

PS: Vague questions are not forbidden, but you (personnaly) want there to be something people can answer, otherwise it will just sink in the great ocean of old unanswered. The title is good, the section could be Gossip, but Engine works fine. For general info about asking questions, this site has a somewhat hidden README.