Iterate through actors of type

Guys,

I searched and found this wiki post that I followed but can’t get it work. I suspect the wiki is out of date or has invalid code samples to begin with as I don’t understand the c++ code sample.

Basically the goal is to iterate through all the player starts in the game and pick a random one. I don’t understand this for loop construct or how this magical “ActorItr” comes from and neither does my compiler.

What am I missing? → I’ll attempt to add to wiki once solves. Thanks Guys.

AScepterCamera::AScepterCamera(const class FPostConstructInitializeProperties& PCIP)
	: Super(PCIP)
{
	for (FActorIterator ActorItr<APlayerStart>(()); ActorItr; ++ActorItr)
	{
		this->SetActorLocationAndRotation(ActorItr->GetActorLocation(), ActorItr->GetActorRotation(), false);
	}
}

you should not being using () or doing stuff in relation to larger world environment in your constructor!

move this into Post Init and you should be fine

also you should always post compile errors if you are getting any

For some reaon 's method seems to be outdated and FActorIterator make error like the template on it does not exist in it anymore… instead i found TActorIterator which seem to work

//in h in class

TArray<APlayerStart*> SomeArray;

//in cpp
for (TActorIterator<APlayerStart> ActorItr(()); ActorItr; ++ActorItr)
{
        SomeArray.push(*ActorItr);
}

Push all PlayerStart pointers to array as i showed and then randomly pick one of them from array ;]

And as said, you should not use that in constructor not only world pointer in () is most likely initiated which will cause the crash, but also your PlayerStart won’t exist at that point in game memory. Do it in BeginPlay() or PostInit()

1 Like

instead i found TActorIterator which seem to work

I myself dont know what I still had FActorIterator in some parts of my code

I looked at this post twice thinking,… “FActorIterator?”

I will go update my wiki page now, thanks !

PS: though I just checked and FActorIterator still compiles for me… strange :slight_smile:

No problem :slight_smile:

Thanks guys makes sense now.