What is a macro and what is it actually used for?

Hello everyone, I am currently learning blueprint via paid training from Evans Bohl for those who know, and I was wondering what a macro is actually used for and what is the difference with a function?

They look very similar on the surface, but there is a fundamental difference: All the macros in your code get replaced by the actual code before the game is run ( just like a text replacement ), whereas only one copy of a function is loaded, and any code that calls it is routed through it at run time.

There are a lot of other differences, for example

1 You can have multiple entry and exit execution pins on macros

2 Functions handle short circuit exiting more gracefully

3 You can write recursive code with functions ( not recommended ), but not macros

4 You can’t have ‘latency’ in functions ( like delay nodes )

5 etc…

I’m sure someone else can chime in.

Ok thanks, but what are macros actually used for?

Making re-usable chunks of code, just like functions :slight_smile:

They are used for convenience and to make code easier to read. The For Loop, Do Once, and Gate nodes are all macros, you can double click them to open them up and show what they look like. They all have multiple exec/exit pins so they can’t be functions.

They are used, amongst other things, for things like UPROPERTY() -which is a macro - etc… that the engine uses to expose variables from your C++ code and make them visible inside the Editor.

Thanks to the arcanery of these macros, we can have our “int x;” typed within the source code showing up as a nice little box inside of the Blueprint Editor, for example.

Note: the convention is to write the whole thing UPPERCASE, so if you see something all capped, that’s likely a macro.

EDIT: just noticed this was under the blueprint section, my bad