Download

Arguments to default constructor possible workaround?

I know this problem has been mentioned previously, and I understand the need of having a default constructor with FObjectInitializer.

I read this
thread, but is still unclear to me how to proceed when I have an AActor included in another AActor, like the following



UCLASS()
class THIRDPERSON_API AMyActorX : public AActor
{
	GENERATED_BODY()

public:

	AMyActorX (const FObjectInitializer& ObjectInitializer);

	AMyActorY Y;
};




I know I can’t add extra parameters to the default constructor, instead I tried using in the constructor FObjectInitializer for MyObjectY



AMyActorX ::AMyActorX (const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer), Y(ObjectInitializer)
{
}




Which compiles but will produce a crash on startup, my guess is that it producing some kind of cycling dependency of the objects when creating them, but please elaborate.

I can solve the problem by using a pointer for AMyActorY*, it will compile and run correctly.

But the question is, how would it be correctly implemented using AMyActorY instance?

Also, would there be a workaround if declaring this Actor as static?

EDIT:
As static works fine, is good to know but it doesn’t answer my other questions

Exactly what problem are you trying to solve?

Including an actor as a member variable like this is a recipe for disaster. While this sort of thing is proper plain C++, UObject initialization is a wholly different beast. The constructor is not always used at runtime, because in certain situations an object can just be constructed faster by serializing it from disk. Being untagged, the actor member variable will not be serialized. And that’s just one of the many ways this will break the engine’s object management.

-Camille

That is the information I was looking forward. Thank you!

Now it is more clear to me why the start up process crashes. Good advice, I am not going to use this kind of instantiation.

For what it’s worth, if you want to nest information from a different class, generally this is done with subobjects in UE4. Actor components are the main example of subobjects and pretty easy to create if you happen to be using an actor.

-Camille