GetOwner doesn't work on components? (4.22)

I’m getting a strange issue that I’ve been stuck on for 2 days now with GetOwner() not working as I think it should.

  1. I have an object with some values and a component attached to it.

  2. In the objects OnBeginPlay, a bunch of those values are set to specific things.

  3. If at any point you use this code to check those values, they will always be defaulted to 0, None, nullptr, etc.


Cast<MyObject>(GetOwner())->someValue;

I’ve tried remaking components, actors, attaching different things, etc. No matter what, any runtime changes are not shown through GetOwner().
It almost feels like GetOwner is giving me a copy of the object instead of the actual one.

Am I just using it incorrectly or is this a bug?

Found the problem immediately after posting this, leaving here for reference.

I was using this to find the component in the world and interact with it:


    for (TObjectIterator<UInteractComponent> obj; obj; ++obj) {
        //Do stuff
    }


However I finally remembered that unreal is unreal, and it was actually finding me components inside the EDITOR and not the ones in the game world, which is why it was only using default values!
Adding this in there to check the world fixed it:



if (obj->GetWorld() != GetWorld()) continue;

You can also check object flags or check if OuterMost is in the level.

Most of the time a class doesn’t override to implement GetWorld() so the value of that function will usually be a nullptr by default.

For Actor Components it’s fine, they all have the GetWorld function - but yeah object iterators will get everything, class-default objects, editor objects, even objects in other maps if they are loaded.