Get Default Object of Class

I know every UClass has a default object, but how do I access this from blueprint? At the moment I just spawn an instance of the object at 0/0/-100000 where it cannot be seen by the player, but thats obviously not the way it should be done. But I really cannot find any “Get Default Object” or “Get CDO” node.

Answer would be appreciated :confused:

I had this problem too the only way to solve this so far build a function or expose it in c++

Add Code to your project derived from the BlueprintFunctionLibrary

HelperNodes.h


// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "Kismet/BlueprintFunctionLibrary.h"
#include "HelperNodes.generated.h"

UCLASS()
class MYGAME_API UHelperNodes : public UBlueprintFunctionLibrary
{
	GENERATED_BODY()
	
public:	
UFUNCTION(BlueprintPure, BlueprintCallable, Category = "Object")
void GetDefaultObject(TSubclassOf<UObject> ObjectClass, UObject & DefaultObj);

};


HelperNodes.cpp



#include "MyGame.h"
#include "HelperNodes.h"

void UHelperNodes::GetDefaultObject(TSubclassOf<UObject> ObjectClass, UObject & DefaultObj) {
if (ObjectClass)
DefaultObj = ObjectClass->GetDefaultObject();
}


well even thou this wasnt bp :slight_smile: hope this helps

2 Likes

Thanks, it would help, but I don’t want to add any code to my project :slight_smile:

You can have this as a plugin

Sorry to necro an old thread, but this code doesn’t work and I am trying to get something figure out that does. Can anyone help me?

for one, BPFunctionLibrary methods need to be static as far as I understand.

Epic has added this to 4.9, so if you can be a bit more patient you don’t need the code :cool:

Yeah so I heard, but I have no idea when 4.9 is coming, and really don’t want to sit on my hands until then.

Ok I think I have this working with the following code:

Header:



UFUNCTION(BlueprintPure, BlueprintCallable, Category = "Object")
static UObject* GetDefaultObject(TSubclassOf<UObject> ObjectClass);


CPP:



UObject* USetiBlueprintLibrary::GetDefaultObject(TSubclassOf<UObject> ObjectClass)
{
	return ObjectClass.GetDefaultObject();
}


I say think because yes it does appear to be working, but for some odd reason I can’t actually save my Blueprint now due some very odd error.



Can't save G:/Documents//Development/seti_prototype_1/Content/Blueprints/Items/ItemManager.uasset: Graph is linked to private object(s) in an external package.
External Object(s):
/Engine/Transient
  
Try to find the chain of references to that object (may take some time)?


As a bonus, if you mark up the method as follows, the returned object will be of the right class too.



UFUNCTION(BlueprintPure, Category = "Utilities", meta = (DeterminesOutputType = "Class"))
static UObject* GetClassDefaultObject(UClass* Class);


5 Likes

Please Use “Get Class Defaults” in 5.1.

1 Like