Is is possible to set macro definitions in an Unreal project at build time?

I am looking for a way to easily define macros at project build time in an Unreal Engine project.

For example, if I want a defined C++ macro MY_BUILDTIME_VAR to be 0 in certain builds, and 1 in others, without having to modify source code every time, in a similar approach to environment varibles in many tools (i.e. NODE_ENV in node.js).

I am aware it’s possible to define target-wide macros in a project’s target.cs with GlobalDefintions.Add("MY_TARGET_VAR=0") or module-wide macros in a module’s Build.cs with PublicDefinitions.Add("MY_MODULE_VAR=0"). It helps to have all things defined in one place, but those definitions are still baked in the source file, and can’t be changed at build time.

The official documentation mentions several ways of building/cooking an Unreal project with custom parameters, such as the Project Launcher tool and the RunUAT script file. Do any of them allow for build-time macro definitions?

1 Like

Did you manage to solve this issue?

No, c++ macros are processed right before compilation. These macros literally include/exclude code entirely based on their value when being compiled. The excluded code doesn’t exist anymore at compilation and after. It’s not like global variables (or CVars) which you can toggle at runtime.