They are similar in:
- I double-click function, it tells me what’s it gonna do; I double-click event, it also tells me what’s it gonna do.
- function can be used outside, events can be used outside.
What’s the main difference between them? When should we use events, and when should we use functions?
Functions have output nodes.
“Functions are guaranteed to execute
and return immediately by limiting
what kinds of nodes can be placed in a
function (latent actions, timelines,
etc… are all prohibited).”
Taken from here.
“…events like its name are a
starting point of any independent
tasks in a task queue system, that’s
why it was given Red color(most easily
spotted even zoom out of graph). And
call event node will have no return
values, which means you can’t and not
allowed to depend on this event call’s
result, you fire it and it will do
it’s thing, just wait and profit.”
Taken from here.
Thanks man! But if so, what’s the point of using events? Looks like functions can do anything, while events have limitations.
I am not a programmer, but here is my uderstanding of it. Events are allowing you to respond to certain, well, events in your game, like your player hit an enemy, you pressed W on your keyboard, so on. You can set up for example an event “W Pressed” and any nodes (including functions) will be executed after that event node when the player presses W, allowing you to decide what should happen in the game when a certain thing (event) happens.
A common example I hear a lot is to think of functions as car factories. If you want to make one or two cars, you don’t build a factory, it doesn’t worth it. But if your are planning to make a lot, it might be a good investment. Same for functions, if you want to add A+B in your game only once in your entire code (quite unlikely tho :P) you wouldn’t make a function, it is easier to just add them with the ADD node. But imagine if you want to make a calculation more complex and you want to use it all over your game, then you should use functions. You can set input values and output values for functions, meaning you can execute the same code inside them but with different values (A+B example: your function would have an ‘A’ and a ‘B’ input and an ‘A+B’ output and in your function you’d add A to B). This would allow you to get the result of A+B any time, no matter what actually A and B means, the function will run with your desired values. As mentioned above, functions don’t allow latent nodes, because they have to return a value immadiately.
I hope it clears up at least a little bit, but take everything I say with considerations, because again, I am not a programmer
Is there anything which events can do while functions cannot?
They can have latent nodes such and Delay, Timeline etc. while functions cannot, and in general, they are for different things.
Why u’d say they are for different things?
As their name suggest, Events are for handling different events in your game. As an example, let’s say you walk into an area that hurts you, and you define that area with a volume. Then you can set up an Overlap event, and when the Player walks into that volume the event will fire, allowing you to respond. You most likely want to call some functions to reduce the health of the player and maybe update his HUD, up to you. Basically you want to your events to check for certain things in your game and when that condition met they will execute all the nodes behind them (most likely you want to execute different functions)
This is an interesting topic and I hope UE staff can give the perfect answer. The answer in this thread is not really helpful.
It isn’t distinctive if you use it in Blueprint. But from a programming standpoint, events are executed in a separate thread (subprocess), while functions are executed in the same thread.
So in computer, every execution flow is considered to be 1 thread. A collection of threads, is called a process. And a collection of process is called an application (it’s called a program during the DOS age).
So if you call a function, then the same thread that you use to call it, will execute the instructions inside that function. But if you call a custom event, then UE will generate another thread to execute the instructions inside that event. So you’ll have 2 executing thread that runs in parallel at the time you call an event. That’s why, you are not allowed to depend on the result of an event execution.
UE staff, please CMIIW about this.
This is the greatest anwer ever! Thank you , Pelangi!
This is what a great answer I have ever seen. Clearly.
That was the greatest answer. You are the greatest human. Thank you, human.
You should consider picking it as “Accepted” so it goes on top