Blueprint Node: Create Object from Blueprint

From Epic Wiki
Jump to: navigation, search

Template:Rating

Overview

This snippet shows you how to make a custom blueprint node that instantiates an object from a blueprint (that extends Object). It was created with assistance from MonsOlympus.

Being able to instantiate an object class from Blueprints like this is useful as objects provide a nice way to store data without any having any of the overhead that actor classes create.

This snippet won't go into major detail, as you should read this tutorial first - It explains how to create your own custom nodes, and the features available (and what they do). - G-Rath (talk)

I have updated this tutorial to work for 4.8, as well as added a note about using 4.9. - joshcamas (talk)

Note: Using 4.9+

In 4.9, Epic has added a blueprint node to do just this. It's called "Construct Object from class", and it's pretty useful. If your wanting to know how to create objects within c++, or interact c++ and blueprints, this tutorial is still useful: however if you want to create an object from blueprint, I advise you use the new built in blueprint function. Good luck!

The Code

CustBluePNode CreateObject.jpg


(Note that the "HyperPro.h" should be replaced with your project name) CPP file: <syntaxhighlight lang="Cpp"> //Copyleft under the creative commons license //For details see http://creativecommons.org/licenses/by-sa/4.0/

  1. include "HyperPro.h"
  2. include "CreateNewObject.h"


UObject* UCreateNewObject::NewObjectFromBlueprint(UObject* WorldContextObject, UClass* UC) { UWorld* World = GEngine->GetWorldFromContextObject(WorldContextObject); UObject* tempObject = NewObject<UObject>(UC);

return tempObject; } </syntaxhighlight>

Header file (.h): <syntaxhighlight lang="Cpp"> //Copyleft under the creative commons license //For details see http://creativecommons.org/licenses/by-sa/4.0/

  1. pragma once
  1. include "Kismet/BlueprintFunctionLibrary.h"
  2. include "CreateNewObject.generated.h"

UCLASS() class UCreateNewObject : public UBlueprintFunctionLibrary { GENERATED_BODY()

UFUNCTION(BlueprintPure, meta = (HidePin = "WorldContextObject", DefaultToSelf = "WorldContextObject", DisplayName = "Create Object From Blueprint", CompactNodeTitle = "Create", Keywords = "new create blueprint"), Category = Game) static UObject* NewObjectFromBlueprint(UObject* WorldContextObject, UClass* UC); }; </syntaxhighlight>

Result in editor

Using a Blueprint extending object named "TestObject":

CustBluePNode CreateObject Layout.jpg

CustBluePNode CreateObject Testing.jpg