Get custom BP from C++

Hey,

I have a UObject custom class UMyClass and used this class as base class for my BP
.in this BP i set some defualt variablse etc…

Then I have another class e.g. AMyCharacter and I want to be able to set any BP with base class UMyClass inside the Editer in AMyCharacter.

right now i do this with c++ only

in AMyCharacter.cpp

UMyClass* mybp = NewObject<UMyClass>();

.I’m sure i need a pointer to UMyClass but i don’t know how to be able to set this pointer via the editor.

Make varable

TSubclassOf<UMyClass> SomeClass

You could use UClass* insted but with this it will limit selection in editor to specific relation. And then to create object

UMyClass* mybp = NewObject<UMyClass>(this, SomeClass, NAME_None, 0);

In editor when you create blueprint from UMyCharacter, you select class in defaults which can be blueprint, keep in mind that only varables defined in C++ will be accessable (easily, as you could get UProperty and then set and get each, but thats kind of messy to do)

tried it with MyClass* Obj = NewObject < SomeClass > () :frowning: … Thank you :))

Btw: are Objects garbage collected with newobject<…>?

hmm wait somehow it’s not working as i want.:

lets say I have a base class and child class,
In My Character class I have a pointer of my base class and want to assign the child class BP from the editor to it, so i want to be able to assign child1, or child2 or child3 to it
,

 TSubclassOf SomeChildClass //choose child in editor
 Base* mybp = NewObject(SomeChildClass );

But i think this just won’t work, and only create a Base class object ?

You just think it does not work? It should work, the template part of NewObject is just to auto cast and if you dont use mroe arguments it sets class to spawn

Sorry for late reponce ^^’

hmm, I tried it again, but somehow the Object which i created with NewObject<> doesn’t have the values which i set inside the editor. It always has the values from its constructor initializer list.

Then check what class is actully spawns, type in console “getall youclassname” and it should show all instances of the class in memoery

Ok by anwsering other quastion i think i find fault why it didnt work, first argument is parent and it will accept UClass object too that why you didnt get any error. I fixed the anwser

ok it seems to work :slight_smile:
what are the 3rd and 4th parameter for?

(this, SomeClass, NAME_None, 0) these 4 paramters doesn’t work, because there is no matching function call,it works with the first 2 or 3.(this,class) / (this,class,NAME_None)

btw: Are object created with NewObject “garbage collected”?

Yes, more then actors as actors are tied to world never gonna be collected