Download

Cast to unknown actor blueprint ?

I have a selected actor promoted variable which can be any object selected, but can’t figure a way to Cast To without know the Blueprint ?

Not sure exactly what you mean…

From a hit result, actor promoted to variable for reference. I need to cast to it but the object can be any selected actor (in play).

Use Get Class from the actor and then do a switch.

It’s smarter to think less about the object you have and more about the object you need. For instance, if you wanted to open a door, you’d cast to a door. If you wanted to shoot a target dummy, you’d cast to a target dummy. You can chain cast nodes off their ‘cast failed’ pin. If it’s just a static mesh actor, it won’t pass any of the casts. Then, for the specialized logic, use the pin on the cast node. ‘As ____’.

Thanks, but what if I don’t want a fixed list ?

If this thread isn’t entirely dead yet, I’d like something similar. I’m trying to get an actor (A) to get a value from an actor (B) on hit, modify that actor’s value based on a value in the actor (A) when actor (B) could be one of several different things.

This is where class inheriting is valuable. Have the many different types of Actor(B) inherit from a parent class, stuff the variable they all have in common inside there (or setup a common overideable function that retrieves that variable if that variable can be different for each sub-class). Then cast to parent class of Actor(B) and you should be able to get the data you need.

OOP is our friend :slight_smile:

Yes it certainly is, Interfaces could be an option here if you want to extend just Actor but still ensure any subclass has the appropriate methods defined :cool:

Do interface, do function in it. However you need to implement that function (make its code) in every blueprint you want to work with interface.

Then from outside of that blueprint with interface, you get its reference then cast to INTERFACE instead of blueprint class.

I did exactly that in my action RPG prototype.

I wanted to have a mouse hover detection for the player.

Every actors that can be highlighted by a player have the interface “MouseHovering”. Each actor has his own hovering function that triggers off when the player is hovering them.

On Tick, I get the mouse cursor hit actor, check if it implements the “MouseHovering” interface, if it does, I know this is an actor the player can interact with.