Download

When Does the C++ Constructor Run?

I have an Instances Static Mesh Component with the instances created in the constructor. The variable that defines the number of instances is exposed to blueprints, but when this variable is changed in a child blueprint and the blueprint compiled nothing changes. (It is originally 64 instances and it stays that way) I feel like I don’t understand when the constructor is run. Can anyone help me out and explain how I can achieve what I’m after?

The C++ constructor is only run once to create a “default” instance of that object, when your code module is loaded (on engine start or hot reload). Thereafter, the engine builds copies of that object via it’s reflection data.

There are a collection of C++ override-able methods you can use to alter this behaviour before or after it’s construction time. For Components, if you want your code to run in the Editor, you probably want to override theOnRegistermethod. This is called every time the component is added to a world (editor and in-game), after it’s properties have been set. Be careful how much you do in here though, “moving” the containing Actor will retrigger this method (a bit like actor construction scripts).

By the sounds of it, you won’t need a matching override for OnUnregister.