TArray<AActor*> Add

in header:



UPROPERTY(Transient, VisibleAnywhere)	
TArray<AAStar*> stellarBodies;


in BeginPlay:



AAStar* cur = GetWorld()->SpawnActor<AAStar>(AAStar::StaticClass(), FVector::ZeroVector, FRotator::ZeroRotator);
		
stellarBodies.Add(cur);


What is the problem you’re having? The Add syntax looks fine.

SpawnActor is a templated function, you have to provide whatever class it is in <> brackets

I edited my previous post a bit, and updated how i am calling SpawnActor. I noticed that AActor::GetActorClass() is depreciated. Also, i never got a compile error when writing without template params… not sure why.

The error i now get on compile is:



error C2663: 'TArray<AAStar *,FDefaultAllocator>::Add': 2 overloads have no legal conversion for 'this' pointer


Also, in the intellisense for TArray::Add() i see 2 overloads with the following params:


Add(AAStar *const &Item)


Add(AAStar *&&Item)

How am i supposed to read those params? Pointer to a constant address of item? Pointer to an address of an address? What is that? How do i understand that? Coming from C# i hate C++ intellisense, in 90% of the time it is not helpful… and very slow.

I don’t know how to add a simple pointer to my dynamic array of pointers. This is lame…

It is likely that the method you are calling stellarBodies.Add(cur) in is const. Can you check that?

If the parameter type can be unambiguously deduced, the compiler is happy to use it without an explicit parameter.

Check out this for a handy way to decipher some C++:

http://c-faq.com/decl/spiral.anderson.html

AAstar *const &Item is:

“item is a reference to a constant pointer to an AAStar”

Const protects to the left, so the pointer is protected, not Item

@Lawlcat: Thanks for the Clockwise/Spiral link, that is very helpful!

My problem is still that i cannot figure out how to add a pointer to my TArray. Would someone be so kind as to just write out how this is done so that i can study how exactly to do this? I have tried multiple ways and nothing seems to work. Thanks



	TArray<AActor*> actorArray;
	TArray<AActor*>* actorArrayReference = &actorArray;


TArray<AActor*> actorArray is a “TArray” containing a list of AActor pointers.

TArray<AActor*>* is a pointer to a TArray containing a list of AActor pointers. We initialize this to the address of (&) actorArray

Thanks Lawlcat, i never thought about getting a pointer to the TArray. Although, i still don’t know how to add a pointer into that TArray. For example my code is:



AAStar* const cur = GetWorld()->SpawnActor<AAStar>(AAStar::StaticClass(), FVector::ZeroVector, FRotator::ZeroRotator);
const TArray<AAStar*>* stellarBodiesRef = &stellarBodies;
stellarBodiesRef->Add(&cur);


Also, i am not sure what the pointer to the TArray (stellarBodiesRef) actually gives me as i can call



stellarBodies.Add(XYZ);


without the additional pointer (stellarBodiesRef).

Error on the “add” line. No instance of overloaded function…

Also, is there a better way to keep a collection of objects? TArray seems like it is not very easy to work with pointers…


AAStar* cur = GetWorld()->SpawnActor<AAStar>(AAStar::StaticClass(), FVector::ZeroVector, FRotator::ZeroRotator);
TArray<AAStar*> stellarBodiesRef;
stellarBodiesRef.Add(cur);

Try this.

What you store in a container is irrelevant, they are generic. Which container is most suitable will depend on what operations you need to perform on the collection.

WHAT?! This compiles:



AAStar* cur = GetWorld()->SpawnActor<AAStar>(AAStar::StaticClass(), FVector::ZeroVector, FRotator::ZeroRotator);
TArray<AAStar*> stellarBodiesRef;
stellarBodiesRef.Add(cur);


but this does not compile:



stellarBodies.Add(cur);


as i have shown in previous posts i have declared stellarBodies and stellarBodiesRef both as TArray<AAStar*>. The only difference is stellarBodies is declared in the header file and stellarBodiesRef is declared locally in the function. Why am i able to add a pointer to one and not the other? stellarBodies is a UPROPERTY… maybe that has something do with it?

the error i get when adding to stellarBodies is:


 error C2663: 'TArray<AAStar *,FDefaultAllocator>::Add': 2 overloads have no legal conversion for 'this' pointer 

Also, in the intellisense it says “No instance of overloaded function”

I was declaring stellarBodies UPROPERTY as private in the header. Changed it to public, but that didn’t make any difference. Can UPROPERTY be private?

WOW! Matt, you are absolutely right. i Was calling stellarBodies.Add(cur) from inside a function marked const. That solves my problem. Thank you!