What the difference between functions and macros

Could you explain please in which cases I should prefer macros over functions

prefer macros over functions

  • you need more than one execution wire
  • you couldn’t care less about local variables / scope
  • you wish to take advantage of latent actions, like delays

And here:

2 Likes

wow! thank you! especially for the link

“Macros” are a timesaving tool that epic has provided for convenience. It’s really just there to reduce the amount of copy-pasting required and to keep your graphs looking nice and tidy.

“Functions” are an important concept in programming (not just in Unreal, but in C++ and most other languages) that have very specific rules for how they can be used, but in turn have a lot of really powerful abilities that macros do not.

Arguably the most important ability of functions is that OTHER blueprints can see and use functions on your blueprint, but they cannot see or use it’s macros. This means if you made a blueprint Car and a blueprint Driver, the Driver could say “Car! Use the MoveForward function!” and it would actually do something.

Another important ability is that different blueprints can have functions that do the same thing but in different ways. For example, a Car blueprint and a Horse blueprint could both have a “MoveForward” function, but what’s inside those functions would obviously be much different. When the Driver blueprint came along, it wouldn’t need to know the difference, all it has to do is say “MoveForward!” and the functions would work even though they do quite different things.

If you’re working in a level blueprint, or just trying to make a bit of easily reusable script (rather than copy paste a bunch of nodes around), a macro is your guy. If you need your blueprints to do some fancy stuff like I described above, you’ll need functions.

Hope this helped!

1 Like

Functions can be Pure :innocent: unlike the Filthy :angry:Macros.

But there’s a greater evil out there hardly anyone dares talking about - The Collapsed Graph.

1 Like