Variables in Unreal Engine

What variables can you use and how they differ from each other?

Variables in Unreal Engine

  • bool - boolean values (NEVER assume the size of bool). BOOL will not compile.
  • TCHAR - character (NEVER assume the size of TCHAR).
  • Numerical values

    • Integers

      int and unsigned int types—whose size may vary across platforms, but which is guaranteed to be at least 32 bits in width—is acceptable in code where the integer width is unimportant. Explicitly-sized types must still be used in serialized or replicated formats.

      Type Kind Size Range Exposed to BP
      int8 8 bit signed integer +/- 128 1 byte false
      uint8 8 bit unsigned integer 0 - 255 1 byte true
      int16 16 bit signed "shorts" +/- 32,768 2 bytes false
      uint16 16 bit unsigned "shorts" 0 - 65,535 2 bytes false
      int32 32 bit signed ints +/- 2,147,483,647 4 bytes true
      uint32 32 bit unsigned ints 0 - 4,294,967,295 4 bytes false
      int64 64 bit signed "quad words" +/- 9,223,372,036,854,775,807 8 bytes true
      uint64 64 bit unsigned "quad words" 0 - 18,446,744,073,709,551,615 8 bytes false
      • PTRINT for an integer that may hold a pointer (NEVER assume the size of PTRINT).
      • FScalableFloat - Generic numerical value in the form Value * Curve[Level]
    • float for single precision floating point (4 bytes).
    • double for double precision floating point (8 bytes).
    • Vectors

      Based on the accurancy you need, you can use the following:

      Type Kind Size Range Exposed to BP
      FVector_NetQuantizeNormal Vector (x,y,z) +/- 1 12 bytes / 2 bytes true
      FVector_NetQuantize Vector (x,y,z) - 0 decimal place of precision +/- 1,048,576 12 bytes / 2.5 bytes
      FVector_NetQuantize10 Vector (x,y,z) - 1 decimal place of precision +/- 1,677,721.6 12 bytes / 3 bytes
      FVector_NetQuantize100 Vector (x,y,z) - 2 decimal place of precision +/- 10,737,418.24 12 bytes / 3.75 bytes
      FVector Vector (x,y,z) - 6 decimal place of precision +/- 3.4E+38 12 bytes / 12 bytes
    • Components - Variables can point (keep address) to any component and thus be type of any component.
  • Text values

    • FName [12 bytes] - Provide a very lightweight system for using strings, where a given string is stored only once in a data table

      FNames are inherently fast, if used correctly - avoid doing of

      if (ActorHasTag(TEXT("MyFNameActor_Tag"))) ...
      which makes temporary conversion of FName to FString. Use something like following, that does not make conversion, uses FName and is roughtly 100x faster:

      static const FName NAME_MyFNameActor(TEXT("MyFNameActor_Tag"));
      if (ActorHasTag(NAME_MyFNameActor))

      The second code defines a static FName instance named NAME_MyFNameActor. This instance is created only once and exists for the entire runtime of the program. No need to create a new tag comparison string; instead, two internal FName identifiers are compared, which is faster and more memory efficient.

    • FText [16 bytes] - Primary component for text localization - All user-facing text should use this class, as it supports text localization
    • FString [24 bytes] - Unlike FName and FText, FString can be searched, modified, and compared against other strings. However, these manipulations can make FStrings more expensive than the immutable string classes.

Variable type conversion

  • FString to FName: FName FStringVal = FName(*FStringVal);
  • FString to int32: int32 FStringVal = FCString::Atoi(*FStringVal);
  • FString to float: float FStringVal = FCString::Atof(*FStringVal);
  • integer to FString: FString IntString = FString::FromInt(IntVal);
  • float to integer: FString FloatString = FString::SanitizeFloat(floatVal);