Event vs direct function call approach

I was wondering how events such as app.fire and app.on works
When I fire an event, what happens, is every entity with the script that has an app.on listing or?

For optimization purposes, what’s better to use, events or approaching direct to script through entity (script.scriptName.function)

Hi @Dava,

You can check the implementation of the fire method in the pc.EventHandler class, posting the link below.

I’d say in terms of raw performance using directly a function on the script instance would be faster. Though in most cases this would be a micro-optimization, quite useful if you calling it e.g. per frame.

When calling events, on app or on an entity, basically what happens with this class is a search on a list for all event handlers that have subscribed to it (using on) and executing that method.

1 Like

To add on that, in many cases pure code clarity can be equally or even more important than raw performance.

Events on game based environments where a lot of behaviors interact with one another can be super useful to be productive.

1 Like

Thank you @Leonidas , this was useful

1 Like