TObjectIterator<ULightComponent> Retrieving Lights Not Even in World

Hello! I’m rewriting my light detection code so that it calculate’s a lit value from all lights found and I’m using TObjectIterator to get the lights. My code works great so far, but I have one problem. TObjectIterator is finding a light that I didn’t place in my world via actors nor components. It’s just one light that is inaccessible or something. When I write out the name of the owner of that light, it comes back with “Default_DirectionalLight”. I’ve checked all of my scenes and this seems to be there in all Unreal scenes. I’ve already tried adding checks to see if it’s valid, but I still get it. Any help? Thanks! :slight_smile:

I actually just went through something similar. I believe what is happening is that there is a default object (to serve as a template of sorts) constructed for every object class. The object iterator picks up these default objects. To get around this, try adding “if (ObjIter->IsRegistered() && ObjIter->GetWorld()->WorldType != EWorldType::Editor)” while you are iterating.

Yes, that’s the CDO (Class Default Object). A better way to see if you have a default object is to do

if (!Object->HasAnyFlags(RF_ClassDefaultObject))
// Not a CDO so operate…

You should also keep in mind that if you were to run the command while doing Play in Editor you will you will get the objects for not just the editor world, but also any play worlds, also potentially the worlds for a preview in the blueprint editor, persona, even the thumbnail rendering worlds, so if you care about only light components in a given world you will need to filter down to it by checking that the GetWorld() is the same as the editor world you are interested in.