Properly using NewObject
I have a UObject-inheriting class here:
As you can see, I have an implementable event there. My idea is that I inherit several classes from this one using blueprints, and implement this event differently in each of them. So, I created a function that accepts a subclass of this UTalent class, creates object of this subclass and calls its ReceiveInit function:
However, when I create a Blueprint which inherits from UTalent, and call this AddTalent func from blueprint, passing the created blueprint's class as argument, the ReceiveInit is not being called.
When I dug deeper, I found out that my way of using NewObject, apparently, creates simply a UTalent object, instead of instance of my blueprint's class. I found it out simply by changing some properties in inherited class. While debugging AddTalent func, I saw that talent object has same property that were set in UTalent class, and not the ones in my blueprint class. So, am I using the NewObject wrong, and how to properly create an UObject using a TSubClassOf thing?
asked Jul 30 '14 at 05:59 PM in C++ Programming
Since this question was initially posted, ConstructObject has been deprecated and you should now always use NewObject. You need to be careful with the arguments though. The first argument is the object to use as the outer (think of it as the owning object) for the newly created object. If you want to specify a particular class to use, pass that as the second argument.
The overloads are not ideal - UClass is actually a descendant of UObject, so if you pass a UClass* as the first argument, this is an acceptable outer, and since the rest of the parameters have defaults, it will compile, but it won't be doing what you intend.
Follow this question
Once you sign in you will be able to subscribe for any updates here