UE4's TArray<T>

TArrays are UE4's version of a dynamic array. To understand what a TArray<T> variable is, you first have to know what the <T> option between angle brackets stands for. The <T> option means that the type of data stored in the array is a variable. Do you want an array of int? Then create a TArray<int> variable. A TArray variable of double? Create a TArray<double> variable.

So, in general, wherever a <T> appears, you can plug in a C++ type of your choice. Let's move on and show this with an example.

An example that uses TArray<T>

A TArray<int> variable is just an array of int. A TArray<Player*> variable will be an array of Player* pointers. An array is dynamically resizable, and elements can be added at the end of the array after its creation.

To create a TArray<int> variable, all you have to do is use the normal variable allocation syntax:

TArray<int> array;

Changes to the TArray variable are done using member functions. There are a couple of member functions that you can use on a TArray variable. The first member function that you need to know about is the way you add a value to the array, as shown in the following code:

array.Add( 1 );
array.Add( 10 );
array.Add( 5 );
array.Add( 20 );

These four lines of code will produce the array value in memory, as shown in the following figure:

An example that uses TArray<T>

When you call array.Add( number ), the new number goes to the end of the array. Since we added the numbers 1, 10, 5, and 20 to the array, in this order, that is the order in which they will go into the array.

If you want to insert a number in the front or middle of the array, it is also possible. All you have to do is use the array.Insert(value, index) function, as shown in the following line of code:

array.Insert( 9, 0 );

This function will push the number 9 into the position 0 of the array (at the front). This means that the rest of the array elements will be offset to the right, as shown in the following figure:

An example that uses TArray<T>

We can insert another element into position 2 of the array using the following line of code:

array.Insert( 30, 2 );

This function will rearrange the array as shown in the following figure:

An example that uses TArray<T>

Tip

If you insert a number into a position in the array that is out of bounds, UE4 will crash. So be careful not to do that.

Iterating a TArray

You can iterate (walk over) the elements of a TArray variable in two ways: either using integer-based indexing or using an iterator. I will show you both the ways here.

The vanilla for loop and square brackets notation

Using integers to index the elements of an array is sometimes called a "vanilla" for loop. The elements of the array can be accessed using array[ index ], where index is the numerical position of the element in the array:

for( int index = 0; index < array.Num(); index++ )
{
  // print the array element to the screen using debug message
  GEngine->AddOnScreenDebugMessage( index, 30.f, FColor::Red,  FString::FromInt( array[ index ] ) );
}

Iterators

You can also use an iterator to walk over the elements of the array one by one, as shown in the following code:

int count = 0;	// keep track of numerical index in array
for( TArray<int>::TIterator it = array.CreateIterator(); it; ++it  )
{
  GEngine->AddOnScreenDebugMessage( count++, 30.f, FColor::Red,  FString::FromInt( *it ) );
}

Iterators are pointers into the array. Iterators can be used to inspect or change values inside the array. An example of an iterator is shown in the following figure:

Iterators

The concept of an iterator: it is an external object that can look into and inspect the values of an array. Doing ++ it moves the iterator to examine the next element.

An iterator must be suitable for the collection it is walking through. To walk through a TArray<int> variable, you need a TArray<int>::TIterator type iterator.

We use * to look at the value behind an iterator. In the preceding code, we used (*it) to get the integer value from the iterator. This is called dereferencing. To dereference an iterator means to look at its value.

The ++it operation that happens at the end of each iteration of the for loop increments the iterator, moving it on to point to the next element in the list.

Insert the code into the program and try it out now. Here's the example program we have created so far using TArray (all in the ATArraysGameMode::ATArraysGameMode() constructor):

ATArraysGameMode::ATArraysGameMode(const class FPostConstructInitializeProperties& PCIP) : Super(PCIP)
{
  TArray<int> array;
  array.Add( 1 );
  array.Add( 10 );
  array.Add( 5 );
  array.Add( 20 );
  array.Insert( 9, 0 );// put a 9 in the front
  array.Insert( 30, 2 );// put a 30 at index 2
  if( GEngine )
  {
    for( int index = 0; index < array.Num(); index++ )
    {
      GEngine->AddOnScreenDebugMessage( index, 30.f, FColor::Red,  FString::FromInt( array[ index ] ) );
    }
  }
}

The output of the preceding code is shown in the following screenshot:

Iterators

Finding whether an element is in the TArray

Searching out UE4 containers is easy. It is commonly done using the Find member function. Using the array we created previously, we can find the index of the value 10 by typing the following line of code:

int index = array.Find( 10 ); // would be index 3 in image above
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.176.194