So I have a single actor which can represent a wide array of objects in my game, however rather than create hundreds of different versions of this one actor using FObjectFinder to set material I was hoping to be able to dynamically define the materials used. Unfortunately I can’t pass a parameter through the constructor, and I cannot access FObjectFinder outside of the constructor, and i’ve heard that there are issues in cooking projects which use direct paths for loading resources. There has to be a logical way to do this that I’m sure I’ve overlooked.
Thanks in advance for your help.
I assume by “dynamically define” you simply mean that you want to re-assign materials to the actor’s mesh during runtime?
If that’s the case, check out this other answer I stumbled across while thinking about how to answer your question:
How to change a static mesh’s material in C++?
I don’t know many different materials you need to be able to swap in or out on your actor — you mention “hundreds” but that seems unlikely to me — but, like the linked answer suggests, you can create
UMaterial* variables on your actor class, as many as you need.
Remember to declare each
UMaterial* as a
UPROPERTY() that includes the
EditAnywhere specifier so you can set each with a material object in the Editor. Then, just write some code on your actor to call up
UMeshComponent::SetMaterial(...) whenever the actor needs to swap to using one of the other materials.
Hope that gets you closer to a solution.
Yeah, I think this is what I was looking for. My main concern is still that preloading hundreds of UMaterials into an object would be highly intensive, so I’m going to use Rama’s dynamic object loading for this, and cross my fingers and hope that it cooks alright…
Thanks for your help!