Download

Error in Assigning value to TArray!

Hi everybody,

I just made a TArray of type FVector:

TArray <FVector> Vertices[1024]{};

then inside a function, I tried to assign a FVector value to the array element and it doesn’t work:

FVector point = someActor->GetActorLocation();
int index = CalculateIndexSomeHow();
Vertices[index] = point;

//Here I’ll get a compile error that says: No operator “=” matches these operand. Operand types are: TArray<FVector, FDefaultAllocator> = FVector.

I think this is a trivial issue, but I didn’t know how to figure it out. I tried making pointer FVectors and dereferencing them for assigning value to the TArray and didn’t work.

You probably got confused with the syntax there, what you actually created is an array of 1024x TArray<FVector>.

I’m assuming you meant:


TArray<FVector> Vertices;
Vertices.Reserve(1024);

Also, you should be using the unreal version of int (int32)

Yup man, I did mixed it up with C++ basic arrays assuming (with no rational reason) that every single operator of basic Cpp arrays is overloaded for me not to bother learning about TArrays.

Thanks indeed.

And thanks for int32 notion, that’s a great advice as well.

Technically you made my day, dude.

Just wanna throw in that you can also use int8, uint16, etc when in C++.

(Though if the int will be used with blueprints it will have to be int32)

Thanks bro.

Actually I think I wouldn’t need any counting index more than 2^16 (65536 item).

Could be, but it’s common practice to just always use int32 for indices in loops even if you don’t need that many.
(I don’t know if there is also a technical reason for doing that)

Yeah I guess Unreal compilation will result in assembly code of the game that fits the proper size for each platform CPU’s word size and instruction set. (memory addresses of 4 byte, 2 bytes, etc for the integer variable ). I think most CPUs allow you to partition memory access in not less than a byte step, or at least loading the whole word (32 || 64) and then partitioning access to its content in the registers based on specific instruction command, which would potentially slice memory access times in half or quarter.

So it seems to me the best practice is to use only the size you’ll need, that would give some performance improvement on some platforms.

This is all speculations from my side anyhow, but it seems to be true, because otherwise why Unreal offers int16 or int8 to begin with.