Events vs Functions

I understand that Functions use a single process while events create a separate subprocess.

However, I cannot find how this translates into how they work:

What I need to know specifically is the following:

What happens if an event/function gets called while it is executing?

Let’s say you have 10 players in your game, but there’s a limit of 5 players to join a dungeon. So, each time a player joins you increase a counter, and once it’s full no more players can join?

The problem is that if events can be called in simultaneous, there’s a chance 2 players can trigger the event at the same time, and if they have a subprocess the counter could be 4 for both players at the moment the verification happens, and you end up with 6 players in the dungeon.

So, this is what I need to know, how exactle do both of them compare?

From what I read, ppl paint it so that functions can only be executed one at a time… like, if you execute it the game will wait for the previous one to finish before allowing your call to it. But there isn’t any actual explanation on this, so it’s purely speculation.

TLDR: What happens when you call an event while it is executing? & What happens when you call a function while it is executing?

I’m not sure what you mean by ‘subprocess’ - in terms of a process of the Operating System - calling a blueprint functions do not launch processes… And game logic happens in the game thread.

Think of multiple blueprint actors in a game: they all fire up their BeginPlay, which is a Blueprint Event. Make an experiment yourself: place your counter for example in the game mode. And each Actor can read the variable from there. The BeginPlay does get called for all of them, so it looks to you, like they are called simultaneously, but code all runs in the gamethread, so only 3 actors in the level will ‘win’.

I’ll make an example: Place 5 test actors in your level with this code in BeginPlay:

Note: You need to have your own gamemode with that Counter variable to make the example work. All 5 actors run this code, but only 3 will print the greeting message.

BTW: Calling an event while it is executing: if you have a custom event or a function defined and call it from within itself - this leads to an endless loop unless you have some exit criteria to limit the call depth.

2 Likes

The chain of nodes you can see hanging on an event will be run in one frame.

If something else calls the same event, their nodes won’t get mixed up, or multitasked. One will run before the other.

The exception to this, is if you use ‘latency’ in the node chain. Then you will get multi tasking problems, because the engine basically puts the rest of the nodes on the back burner the moment it sees latency, and runs something else.

Latency is any node with a small clock in the upper right. Delay, being a good example.

If you are talking about multiplayer specifically ( you didn’t specify that in the tags ), then other rules apply.

1 Like

Functionally (har har) there are a few differences that I can think of, but for many uses they are interchangeable.

Big differences:

Events can be called from console commands (ce, ke), functions can not
Events run in the main event graph, therefore can access timelines, and can use latent nodes, functions can not
Events can be created that connect to delegates, functions can not

Functions can have return values, as far as I’m aware events cannot (although i’ve never tried)
Functions have capability to have local variables, that are only in scope inside that function (and, bonus, it creates local variables that match all the input parameters automatically, which helps keep large blueprints from having lines all over, or making temp variables, as you might with an event)

… if none of those situations apply, they are pretty much interchangeable.

oh, multiplayer probably has some fun dealings with functions vs events, but i haven’t actually done anything multiplayer in a bit over a decade… so… not real sure on what’s up in there these days.

Here’s some table which I started to create quite some time ago, not finished but maybe useful, so I share this one here.

Function Event Macro
callable from outside of blueprint Y Y N
can contain latent nodes (e.g. delay, timeline) N Y Y
can return values Y N
wildcard variables Y1 Y
Multiple (output) execution pins N Y
Call to Event Dispatcher possible from within Y N
Bind/Unbind event possible from within N
Use with no execution pins Y2 Y
Can use local variables Y
Can be replicated N Y
Child class override Y N
Call Bind event Y N3

1 C++ UFUNCTION with CustomThunk for example allow to create wildcard array inputs.
2 use pure functions
3leads to “Bind Event to ED_endreached cannot be placed in macro graph”

Hi @ClockworkOcean, thanks for pointing this out. My table is quite old, from my beginnings with UE4. I updated the table. Would be great to get that table filled completely and rechecked. I’ll see if I can rework this. Any input is highly welcome.

3 Likes

You can have a function without execution pins :wink:

1 Like

sure can. In the function properties, select “pure”. But also make darn sure that it actually is pure, that it doesn’t rely on any outside variables, or perform any side effects on anything outside of the function.

Also, make sure you don’t drag more than one pin from it, otherwise it gets called more than once… :slight_smile:

Fun fact, a function without a return value is treated as event, you can tell by the icon.

1 Like