Conditionally Create Components in Constructor
I want my BaseItem classes to support either a static mesh or skeletal mesh, and I don't want to create 2 separate components if only one is going to be used. I've set up some properties in an attempt to support that:
And in the constructor:
First of all, in order for this to work after recompiling, I had to cache off any changed values (such as bUseSkeletalMesh) to a static editor object, since those get wiped during the recompile, then retrieve and set them in the constructor. I also set up the new component objects in PostEditChangeProperty, in response to the bUseSkeletalMesh property being changed:
What I'm seeing is that while I'm in the editor, I check UseSkeletalMesh, the component gets properly created by the above code. I then set the static mesh in the component to a mesh, compile it, save it, and all is well.
THE PROBLEM: when I reload the editor, both the static and skeletal mesh components are null, as if no data for either was serialized/deserialized. I'm assuming I'm missing some registration call or something to make sure these NewObject-created components get serialized properly, but I'm not sure...
There might be issue doing conditional things in constructor or else it is something like build information that does not change. Constructor is used to generate CDO with is countasins default values and it's only generated in engine init and constructor data gonna be generated only on that moment, there mightfearther problem as blueprint dupicate and override that CDO to child blueprints. So yea constructor is not good place to do dynamic or conditional thigns in constructor.
If you want to do thins kind of things, constructor is best place, also you can init both component and enable only one depending on bool state. But i think better would be if oyu made base class with common stuff and two child classes for static and skeletal mesh, which would also help you handle both differently
answered May 29 '19 at 08:26 AM
Follow this question
Once you sign in you will be able to subscribe for any updates here