Is there a way to "Switch On Class"?

You can create a macro like this

Hi! For my game I’m trying to make the blueprint code for when you pickup something on the ground. I have it set so that after you come into range and press a certain key, it gets the result of a sphere trace. I want to get the class of the hit actor, and then switch into different possibilities based on the other class. Currently the way I’m doing it is using a bunch of branch statements, but it looks ugly and is a bit annoying. Is there any way to do a switch statement using class?


Thanks, unfortunately that’s essentially what I’m doing (can’t use cast). Do you think I could post this over at the feature request section in the forum? Or is there a reason for this?

Not sure if you’re still looking for a way to do this, but here’s how I’ve been doing a ‘Switch on Class’

To get it to work, I had to take the class name, and append _C to it in the switch case.

1 Like

quick note: beware this version will return FALSE on a superclass:
if C inherits B, C instance won’t be seen as a B instance (which it is as well!) because of the name difference.

use interface functions:

you should call an interface function on the other actor, and let the other actor perform some logic. you could have the interface function return an integer or name identifier, so you can do a switch case, but that means each new pickup actor you add, you might have to update the character as well.

its usually better to make actors know how to use interface functions, rather than knowing how to talk to many specific actors.

You are my hero! I was elbow deep in environmental queries. This is sooo much simpler

Create a function and have hit on actor with equal to class nod the branch and check that like he did on the macro, that’s what I did.

Not safe if you decide to rename a class

I try to achieve SwitchOnClass like this. Create an array that contains the classes you want to switch on and use a FindItem node to find the input class from this array and then switch on the result int. This way if you won’t change the orders of your array items it’s totally safe.

1 Like

You could also simply cut off the last two letters of the class name with Left Chop

I know this post is old but I found a solution to compare which Sense is which and keeping a reference comparison at the same time. Just create a macro and do the reference comparison inside it.

And then you call the macro like this:

This is gross, I know… but at least you do not compare class names with strings but with references.