Download

Why is the 'Construct Object from Class' node not accepting (any) classes?

Hi,

I have some classes that ultimately derive from UObject. Now I want to create instances of said classes within a blueprint. For objects that are not actors, there is a ‘Construct Object fom Class’ node for that purpose.
However, I only get this blueprint compilation error:

I found a post on AnswerHub mentioning that the class must be directly derived from UObject, but even then I only get this error.
What am I missing here?
Any help is appreciated :slight_smile:

Cheers,
Klaus

The class must be marked as BlueprintType, since you want to use the result in blueprint. It’s a bit silly how it lets you select classes you can’t actually use.

@Zeblote
Awesome :smiley: Works like a charm. Thanks.
And not only just with direct descendants of UObject (as the AH post suggested), but with any class ancestor that inherits from a ‘BlueprintType’ class.
Once I added ‘BlueprintType’ to my GameItem class, I can hold all kind of descendants in the array and identify them by their type :smiley:

Yeah. If you omit the ‘BlueprintType’ specifier you cant select it as a parent in the BP wizzard dialog but here the class is just available for selection.
Also the documentation for that node is quite sparse (or I didnt look in the right places).

How do you set BlueprintType? Is there a way to do that right inside Blueprints, or does it require C++?

@Rooster128 This is a C++ specifier that goes into the class definition.

If you’re wishing to go with a more robust and testable approach, you could use a combination of classes and a factory pattern to issue the object. Check out the following, you’ll see that with this setup, including more granular logic and testing down the road would come easier and afford you greater flexibility. You’re also abstracting away new-ing up the object yourself behind the .issue call - which can be beneficial and convenient in some cases.

I also notice that you mention your PHP background, so I’m also showcasing a bit of how an object-orientated approach can be taken in ES6.
class AbstractShape { constructor(type) { this.type = type; } getType() { console.log(I am a ${this.type}); } } class Square extends AbstractShape { constructor(type) { super(type); this.sides = 4; } getDescription() { console.log(I have ${this.sides} equal sides); } } class ShapeFactory { static issue(type) { switch(type) { case ‘Square’: return new Square(type); break; case ‘Circle’: /* same pattern with a Circle class / break; } } } let shape = ShapeFactory.issue(‘Square’); shape.getType(); / I am a Square / shape.getDescription(); / I have 4 equal sides */

What if your type is blueprint without any C++? I have some components that I need to create and add at runtime, but it refuses to let me .

@crossmr

Are you using the “Add component” node?

No, because add component is component specific, you can’t pass it a class to add. I ended up creating a C++ function that can take in a class and add it.

@crossmr Is it a blueprint node? If so, could you share your implementation? I have a project with different attack styles from unit to unit (ex. melee,range,AoE) but they’d all have an “Attack” component to reference in the rest of the code.