How to create macro in c++?

Well, I want to return multiplie exec’s but I guess I can do it other way, so thanks for the information. :smiley:

Hello!

I am writing Blueprint project in c++ and that blueprint has some macros in it. So my question is how can I create a macro in only c++ like in bp? How can I put in it for loops, functions and things like that? (Sorry if this is a dumb question I’m still learning c++)

Hi Ɛdmm

Macro’s in blueprints are different from macros in c++. Basically, you cannot write a blueprint-styled macro in C++.
In c++ macros are instructions you can give the compiler about how to interpret your code. In Blueprints, macros are reusable event graph snippets that will get unpacked once it is compiled.

What is it you’re trying to achieve exactly?

Ahh I see. You can achieve something similar using ENUMS and adding the “ExpandEnumAsExecs” meta specifier in the UFUNCTION macro.

There is another thread that has a detailed response.

Thats false, #define directive is equivalent of blueprint macros that paste snippits of defined code in place of use and it also supports arguments (outputs kind of too as you can declare variables in macro that will be valid in later code as long as you do it in same scope).

In fact UE4 extensively use macros, GENERATED_BODY() is used to paste generated code inside that user written class which generation there would be pain to implement considering user can do anything there, IMPLEMENT_PRIMARY_GAME_MODULE() is used to paste ready module class without you even need knowing what it is (main reason any people are not aware of modular structure of UE4 and discover that when they start making plugins), DECLERE_DELEGATE() paste delegate decleration code that would be pain to write and write over again so the made it simple one liner. UE_LOG() is used with preprocessing conditionals (#if #endif) to remove any log code from binaries to build configurations that does not include logging, something you can’t do in blueprints as blueprints don’t support preprocessing conditionals. stat system also using macros for same reason. Stats system does the same for exact same reason

UFUNCTION and UPROPERTY etc. that might confuse you is actually macros only to make C++ compiler… omit them :stuck_out_tongue: in reality they empty macros pasting nothing, as they are only used in complitly different tool called UnrealHeaderTool which generates extra C++ code based on those markers, so you don’t need to register all function and properties manually to reflection system.

Because C and C++ are more flexible the blueprints macros are more rarely used and you kind of achieve similar results using inline functions if you just want to paste procedural code snippets (if you want to paste declarations you need to use macros). That said UE4 by convention don’t expect you to define your own macros.

1 Like

Oooh, I understand now. Thanks for that good explanation! :smiley:

Hi

I wasn’t really clear with my answer. I just wanted to quickly sum up that you can’t really create a blueprint macro inside c++ as they are read differently by the compiler. Yes of course you can use c++ macros to insert / replace / structure code.

The Cherno released a good explanation about macros a while ago: Macros in C++ - YouTube

Macro is specifically referenced to code replace created by define statement by preprocessing, ofcorse C++ preprocessor can do more fancy stuff ;p but most basic and core function is to paste code text to specific point (they even say that on your video), same as most basic function of blueprint macro is, bah in C++ it it goes even more basic as they most commonly used to hold configurable consts on compiler level.

Dont know how UE4 VM compiler deals with it, but that most basic function is to paste code fragment on specific point and what blueprint macros does is 100% doable with C++ macros because pretty much they do the same thing at it’s core.