Pages

quarta-feira, 26 de agosto de 2020

Float, struct, and FVector

In this article, we are going to take a quick look at some concepts needed to position an Actor in 3D space.

First, let's talk about floating-point types of variables. These types of variables are used to represent decimal numbers. The main types are float, which has 32 bits, and double, which has 64 bits.

Most Unreal Engine code uses the float type for decimal values. These are some examples of float variables:  

float var1;

float var2 = 7.45F;

float var3 = 8.f;  // It is the same as 8.0f 

The suffix f or F is necessary to indicate that a value is of type float because by default the values with a dot are of type double.

Another important concept is the structure. A structure is used to gather in one place several related variables. The variables that are part of a structure can be of different types. You can also have structures that contain other structures.

To define a structure in C++ use the keyword struct. In C++ it is possible to add functions to structures, making them very similar to a class. The only difference between struct and class in C++ is that the members of a structure are public by default and the members of a class are private by default.

In Unreal Engine, the structures are represented by types that have the prefix F as FVector and FTransform for example. If you use a type/class that has the prefix U or A, you must create a pointer. But for types prefixed with F, you don't need to create a pointer.

The FVector structure contains three variables of type float which are XY, and Z. The following example shows the creation of two variables of type FVector containing the same values for XY, and Z.

FVector MyVector1;
MyVector1.X = 500.f;
MyVector1.Y = 700.f;
MyVector1.Z = 300.f;

FVector MyVector2 = FVector(500.f, 700.f, 300.f);

Note that to access the structure variables, a dot (.) was used between the structure name and the variable name. You only use -> when using pointers.

The AActor class has a function to set the location of an Actor in 3D space. The function name is SetActorLocation() and it receives an FVector with the new location as a parameter.

The example below shows two ways to call SetActorLocation():

FVector NewLocation = FVector(300.f, 200.f, 100.f);
SetActorLocation(NewLocation);

//It is the same as:

SetActorLocation( FVector(300.f, 200.f, 100.f) );


Table of Contents C++