0 votes

What is Events Programming?

Godot version 3.3
in Engine by (12 points)

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

1 Answer

+1 vote

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.

by (2,634 points)
edited by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.