What is the difference between sending a message and calling a function?

OK so… from programming I know the difference between these two things. Sending a message means that anything you send it to may handle it or not depending on whether they implement a listener. On the other hand a function must exist to be called.

In blueprints I’m less certain what the difference is.

I added an interface to my Character class. When I try and access that interface I get a function call from the character class and a call to the interface from the AI controller. Both seem to do the same thing. What is the difference between these two things and why are they differently accessible?

I would expect to be able to send the message through the character class too… but you can’t. or maybe you can but it’s not in the context sensitive OR non-context sensitive menus?

Calling a function is basically executing code which you know is there in the object you are calling. Sending a message is asking the object to please execute a method you think it might provide. When you have the type of the object and that type provides the function I think the function call should be slightly more efficient. When you have a type which may or may not implement the function then you need a message.

The reason that both exist is basically because making Blueprint and C++ work together is complicated. See Grand unified C++/blueprint cast interface explanation - UI - Unreal Engine Forums for a longer discussion.

Calling a function is basically executing code which you know is there in the object you are calling. Sending a message is asking the object to please execute a method you think it might provide. When you have the type of the object and that type provides the function I think the function call should be slightly more efficient. When you have a type which may or may not implement the function then you need a message.

The reason that both exist is basically because making Blueprint and C++ work together is complicated. See Grand unified C++/blueprint cast interface explanation - UI - Unreal Engine Forums for a longer discussion.

A function is always going to be a lot more efficient in terms of code path length. Let’s compare to the two, what has to actually occur?

Everything here, is from an Assembler point of view, when I say instruction, I mean exactly that, there is in truth, no 1 to 1 relationship between assembly, and “high level languages”, for near every statement will generate 10’s if not 1000’s of assembly instructions. Many C compilers, actually generate assembler, and then have the assembler actually make the binary object.

Function:

Recoginize that a function is going to be called
Set up the Stack (i.e. information needed to return, passing paramerts (by reference or value)
Making the call (this is like, geezus, all of one instruction)
Your done.

Message…
create the “message”
Set up the stack
Make the call to put the message into some form of queue, for delivery to all the available targets that are listening
Targets get the message (i.e. a function call, to enter into the target’s “code space” if you will
process the message
Deliver the message to the next target, ad nauseum
If the message is to actually generate a response, the we have to do this all over again, because no one knows who anyone is.

With a function, you know what is happening, with a message you can never be sure, unless your the one that is writing the underlying “messaging” system, or as with the Unreal engine you have access to the source code (gratz to Epic Games!), and have the time to look at the messaging system.

So as you can see, the message process is is a superset of the function process, some kind of function call (either to directly execute the code that you want to fire, or to just get a “message” into a queue for delivery) is going to ocurr. It just depends on your needs, and how much overhead your willing to force upon the game engine.

Anyway, just some more thoughts on the topic.