I would like to create code that adds static meshes and sets the material on the fly.
The calling code knows what it wants. It knows location, scale, rotation, mesh and material.
I envision having an array of mesh paths and an array of material paths. I could then create anything I want at any time.
This is my current calling code:
ABlock* MyActor = World->SpawnActorDeferred<ABlock>(ABlock::StaticClass(), SpawnTransform);
// Good luck trying to set mesh and material here. Any attempt seems to cause the engine to blow up.
MyActor->FinishSpawning(SpawnTransform);
In the constructor I have this:
ABlock::ABlock(const class FObjectInitializer& PCIP) : Super(PCIP) {
MyBlock = PCIP.CreateAbstractDefaultSubobject<UStaticMeshComponent>(this, TEXT(“SolidBlock”));
static ConstructorHelpers::FObjectFinder<UStaticMesh> StaticMesh(TEXT(“StaticMesh’/Game/cube_1_texture.cube_1_texture’”));
MyBlock->SetStaticMesh(StaticMesh.Object);
static ConstructorHelpers::FObjectFinder<UMaterial> Material(TEXT(“Material’/Game/Material.Material’”));
MyBlock->SetMaterial(0, Material.Object);
}
This is not what I want but if I try to use FObjectFinder outside the constructor, the engine blows up. I understand that method is limited to object construction.
The constructor doesn’t know the provided location, rotation or scale either. I can’t even use that as hacked communication channel and then change it before calling FinishSpawning.
I’ve read that an alternative is a static loader. I have not been able to make that work.
Is there no way to communicate to this constructor from the outside to tell it what I want for mesh and material?
If I could just pass in a variable through some side channel to tell it what I want. I may resort to a singleton that the caller can set and the constructor can pull but that could run into issues if this constructor is loaded multi-threaded so that between the call and construction, the value could change.
Another alternative is to create a subclass for each mesh. I don’t want to go down that road. I could create subclasses via code generation but that would be nasty.
If anyone has a way to talk to that constructor from the caller, that would be awesome.