NewObject

Convenience template for constructing a gameplay object

Windows
MacOS
Linux

References

Module

CoreUObject

Header

/Engine/Source/Runtime/CoreUObject/Public/UObject/UObjectGlobals.h

Include

#include "UObject/UObjectGlobals.h"

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;  
}
Select Skin
Light
Dark

Welcome to the new Unreal Engine 4 Documentation site!

We're working on lots of new features including a feedback system so you can tell us how we are doing. It's not quite ready for use in the wild yet, so head over to the Documentation Feedback forum to tell us about this page or call out any issues you are encountering in the meantime.

We'll be sure to let you know when the new system is up and running.

Post Feedback