I have a custom object which needs to be automatically initialised before it’s owning Actor invokes BeginPlay(), Here’s what I’ve tried so far based on this question:
void UMyObject::DoSomething() {
if (ActorOwner) {
// ... Do something with ActorOwner
}
}
void UMyObject::PostLoad() {
Super::PostLoad();
if (GIsEditor && !GIsPlayInEditorWorld) {
return;
}
Init(GetOuter()); // ActorOwner = Cast<AActor>(GetOuter());
}
My main goal here is to avoid having to use MyObj->Init(this) inside the MyActor and instead the let object initialise itself since it becomes tedious when there are several custom objects
No, there’s no particularly good hook for this with UObjects. Init functions are the most common practice.
One alternative would be for your object to be a UActorComponent instead of UObject. Those do include additional hooks into the Actor lifetime so that you could do things prior to BeginPlay.