NewObject

Syntax

template<class T>
T * NewObject
(
    UObject * Outer,
    UClass * Class,
    FName Name,
    EObjectFlags Flags,
    UObject * Template,
    bool bCopyTransientsFromClassDefaults,
    FObjectInstancingGraph * InInstanceGraph
)

Remarks

Convenience template for constructing a gameplay object

Returns

a pointer of type T to a new object of the specified class

Parameters

Parameter

Description

Outer

the outer for the new object. If not specified, object will be created in the transient package.

Class

the class of object to construct

Name

the name for the new object. If not specified, the object will be given a transient name via MakeUniqueObjectName

Flags

the object flags to apply to the new object

Template

the object to use for initializing the new object. If not specified, the class's default object will be used

bCopyTransientsFromClassDefaults

if true, copy transient from the class defaults instead of the pass in archetype ptr (often these are the same)

InInstanceGraph

contains the mappings of instanced objects and components to their templates

Example Code

MyComponentSpawner.cpp at line 75:

USceneComponent* AMyComponentSpawner::SpawnNewComponent(UClass* ComponentClassToSpawn, const FTransform& SpawnLocation)  
{  
    //Make sure it is a scene component since we are going to attach it to our root component  
    check(ComponentClassToSpawn->IsChildOf(USceneComponent::StaticClass()));  

    // Construct an object of the class passed to us but return a point to USceneComponent  
    USceneComponent* Component = NewObject<USceneComponent>(this, ComponentClassToSpawn);  

    //Attach the component and position it at the location given  
    Component->SetupAttachment(RootComponent);  
    Component->SetWorldTransform(SpawnLocation);  

    return Component;  
}

References

Module

CoreUObject

Header

Runtime/CoreUObject/Public/UObject/UObjectGlobals.h