Skip to content
The Chat Archive
gpt-4-1106-preview· OpenAI Arena Human Preference 55K

In UE5, I can use CreateDefaultSubobject to create a subobject within the constructor of…

  1. User

    In UE5, I can use CreateDefaultSubobject to create a subobject within the constructor of e.g. an actor. But, how do I create an object later, for example in PreInitializeComponents?

  2. Assistant

    In Unreal Engine 5 (UE5), CreateDefaultSubobject is indeed used within the constructor of a class derived from UObject to create components or other UObject-derived subobjects that are intended to exist for the entire lifetime of the parent object. These subobjects are also automatically replicated, serialized, and integrated into the Unreal Engine's garbage collection system.

    However, if you need to create an object at a later time, such as during the PreInitializeComponents phase or at any point after the constructor has run, you should not use CreateDefaultSubobject. Instead, you can use the NewObject function to dynamically create instances of UObjects.

    Here's an example of how you might create a component during the PreInitializeComponents override:

    void AMyActor::PreInitializeComponents()
    {
        Super::PreInitializeComponents();
    
        // Make sure to only do this once and that the component is not already created
        if (MyDynamicComponent == nullptr)
        {
            MyDynamicComponent = NewObject<UMyComponent>(this, UMyComponent::StaticClass());
    
            if (MyDynamicComponent != nullptr)
            {
                // Optionally set any properties on the component
                MyDynamicComponent->SetSomeProperty(SomeValue);
    
                // Register the component with the engine so it gets updated/rendered
                MyDynamicComponent->RegisterComponent();
    
                // If you want to attach it to an existing component, do so here
                MyDynamicComponent->AttachToComponent(GetRootComponent(), FAttachmentTransformRules::KeepRelativeTransform);
            }
        }
    }
    

    In the above example, MyDynamicComponent is a UPROPERTY member of AMyActor. It's a pointer that's initially null and gets assigned a newly created instance of UMyComponent by using NewObject. After creating the new component, it's registered with the engine using RegisterComponent, so that it becomes part of the actor's component hierarchy and receives updates. If you want to attach the new component to another component, such as the root component, you would use AttachToComponent.

    Please note that when you dynamically create components like this, they are not automatically replicated or saved/loaded with the actor unless you set those properties manually. In contrast, components created with CreateDefaultSubobject in the constructor are automatically set up for replication and serialization.

    Always remember to check if the object has already been created before creating a new one to avoid memory leaks and unnecessary object creation.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.