`FindObject` called in editor, from `OnConstruction`: never finds anything

So what am I missing regarding the correct use of FindObject?

I get this error:

LogMyGame: Error: BP_RockCylindric_C /Game/Menu/MainMenu.MainMenu:PersistentLevel.BP_RockCylindric_C_2: couldn't find AObject by fname Orbit_BP_RockCylindric_C_2
LogSpawn: Error: Cannot generate unique name for 'Orbit_BP_RockCylindric_C_2' in level 'Level /Game/Menu/MainMenu.MainMenu:PersistentLevel'.

indicating that an object of fname “BP_RockCylindric_C_2” could not be found, yet creating an actor with that name fails because the name is already taken.

Is there a problem using FindObject in OnConstruction and expecting it to work while using the editor? (Not PIE, but design-time)

My best shot at the different FindObject variants is this:

AOrbit* Orbit = FindObject<AOrbit>(GetWorld(), *StrName);

Maybe related I get this error from within existing orbits:

Ensure condition failed: MyOwnerWorld

… as if there is something wrong with my world object.

1 Like

Your object is not yet in the world when it’s being constructed.
If you want to interact with the world, your best bet is to do it in Begin Play, or set a 0 duration timer to do it.
Or use an accessor function that looks for the object you need if it’s not already known, so it’s done on demand.

The object I am looking for is not being constructed. It exists just fine.

I can even use the object iterator to loop through all objects of a class and this way, I find it just fine. It’s just a bit weird that I would use the object iterator, when I have should be able to look up the object using its FName.

Hi rubm123,

I’d check that World - maybe the construction is happening in another editor viewport?
Maybe try using this reference for world:

UWorld* world=GEngine->GetWorldContextFromGameViewport(GEngine->GameViewport)->World();

Yes! I have problems elsewhere relating to World being invalid. It all makes sense now :slight_smile:

I’ll get back here, once I figured it all out … but we are on to something.

What am I missing?

Well I am in OnConstruction of one Actor to call FindObject to find another actor.

The actor that calls GetWorld() is the one that’s in OnConstruction(), then?
Which would mean that it’s not in the world yet?
Which means that GetWorld() would not return the world yet?
Something to check.

1 Like

I will check that, thanks.

My latest try to step through the process with the debugger was quite confusing. The
GetWorld of the orbit actor that I am spawning returns gibberish, but then later, the same GetWorld() == World gives true (where the second world is from the spawning actor).

I will try and debug this again systematically tomorrow.

That’s the behavior I noticed too - but it was seemingly random, until I figured it was the Blueprint Editor calling the constructor script on it’s copy of the object as well as the level calling it (only happened when it was open) - if you check the GetWorld() of that actor and compare it with the version you get with that code I mentioned earlier you should be able to tell which is which. This is Editor-only of course…

1 Like