..

UE C++ Coding Standard

When comes to Game Programming, Coding Standard first.


Naming Conventions

  • Template classes are prefixed by T.
  • Classes that inherit from UObject are prefixed by U.
  • Classes that inherit from AActor are prefixed by A.
  • Classes that inherit from SWidget are prefixed by S.
  • Classes that are abstract interfaces are prefixed by I.
  • Epic’s concept-alike class types (used as the first argument to the TModels type trait) are prefixed by C.
  • Enums are prefixed by E.
  • Boolean variables must be prefixed by b (for example, bPendingDestruction, or bHasFadedIn).
  • Most other classes are prefixed by F, though some subsystems use other letters.
  • Typedefs should be prefixed by whatever is appropriate for that type: F if it’s a typedef of a struct, U if it’s a typedef of a UObject and so on.

Range for

TMap<FString, int32> MyMap;

// Old style
for (auto It = MyMap.CreateIterator(); It; ++It)
{
    UE_LOG(LogCategory, Log, TEXT("Key: %s, Value: %d"), It.Key(), *It.Value());
}

// New style
for (TPair<FString, int32>& Kvp : MyMap)
{
    UE_LOG(LogCategory, Log, TEXT("Key: %s, Value: %d"), *Kvp.Key, Kvp.Value);
}

Typed Enums

// Old enum
UENUM()
namespace EThing
{
    enum Type
    {
        Thing1,
        Thing2
    };
}

// New enum
UENUM()
enum class EThing : uint8
{
    Thing1,
    Thing2
}

enums exposed to Blueprints must continue to be based on uint8.

General Style Issues

  • Always use the TEXT() macro around string literals. Without it, code that constructs FStrings from literals will cause an undesirable string conversion process.
  • Use This: FShaderType* Ptr
  • Use the virtual and override keywords when declaring an overriding method.

Platform-Specific Code

Engine/Platforms/[PLATFORM]/Source/Runtime/Core/Private/[PLATFORM]PlatformMemory.cpp