Problems spawning an Actor in C++ for use in the editor

I’m creating actors via command line in the editor through the EdEngine Exec. The actors I create go into the editor, not into the game realtime (I’m generating actors with components based on command line parameters). I’d like to be able to modify them in the level once they’re created as normal: modify parameters, location/rotation, copy/paste, have it be saved in the level and loaded like any other actor placed in the editor.

I’m using the basic:

NewActor = InWorld->SpawnActor<AActor>(AActor::StaticClass());

Then I add some components. For example:

UInstancedStaticMeshComponent *SMComp = NewObject<UInstancedStaticMeshComponent>(NewActor);

and I call SetFlags(RF_Transactional); on the actor and the components.

Problem #1: I can’t modify any properties on the components in the editor. It says native components need uproperties. There is no C++ class for this. I simply spawn an empty actor into the level and add components on the fly.

Problem #2: If I spawn my actor with a name (using FActorSpawnParams), and then I try to spawn another actor with the same name, instead of increasing the counter on the name like it normally happens in editor, it doesn’t spawn anything and SpawnActor returns the actor that already has that name. I’d like to give it an arbitrary name that automatically counts up when I try to place more of them.

Problem #3: If I add two Instanced Static Mesh Components to one actor, the second one shows up in PIE but not Standalone. If I set RF_Standalone on the component, they’ll show up in Standalone mode, but not in the packaged build, and playing it in PIE hits an ‘ensure’.

Problem #4: Copying/Duplicating the actor, doesn’t Copy/Duplicate the Instances array in the InstancedStaticMeshComponent. This problem also happens if I create the actor and components directly in the editor, so it’s not a C++ problem. I’d like to be able to copy and/or duplicate these actors though.

Problem #5: Can’t move the actor from one sublevel to another because of Problem #4 (apparently Unreal copies, deletes, and pastes to make that happen). When I say move, I mean assign the actor to a different level, I don’t mean change the location. Like, select the actor, click on the level editor, select the level you’d like to move the actor to, right-click it and select “Move Selector Actor to Level”.

Is anyone familiar with spawning an actor for in-editor use? Any ideas?

Problem #1 is solved. Right after spawning the component set: SMComp->CreationMethod = EComponentCreationMethod::Instance; and I was able to modify the parameters as normal.

    1. Call NewActor->AddInstanceComponent(SMComp) to set the creation method and add the component to the instance components array.
    1. That’s the problem with names: they are supposed to uniquely identify objects. Use SetActorLabel instead if you want to set in-editor labels that can be the same. Use MakeUniqueObjectName to generate a unique name.
  • 3-6. I think most of this weirdness is caused by the object not being in the instance components array. Try calling AddInstanceComponent and see if these issues persist.

Thank you so much dbuchoff, that was awesome! Most helpful answer I’ve gotten in these forums yet!

The only issues is (4),(5) but those happen even if I create the Instanced SM Component manually in the editor. The instances array doesn’t get copied, which is weird, but nothing to do with my code.

hmmm… that’s odd! I’m seeing that problem too: duplicating an actor with a ISMC “forgets” the instances.

I have no idea why it’s like this, but I’m fairly confident it’s because UInstancedStaticMeshComponent::PerInstanceSMData is marked DuplicateTransient. You may need to populate the instances with your own Construction Script in order to make this work right :-(.