Cast to wildcard/class?

Hey there,

Is there anyway we can have a generic cast node (like we get in C++) in blueprints to cast to the input class rather than hardcode the class we want to cast to?

I understand there might be some limitations from blueprints being more of a “scripting” language, it’d still be quite useful.

301245-capture-decran-66.png

Not really.

Because the whole point of casting is that you’re choosing at which level to interpret the object you’re faced with.

For instance, make a BP, MyBP. And the make a child from that MyChild.

Both of them are still actor class.

So if you passed a reference to your object to this magic function, how is it supposed to know whether you want the actor, you MyBP, or MyChild?

Something which may allieviate the pain of casting a little is pure casting, don’t know if you’ve seen that. Instead of:

301246-cast.jpg

You can have:

301247-pure.jpg

So at least it’s not in the execution chain.

EDIT: Also, your node there is basically the same as a conventional cast :slight_smile:

Yep, in which case if you create a new child, you have to remember to add the cast to the new class in that function!

I was thinking of using a wildcard output pin in a macro library.
But I’m guessing I might resort to one function per class or the cast waterfall just to keep it somewhat simple aha

Mmmh … I get your point. But imagine the following:

I have a parent class to roughly 5 classes. They’re all “singletons”, so I want to create a “get” function that would return a reference to any of the 5 instances based on an input parameter (which would be a class type). Now the only way I can see myself doing that is by manually adding a cast node for every class, which I don’t really want to do especially if I end up adding more than just the 5 classes.

If I could specify the class to cast to, I’d only need the one node. My only worry would be to get a reference to each instance to pass in as object.

Does it kinda make sense?

Thing is, if you have to specify the class on your node like this, it is a cast. You had to supply the actor and class, just like you do with a normal cast…

Are you saying, you don’t want the hassle of figuring out which one of the 5 it is? That you can do something about…

Yeah, say I had an array of these objects (the type would be their common parent), I’d let the cast node figure out which one is the one that matches the class I want and return a pin “casted” to this class. I’m picturing this working like the GetComponentByClass node, you simply input the class and it does everything for you.

I think you need a specific example. You can’t have a function or macro that just returns a variable of the class you want. Unless you have one for each class, and you’re back to square 1.

But, for instance, if they all the classes had an integer variable, and you wanted to get that or set that. Then you can do that sort of thing:

Which you can use like:

301265-in-use.jpg

This is a great question!
Did you feature request this somewhere ?

How did you create the node screenshot ? Do you share the code if you have a working solution ?

I made it, but it’s only for actor not for object

Sadly the node I made for the screenshot was made with a macro, it doesn’t actually work. :slight_smile:

And no, I haven’t made a feature request for this.