# Is there a way to "Switch On Class"?

**URL:** <https://forums.unrealengine.com/t/is-there-a-way-to-switch-on-class/319644>\
**Category:** Platform & Builds\
**Tags:** Switch, unreal-engine, UE4, question\
**Created:** [May 24, 2015, 6:20pm UTC](https://forums.unrealengine.com/t/is-there-a-way-to-switch-on-class/319644 "2015-05-24T18:20:52Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![anonymous\_user\_e815e8ea](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_e815e8ea](https://forums.unrealengine.com/u/anonymous_user_e815e8ea)\
**Post date:** [May 24, 2015, 6:20pm UTC](https://forums.unrealengine.com/t/is-there-a-way-to-switch-on-class/319644/1 "2015-05-24T18:20:52Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![Mhousse1247](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Mhousse1247](https://forums.unrealengine.com/u/Mhousse1247)\
**Post date:** [May 24, 2015, 6:51pm UTC](https://forums.unrealengine.com/t/is-there-a-way-to-switch-on-class/319644/2 "2015-05-24T18:51:16Z")

</div>

You can create a macro like this

 ![44021-m0.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/c/a/b/cabaccc7f3e49233628d9d22f21291f91b3c6ef1.png)

 ![44022-m1.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/3/5/b/35b6386d797d766593925eb23f3c9f400424f0d6.png)

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_e815e8ea](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_e815e8ea](https://forums.unrealengine.com/u/anonymous_user_e815e8ea)\
**Post date:** [May 24, 2015, 7:30pm UTC](https://forums.unrealengine.com/t/is-there-a-way-to-switch-on-class/319644/3 "2015-05-24T19:30:00Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![scrcro](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/scrcro/32/1008648_2.png) [@scrcro](https://forums.unrealengine.com/u/scrcro)\
**Post date:** [December 17, 2015, 12:14pm UTC](https://forums.unrealengine.com/t/is-there-a-way-to-switch-on-class/319644/4 "2015-12-17T12:14:31Z")

</div>

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’

 ![70993-capture.png](https://d3kjluh73b9h9o.cloudfront.net/original/4X/0/a/d/0ade90422ab0a25a773cd07ff0d696d550a43011.png)

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

---

<div class="post-metadata">

**Author:** ![Arty-McLabin](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/arty-mclabin/32/1012076_2.png) [@Arty-McLabin](https://forums.unrealengine.com/u/Arty-McLabin)\
**Post date:** [January 17, 2016, 9:59pm UTC](https://forums.unrealengine.com/t/is-there-a-way-to-switch-on-class/319644/5 "2016-01-17T21:59:12Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ScottSpadea](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/scottspadea/32/1194682_2.png) [@ScottSpadea](https://forums.unrealengine.com/u/ScottSpadea)\
**Post date:** [January 18, 2016, 7:21am UTC](https://forums.unrealengine.com/t/is-there-a-way-to-switch-on-class/319644/6 "2016-01-18T07:21:54Z")

</div>

use interface functions:

[![]( " - YouTube") ](https://www.youtube.com/watch?v=WwfsWr4A894)

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.

---

<div class="post-metadata">

**Author:** ![anonymous\_user\_1ea4bc78](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@anonymous\_user\_1ea4bc78](https://forums.unrealengine.com/u/anonymous_user_1ea4bc78)\
**Post date:** [November 7, 2018, 8:45pm UTC](https://forums.unrealengine.com/t/is-there-a-way-to-switch-on-class/319644/7 "2018-11-07T20:45:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pippin007](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@pippin007](https://forums.unrealengine.com/u/pippin007)\
**Post date:** [December 14, 2018, 3:31pm UTC](https://forums.unrealengine.com/t/is-there-a-way-to-switch-on-class/319644/8 "2018-12-14T15:31:45Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![KazemAkhgary](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@KazemAkhgary](https://forums.unrealengine.com/u/KazemAkhgary)\
**Post date:** [September 6, 2019, 7:26am UTC](https://forums.unrealengine.com/t/is-there-a-way-to-switch-on-class/319644/9 "2019-09-06T07:26:45Z")

</div>

Not safe if you decide to rename a class

---

<div class="post-metadata">

**Author:** ![Jenkins\_Gage](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/jenkins_gage/32/677675_2.png) [@Jenkins\_Gage](https://forums.unrealengine.com/u/Jenkins_Gage)\
**Post date:** [July 7, 2022, 6:24am UTC](https://forums.unrealengine.com/t/is-there-a-way-to-switch-on-class/319644/10 "2022-07-07T06:24:29Z")

</div>

![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/3/9/239d9f8e2a3ff40754a7196f417254be9c83ce0a.png)  
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.

---

<div class="post-metadata">

**Author:** ![Tom8788](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@Tom8788](https://forums.unrealengine.com/u/Tom8788)\
**Post date:** [September 10, 2022, 12:15pm UTC](https://forums.unrealengine.com/t/is-there-a-way-to-switch-on-class/319644/11 "2022-09-10T12:15:29Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![PixelFireXY](https://d1ap1mz92jnks1.cloudfront.net/user_avatar/forums.unrealengine.com/pixelfirexy/32/1195765_2.png) [@PixelFireXY](https://forums.unrealengine.com/u/PixelFireXY)\
**Post date:** [June 11, 2024, 9:03am UTC](https://forums.unrealengine.com/t/is-there-a-way-to-switch-on-class/319644/12 "2024-06-11T09:03:50Z")

</div>

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.

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/8/a/9/8a9fecd7251f4be49cb2838f96b7e2184e1a05a6.png)  
And then you call the macro like this:  
 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/b/4/e/b4eed924724a131b30ed6048f291685bf8e6f034.png)

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

---

<div class="post-metadata">

**Author:** ![jmac11927](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@jmac11927](https://forums.unrealengine.com/u/jmac11927)\
**Post date:** [July 2, 2024, 7:38pm UTC](https://forums.unrealengine.com/t/is-there-a-way-to-switch-on-class/319644/13 "2024-07-02T19:38:10Z")

</div>

![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/f/6/b/f6b609c486fd7f86d3b5f57ca2fcd68f978ad246.png)

You can use simple boolean logic to make a macro - so no execution nodes are needed.

---

<div class="post-metadata">

**Author:** ![giodav2015](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@giodav2015](https://forums.unrealengine.com/u/giodav2015)\
**Post date:** [December 29, 2024, 10:07pm UTC](https://forums.unrealengine.com/t/is-there-a-way-to-switch-on-class/319644/14 "2024-12-29T22:07:11Z")

</div>

Just use something like this

 ![Screenshot 2024-12-26 030936](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/7/7/e7746c7634881491ce61769a33f79da3509cd7a6.png)
