What's the difference between Blueprint Macros and Blueprint Functions?

They both seem to do the same thing in the end. When should I use a function and when should I use a function?

10 Likes

This may not be the answer… I’m using a common sense approach but

Macros are like a series of functions or a bunch of instructions tied together to activate an outcome or shortcut quicker.

A function does, well a function, one particular thing. Like the function of a toothbrush is to brush teeth…

1 Like

As 310Thirty said, the difference is basically that Macro is a collection of regular Blueprint nodes that you can use easier than reinventing the wheel everytime. But a function is very similar but still very different as you have many other things like replication and other stuff that is only available to functions. And they are used to get something done within that function. Macros can be anything from just a small “shortcut” to something massive but it’s still just a collection of nodes for easier use.

Functions is used all the time in code, so it’s easier to understand if you had come from a coding background. So is also a lot of the terminology in Blueprints as it’s basically a visual coding interface. Or more commonly said, it’s visual scripting.

//Sparven

1 Like

Thanks! Knowing that function have stuff like replication makes it a lot clearer. I also notice that macros can be put into a macro library and used across many different blueprints which you cannot do with functions.

I guess there are probably a lot of cases where you could have used a function or a blueprint and it wouldn’t make a lot of difference.

Great! Please use the “Solved”-button next to my answer if you think this is solved. It’s great if you do so, otherwise everyone will see this as unanswered.

I think I have? Or perhaps I didn’t do it right. D:

4321-in-editor+multiplayer+test+only+works+on+one+client+window+-+ue4+answerhub+-+opera.png

That green checkbox is the one you are looking for :slight_smile: The answer you choose is then highlighted in green when its done the correct way :slight_smile:

Great! No problem :slight_smile:

Got it! Thanks!

A function is a collection of nodes compiled into a single individual unit, that can be called over and over from other places. A function does not support calling latent nodes like Delay.

They also only have one input execution wire, and one output execution wire.

A macro on the other hand, is basically a template for nodes and supports both latent functions as well as multiple input and output execution wires.

Unlike functions, which is compiled to a single unit, which is then called again and again, a macro is compiled as if you inserted all its nodes directly into your blueprint. This also means, that if you don’t use your macro, nothing will be compiled.

A real world analogy (of sorts) would be, if you needed to make 10 cars, a function would be one single factory which you then tell to make a car, 10 times. You get 10 cars, but all produced by the same single factory. With a macro on the other hand, you would build 10 factories to produce each car.


So, to sum it up:

Function

  • Represents a single unit that is compiled once, and can then be called over
    and over.
  • Does not support latent nodes.
  • Has one input execution wire, and one output execution wire (Unless pure).
  • Can have multiple inputs and outputs.
  • Calls to functions can be replicated in network games.

Macro

  • Represents a template of nodes, where each reference to it will be replaced by the actual nodes at compile time, and thus is only compiled if used.
  • Supports latent nodes.
  • Can have multiple input execution wires, and multiple output execution wires (Including no execution wires at all).
  • Can have multiple inputs and outputs.
  • Cannot be replicated, as they aren’t “tangible”.

Let’s say you have a function called GetCircleCircumference, defined like so:

And your event graph looks like this:

When you compile, your function is compiled to a single unit (one single factory), and your event graph tells that single function to calculate the circumference of a circle 5 times.


However, if you instead use a macro, defined like so:

And much like before, your event graph uses it to calculate the circle circumference 5 times:

You have much the same behaviour, but instead of having one single compiled unit you call over and over, all references to the macro are replaced by the contents of the macro, and thus, what is actually compiled, is this:


The fact that macros are inlined wherever they’re used, also means that when you don’t use them, nothing from the macros is compiled and thus, if it contains errors you won’t get any messages about it.

It is also important to keep in mind, that due to the inlining, errors you get in macros, are actually caused from where you use them!

For instance, consider the following macro called GetWorldLocationOneMeterInFrontOfComponent:

It does exactly what it sounds like. Takes a component as input, and outputs the location one meter in front of it.
If we compile now, nothing is actually compiled from the macro, as we’re not using it.

Now, say we use it to print the location when the game starts:

When we hit compile, the compiler outputs an error inside the macro:

As we can see, the Target inputs are connected to the input; however, due to the fact that it’s inlined, the error is actually occurring where we use it, like it would, had we used the nodes directly there. It is in fact compiling to the following, which makes it clear why we get the error:

Wiring the input of the macro where we use it, will satisfy the compiler:

Which it then compiles to:

And of course, if we had used it in two places without wiring the macros’ input, we’d get 4 compile errors of missing connections.


Hope that helps you understand the difference between functions and macros.

PS: You can right click on a macro and choose “Expand Node” to inline it directly in your event graph (Which is essentially what the compiler does to all macros before compiling).

26 Likes

Holy wow. In depth answer is in depth! Thank you muchly, good sir! :smiley:

1 Like

A function can take parameters, and can return multiple values, hence they are far more flexible than Macro’s.

To find the parameter set up, just click on the entry node to the function, and then look in your details panel, there you can create as many parameter’s and return values as you wish

A macro, and all macro’s are truely nothing more, than saying,

I want to take this sequence of statements/nodes, and give it a name, and where ever “you the compiler” see this, inline the macro and compile it up. So long as the macro’s are kept to a reasonable complexity, they are handy. But for breaking down a problem, and being able to compartmentalize, etc, functions to me, are far better. Some say it’s a matter of taste, I argue, it’s also a matter of code isolation.

Macro

Good things: is easier for the programmer
Bad Things: harder on the computer, unless the macro is only used 1 time, and if it’s only used one time, why did you bother making a macro

Function

Good things: is easer on the programmer,
keeps the compiled code size small
easier on the processor, will not create as many page faults, doesn’t need as much memory, tends to stay in all the caches far more

This is not to say a macro shouldn’t be used, it definitely has it’s place in coding. For instance, if we know that we are going to be doing a lot of loops, and the loops are are taking the basic same form of input, i.e.
Start index, End Index, etc. Then why not create a macro, to do that, for we are going to have to do that anyway, so it’s easier on the programmer.

When not to do it, when the macro is going to be humongous, and used all over the place. Because the compiler is going to “inline” (i.e. replace that one blueprint nodes, with all the nodes that are in the macro) the code. This can lead to a lot of code bloat, because the compiler is not going to see it as a macro. The first scan over the tokens/nodes, is going to expand each macro, making it far easier on the lexical analyzer, etc to deal with it. In this fashion, whether the code has macro’s or not, it all looks the same to the lexical analyzer, or tokenizer, just a stream of statements/blue print nodes.

So if you will, a function is actually a part of the processor, in that there are instructions for dealing with functions. A macro is a part of the “editor” in that it’s a convenience. And the core compiler, and certainly the processor, has no idea what the hell a macro is.

One problem I found with macros is that they don’t seem to allow calls to event delegates.
Try collapsing a call to event delegate into a macro. It should fail.

Functions don’t seem to have this limitation.

Sincerely

Thank you sir, very informative and still currect(as of current and correct) in 2016.

Wow… one of the best posts I’ve seen on here… Thx so much!

This is an excellent thread so I thought I’d add one more thing that hasn’t been mentioned and further clarifies the difference betweem macros and functions - macros can’t have local variables.

I ended up here after reading through the page on “nativization” because it says that macros cannot be “nativized” …just thought I’d add that since it’s important for optimization.

Actually, that’s a wording error marc. They meant macro libraries. I recently tested this out and yes, macros within blueprints nativized just fine.

why functions cant support latent nodes?