Variables in Unreal Engine

Published on

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

Platforma

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.

      TypeKindSizeRangeExposed to BP
      int88 bit signed integer+/- 1281 bytefalse
      uint88 bit unsigned integer0 - 2551 bytetrue
      int1616 bit signed "shorts"+/- 32,7682 bytesfalse
      uint1616 bit unsigned "shorts"0 - 65,5352 bytesfalse
      int3232 bit signed ints+/- 2,147,483,6474 bytestrue
      uint3232 bit unsigned ints0 - 4,294,967,2954 bytesfalse
      int6464 bit signed "quad words"+/- 9,223,372,036,854,775,8078 bytestrue
      uint6464 bit unsigned "quad words"0 - 18,446,744,073,709,551,6158 bytesfalse
      • 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:

      TypeKindSizeRangeExposed to BP
      FVector_NetQuantizeNormalVector (x,y,z)+/- 112 bytes / 2 bytestrue
      FVector_NetQuantizeVector (x,y,z) - 0 decimal place of precision+/- 1,048,57612 bytes / 2.5 bytes
      FVector_NetQuantize10Vector (x,y,z) - 1 decimal place of precision+/- 1,677,721.612 bytes / 3 bytes
      FVector_NetQuantize100Vector (x,y,z) - 2 decimal place of precision+/- 10,737,418.2412 bytes / 3.75 bytes
      FVectorVector (x,y,z) - 6 decimal place of precision+/- 3.4E+3812 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);
Vrealmatic consulting

Anything unclear?

Let us know!

Contact Us