Can't work out how to use TObjectIterator...

I just want to iterate through all the objects in my world of UBZGame_GameObjectComponent>. This is just an ActorComponent that I can attach to almost any kind of Actor.

However, it returns objects in the content browser which I really don’t want, I only want it to use objects that are currently in the game world / level. I want to use ObjectIterator instead of ActorIterator, since it’s mainly the Object itself I want to access, not the Actor. That, and not all actors will have the component.

Any idea how I can solve this? The following code just prints the name of every actor whether in play or not to the screen (and not for 0.1 seconds either oddly enough)

	/* For all GameObjectComponents In the World */
	for (TObjectIterator<UBZGame_GameObjectComponent> Itr; Itr; ++Itr)
		AActor* GOCOwner = Itr->GetOwner();
		FVector TargetLocation = GOCOwner->GetActorLocation();

		/* Discard Instigator and any actors being destroyed */
		if (GOCOwner == NULL || GOCOwner == Instigator || GOCOwner->IsPendingKillPending())
        GEngine->AddOnScreenDebugMessage(-1, 0.1f, FColor::Green, FString::Printf(TEXT("Name- %s"), *GOCOwner->GetName()));


World Comparison

You can do a world check with an object that you know is part of the correct world (not the editor world)

UWorld* YourGameWorld = //set this somehow, from another UObject or pass it in as parameter

for(TObjectIterator<UYourObject> Itr; Itr; ++Itr)
   //World Check
   if(Itr->GetWorld() != YourGameWorld)
   //now do stuff

**In-Engine Example ~ Get All Widgets Of Class**

The above is the code structure that I used for my Get All Widgets of Class node, pull request that Epic accepted that is now live in 4.7 !

**Github Link**

I avoid getting the UMG widget default objects /editor objects by passing in the world using the Blueprint method of setting a WorldContextObject!



Thanks Rama, that’s exactly what I did in the end :slight_smile: I was just wondering if there was any ability to filter it out beforehand, thanks!