3

I am looking for a way to easily define macros / preprocessor directives at project build/cook time for 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), or to the command-line approach of i.e. g++ (see here).

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?

Note: I am not using MS Visual Studio, but JetBrains Rider with .uproject only, so I can't define them in an .sln solution file.

goose_lake
  • 847
  • 2
  • 15

1 Answers1

1

Use an environment variable and read it in your build.cs file. Based on the environment variable set the value of your macro.

This is a handy utility method I use for this purpose:

private void AddEnvironmentVariableDefinition(string variableName, string defName, string defaultValue = "0")
{
    string value = System.Environment.GetEnvironmentVariable(variableName) ?? defaultValue;
    PublicDefinitions.Add(String.Format("{0}={1}", defName, value));
}
Rotem
  • 21,452
  • 6
  • 62
  • 109
  • Thank you. Was considering trying something like this, but was wary of complications of using local environment variables on a shared project, since it’s not possible to sync them on source control. I guess a build guide meant for others with simple instructions will suffice. – goose_lake Nov 11 '22 at 08:44
  • If youre using some build tool you can inject environment variables directly into the process youre starting for the build. Thats what we do – Rotem Nov 11 '22 at 14:02
  • Sounds like a good idea. What is an example of a fitting build tool? I am not so well informed about CI/CD things unfortunately. – goose_lake Nov 23 '22 at 16:57
  • 1
    Ah cant really say. We roll our own. But maybe you can look at Jenkins? It has a UE integration I think. – Rotem Nov 24 '22 at 17:20
  • I'm attempting to set this up but so far I haven't been successful. Where exactly do I define these environment variables? I've attempted to add them to the "Environment" in the configuration manager in VS as well as the system-wide environment variables, with no effect. – DAVco Aug 10 '23 at 15:56
  • @DAVco the question assumes you have some build tool which can define them. I'm not sure configuration manager in VS will have any effect since the build environment is overridden by Unreal Built Tool. System wide environment variables should work but keep in mind that changing the machine-wide env vars requires a restart if I remember correctly. The user-specific env vars should work though. – Rotem Aug 14 '23 at 14:24