Hello. Im not sure the title of the question is even 80% accurate.
Anyway I want to spawn an array of “child actor components” (no problem there ( for now ) ) depending on the size of a UProperty i have set up. But when i tried that ofc, it defaults to 0 ( as that is default ) at first. But even, if I change it in the editor it stays 0, throughout the construction and on my begin play" breakpoints - it has been changed to the value given from the editor.
So is it possible through ‘some means’ to be able dynamically change a exposed to editor property and for it to effect constructor or should i just do the components creation in the “begin play”
Well, a constructor (as i’m sure you know :D) simply represents the ‘layout’ or floor plan of the class and when it becomes an object the variables are the defaults when constructed.
So if I understand your question correctly then I would say no it is not possible. After the initial construction of course it is possible to edit the values but otherwise if you want the default values to be something else then place it in the constructor.
If you want to change it at run time rather then creation then you’ve got the right idea. Use BeginPlay.
Yes, it’s possible using the OnConstruction function. Basically, this is called each time you modify a property in the editor. So, if you want to have 2 actor component after you input 2 in the editor, Spawn those components in the OnConstruction call.
You can do it in Blueprint or C++.
The C++ function signature you need to overide is this :
I recommend using the #if WITH_EDITOR macro here just so its explicit this is only true in the editor.
After that, you can spawn components. Here is an example:
for (int32 i = 0; i < nNbComponentsToSpawn; i++)
{
USphereComponent* spawnPoint = NewObject<USphereComponent>(this);
spawnPoint->CreationMethod = EComponentCreationMethod::UserConstructionScript;
spawnPoint->AttachParent = RootComponent;
spawnPoint->bCastDynamicShadow = false;
spawnPoint->SetCollisionEnabled(ECollisionEnabled::NoCollision);
}
RegisterAllComponents();
In this scenario, nNbComponentsToSpawn is the variable you have change value of, and it’s going to spawn the right number of components (sphereComponents in this case).
a small issue arose - since NewObject - ‘NewObject’ : invalid template argument for ‘T’, type expected. And im using - static ConstructorHelpers::FObjectFinder SegmentBlueprint(…) it doesn’t register it at comple-time it seems. Is there a way to ‘circumvent’ this