Get UE version in ModuleRules (.Build.cs file)

We’re working with 2 versions of UE4 here due to a bug in the later versions, and I need to be able to check what version to do some conditional stuff in my Build file. Is there a way to do this, ideally with preprocessor checks?

Before UE4.14:

var VersionFilePath = Path.Combine(UnrealBuildTool.UnrealBuildTool.EngineDirectory.FullName, "Build" + Path.DirectorySeparatorChar + "Build.version");

BuildVersion Version;
if (BuildVersion.TryRead(VersionFilePath, out Version))
{
	if (Version.MajorVersion == X && Version.MinorVersion == Y)
	{
		// do version specific stuff
	}
}

After and including UE4.14:

BuildVersion Version;
if (BuildVersion.TryRead(out Version))
{
	if (Version.MajorVersion == X && Version.MinorVersion == Y)
	{
		// do version specific stuff
	}
}

This doesn’t answer the question.

This works for C++ files, but not in C# (ie: Build.cs)

In 4.17 Epic introduced C# preprocessor macros for this (see release notes), and you can now conditionally compile blocks with something like:

#if UE_4_17_OR_LATER
// Do something
#else
// Do something else
#endif

Additionally, from 4.16 the macro WITH_FORWARDED_MODULE_RULES_CTOR is also defined. This was intended to decide whether the constructor of your ModuleRules subclass should receive a TargetInfo (up to 4.15) or a ReadOnlyTargetRules (from 4.16). However, you could use it to check for version 4.16 or later too (assuming that macro will not go away in the future…); that is, you could do:

#if WITH_FORWARDED_MODULE_RULES_CTOR
#define UE_4_16_OR_LATER
#endf
1 Like

Dude where did you find this ?!? Please elaborate.

What exactly? As mentioned, the UE_4_X_OR_LATER macros were introduced in the 4.17 release notes. The macro WITH_FORWARDED_MODULE_RULES_CTOR was introduced in the 4.16 release notes, as part of the change from TargetInfo to ReadOnlyTargetRules. I didn’t learn about these from the release notes themselves (I don’t remember where I saw them first, but I think probably in some question on this site), but that is the best reference I could find for them (besides the source code).

I know this will revive this old topic, but as this topic comes up pretty much top of the list on Google, there should be an updated response.

If you want to check the Unreal Engine version in any Build.cs, you can check with Target.Version.

Target.Version.MajorVersion, Target.Version.MinorVersion & Target.Version.PatchVersion are all the parts of the Unreal Engine version.

Console.WriteLine(String.Format("Unreal version {0}.{1}.{2}", Target.Version.MajorVersion, Target.Version.MinorVersion, Target.Version.PatchVersion));

You can also use ReadOnlyBuildVersion.Current to get the Version being used, in place of Target.Version. Hopefully this helps someone.

4 Likes