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
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!
Macros are a bit overrated as they bring more problems than offer real benefits.
They’re clunky to copy/duplicate if you need to reuse them across many classes.
They also get in the way when you need to set Breakpoints and Debug BP code!
Overall they may hide bad habits and compile out to copy & paste code anyway.
So its often easier to just build games using Custom-Events + Functions only!!!
Function-Libraries offer more benefits versus macros too, as they work globally.
If you need function-ish features but with Delays, then Macros are the only way.
Functions can be Pure unlike the Filthy Macros.
But there’s a greater evil out there hardly anyone dares talking about - The Collapsed Graph.