C++ alternative to BP-"Set array elem" node ?

This bp specific node inserts an element into the array at a chosen location even if the location doesn’t exists yet.
What can i do to achieve that in C++?

Edit: Answer below.

I think you may have the wrong method. Init sets a number of array elements to “Element” (that’s the two parameters you passed in, the element to copy into the array, and how many of that element to copy).

Ex:



// In my header file, or as a variable somewhere.
TArray<int> IntegerArray; // Right now, this array is completely empty. It contains nothing, not even memory to hold any integers.

// Somewhere in my code.

IntegerArray.Init(0, 10); // Allocate enough space for 10 integers, and set all those values to 0.
// Memory wise this would look like [0][0][0][0][0][0][0][0][0][0]

IntegerArray[4] = 20; // Set the 5 element (0 based counting) to 20.
// Memory wise it now looks like [0][0][0][0][20][0][0][0][0][0]


I see, so what is the equivalent of “set array elem” in c++ ?
Thanks.

Just the bracket operator “]”.

myArray[IndexToSet] = ValueToSet;

But will it resize the array to the proper size ?

Ok i’v found myself a solution, eventhough it would be one to have a built-in one:

Just have to call this function from were ever:



In MyFunctions.h:

	template <typename T>
	static void SetArrayElement(T item, TArray<T>& item_array, int32 index)
	{
		if (item_array.Num() - 1 < index)
			item_array.SetNum(index);

		item_array.Insert(item, index);
	}

like this:

cpp:


#include MyFunctions.h

MyFunctions::SetArrayElement<var_type>(var, var_array, index);

4 Likes