x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Properly using NewObject

Hi guys,

I have a UObject-inheriting class here:

alt text

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:

alt text

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?

Product Version: Not Selected
Tags:
ghsdgdghh.jpg (30.5 kB)
sdcfasdfl.jpg (19.0 kB)
more ▼

asked Jul 30 '14 at 05:59 PM in C++ Programming

avatar image

Lordink
229 47 54 61

avatar image Lordink Jul 30 '14 at 06:04 PM

To clearify things a little bit, here is a blueprint class that I inherited and my call to AddTalent func with that class as argument (which apparently still creates an UTalent object): alt text

alt text

fasd;flkjf.jpg (107.4 kB)
rfasfsfg.jpg (78.8 kB)
(comments are locked)
10|2000 characters needed characters left
Viewable by all users

2 answers: sort voted first

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.

more ▼

answered Jan 17 '16 at 04:39 PM

avatar image

kamrann
2.1k 83 33 119

avatar image Shadowriver Jan 17 '16 at 04:41 PM

Note that this question is old (1.5y actully) back when everyone was using ConstructObject

avatar image kamrann Jan 17 '16 at 04:50 PM

True, though it's worth having it updated if people still find it when searching. Updated my answer to reflect that there was nothing wrong with your original answer.

avatar image darkgaze Aug 21 '17 at 09:38 AM

Do we have to use RegisterComponent afterwards if we create a component object? And UnregisterComponent?

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

Try ConstructObject insted of NewObject

This anwser is outdated use NewObject(SomeParent_CanBeSimplyThis,UClass*)

more ▼

answered Jul 30 '14 at 06:28 PM

avatar image

Shadowriver
35.8k 926 169 1104

avatar image Lordink Jul 30 '14 at 08:58 PM

WORKS! Thanks, man. Always thought that ConstructObject is just a slower and more specific way of creating uobjects.

avatar image Shadowriver Jul 30 '14 at 09:06 PM

Ok converting to anwser then :) ConstructObject is equivlent of SpawnActor, i find it out by lurking in engine source code so it ok to use it

avatar image lagrange Sep 13 '15 at 02:44 PM

I see now that ConstructObject is being deprecated in v4.9. What is the correct method then, when ConstructObject is not available anymore?

avatar image Shadowriver Sep 13 '15 at 03:39 PM

NewObject ;] now it works ok

avatar image zulman Jan 15 '16 at 02:27 PM

Is NewObject still fine? In 4.10.1 i found that ConstructObject is ok, but NewObject is still always creates objects with basic type ignoring TSubclassOf<> argument.

avatar image Shadowriver Jan 17 '16 at 04:59 AM

What do you mean by "basic type"?

avatar image zulman Jan 17 '16 at 08:33 AM

I mean that (according to Lordink code) basic type is UTalent and we are trying to create some object of derived class (BP_Talent for example). So variable TalentClass is TSubclassOf and it's value is BP_Talent. After calling NewObject(TalentClass) instance of UTalent will be created, not BP_Talent.

avatar image Shadowriver Jan 17 '16 at 04:13 PM

You calling it like this?

 NewObject<UTalent>(SomeBlueprintClassVarableYouUse);

Also one note TSubclassOf is UClass* template type, it only limits selection of classes in editor to specific relation

avatar image zulman Jan 17 '16 at 04:16 PM

Yep, i'm calling it like this. Thanks, understand UClass* limitations

avatar image Shadowriver Jan 17 '16 at 04:34 PM

Ah i forgot, first argument of NewObject is InParent so you need to call it like this

 NewObject<UTalent>(this,SomeBlueprintClassVarableYouUse);

"this" will become owner of object, it should not have much effect on it i think so you can place whatever you like on it, you might try placing "nullptr" here if you want. I can't find NewObject in API refrence to show you that, but it should work

avatar image zulman Jan 17 '16 at 04:37 PM

Brilliant, thank you, problem was fixed :)

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question