Cast object to class variable in Blueprint?

I am wondering if there is a way to cast an object reference to a class by a class variable reference.

This is possible in Blueprint it seems for classes using the following:

However, there does not seem to be an equivalent node for some reason to return an object reference (only a class reference). I can use a regular cast node to do this most of the time, but this requires that I know in advance the type of class I want to cast to, and it therefore can’t be set later since there is no option to do the same as above and return an object reference of that class variable type.

Any way around this limitation? Seems like an oversight to not have any node for such an obvious use-case. I could use a BPI, but this requires a lot of maintenance since it needs to be added to every class that would want to use this functionality…and anyways, if “Cast to Class” already exists, surely there is a node for this returning an object reference of that class and I just can’t find it?

EDIT: To clarify - I am aware that you cannot objects dynamically. This is mostly for convenience of being able to set a character class through class defaults. But perhaps because people would try to set it at runtime is why it may not exist?

Do you mean you want GetClass() ?

No; I want to get an object reference of that class. I.e., Cast to class, but with a class variable input. Right now this is not possible it seems?

There are separate cast nodes for each class - but not a single cast node that lets you choose a class to cast to. Hopefully that makes sense.

image

There is a variant (in my original post above) that lets you do this and returns a class reference, but not one that lets you do this and returns an object reference.

but, what do you want to do? this looks like an XY problem

maybe If you don’t know the class to cast, you could use an interface

I did say right above that an interface is not the most convenient :'D This has already been addressed.

To clarify - what I want to do is avoid setting this up using interfaces. This is for a component class that could be added to any character.

Now, part of the point of a composition is that you design something that can be added to any actor class. This would achieve the goal of composition; a BPI will not. It would need to be added on a case-by-case basis, and the above node (if some alternative exists) would avoid that blueprint limitation.

You can Use GetComponentByClass() then?

(post deleted by author)

That’s the idea I’m going for, yes - so that the component can be retrieved this way later without having to also add BPIs etc. to the actor that should use it.

However that still doesn’t return a reference to an actor that the component goes on. That would return a component only, not a reference to any other class - for example a character or another actor whose class you might want to specify by variable that would be required to use the component.

That doesn’t make sense unfortunately.
The point of casting is to access specific properties and functions that only exist in the casted class type.
If your class is dynamic, which properties/functions are you gonna use ?
If your goal is just to test inheritance, use ClassIsChildOf
image


Cast doesn’t magically give you an object reference out of nowhere.
It transforms the object reference you give as input, after checking that it is of the right class.
Cast is merely a way of checking validity of a given object, before accessing properties/functions that are specific to a certain subclass.


There must be an existing object of that class in the first place, instanced in the world.
If there is, you can use GetActorOfClass (for actors) or GetAllWidgetsOfClass (for widgets) or GetComponentByClass to attempt to find it.
All of these accept a variable class input :
image


If you have a component, you also have its owning actor.
GetComponentOfClass requires an actor input, so technically you already have the actor reference when you call that.
But even if you don’t, you can still use GetOwner on the component to go back to the actor :

2 Likes