Something like a switch statement for static meshes?

Hi all!

Sorry for the noobish question. I’m fairly new to UE4 and have been doing a lot of tutorials. I came across one lately for chopping down trees. However, the way it’s setup seems only efficient (to me anyways) for one type of foliage, as more than that would need to use a lot of Branches. I want to be able to interact with lots of different foliage, like different types of trees, rocks, etc. I think it would seem a lot cleaner if something like a switch statement (I’m not sure of another analogy) for static meshes or something exists, or something like a Branch with an “else if” part exists. Otherwise, I’d just have to use a bunch of branches.

Anyone know if these nodes exist or could give some advice here?

Here’s the section of the BP i’m talking about.

What exactly are you trying to do?

There is no “Switch on Static Mesh” tho. You have to use branches.

There’s also Select.

But it sounds more like you need to use Blueprint Interfaces.

In this case you could get the name of the instanced static mesh, then do a string op to get the last character (An integer from 1 to 4 in your case) - then feed this integer into a select node (this is basically a switch). If you drag off of your ‘class’ pin on your spawn actor node you should be able to type ‘select’ to get a select node configured to select Actor BPs - or you could just create a generic select node then plug it into the class pin and it’ll set itself up that way.

Then plug your derived integer into the selector (It starts as a bool I think but it’ll change to switch on integer or switch on enum) - then you can add more selections until you get your 4 foliage BPs.

This is the perfect situation for an enumerator. Build an enumerator for each mesh type. Add the enumerator as a variable to each actor that you’re trying to differentiate between by their mesh type. Set the enumerator to the appropriate value based on mesh type in the actor blueprint. Then, you can switch on the enumerator instead of having a bunch of branches.

Ultimately, using switches will work but it is tedious to scale and maintain. Every time you add a new foliage type you’ll need to maintain your enum and add a new static mesh to the switch. This might be tolerable if you just want to use 4 and never add anything else, but I agree with cyaoeu that an interface would be a better solution.

Using an interface, you wouldn’t need to bother trying to figure out what kind of foliage it is. Simply send a “Chop” message using the interface and, if the interacting actor is implementing that interface, you can receive the message and respond appropriately.

To add to this, I would probably have a base class for your foliage, maybe “BaseFoliage” and your trees can be children of this base class. Then have the base class implement the interface and you can have all your common foliage functionality handled in the BaseFoliage (instead of copy/pasting the same code into each foliage type).

There will likely be differences between each type of foliage. Like maybe the number of chops needed to take it down. You could define an integer in your BaseFoliage class and it will be available in all the children. Then, you can go into each child and set the default to be a value appropriate to that type of foliage. When you handle the “Chop” message in the BaseFoliage, you can read from this integer and it will handle it appropriately.

Ok, I’m rambling on because I don’t want to go back to work but I hope this gives you some ideas.

Would an array or map of static mesh objects work?

20 years later…
Did you ever get a solid solution for this? I am also in this boat.

I appreciate when people respond with, “well, what i’d do is take the bpi and the abp and enum struct thinga-ma-jig and connect it to the rumplstiltkin apparati and next thing you know wham bam thank you mam, you got your self a toaster…” But i gotta google all that ■■■■ and figure out which one to use, and how to implement it, when more consntructive is resource links, ala tutorials/youtube videos/ a few screen shots.

this doesn’t come from a place lacking in gratitude. it’s just that not everyone is as advanced as the people answering, so it helps to keep in mind that while you may be answering the question, to a lot of newbies, you’re answering in greek.

I keep getting, ‘use an enum , a for loop and a switch’ i’m 45 minutes into video tutorials and still havn’t seen how you put a static mesh of a tree into an enum, and compare it to the result of the trace hit, then how to put my interactive blueprints of tree’s into an enum and load the right one in place of the static mesh that line trace has hit…

I also have 12 branches individually checking if line trace hit is ‘mesh a destroy and replace with blueprint a’ next branch, ‘mesh b? destroy and replace with blueprint b’ next branch.

basic version:
From a new top down template.
Create a new enum “MesheNameEnum” inside add manually each mesh name.

duplicate “1M_Cube” 2 times, name it Mesh 1 and mesh2. Set the material of the second mesh to a different texture

Create a new actor, Add a static mesh component, and choose Mesh1 on the right tab, add a new variable type enum, the enum you just did before, set it to the mesh name you want.

For this tutorial, set the static mesh object type to world static.you need to set to custom collision first

Now for the line trace, default setup here is from a top down template. Inside your controller:

Now add your master mesh inside your level. Hit play, spacebar with the mouse over your mesh.
At the end of the line trace logic (inside the controller) you could instead spawn a new actor from the old actor location and destroy the old actor. Still need a “set static mesh”

Better version:
do no set the static mesh and enum inside your “master mesh”. It is the parent of all your meshes, it just hold the variables that’s all.
For each type of mesh (walls,floors,etc), you create a child blueprint from your master mesh and then set the variable inside this child.

EDIT:I was going to upload the project but then the map crash. Sorry. I had to redo everything because I moved the folder. Some variables might have a different name on the screenshots. Don’t mind the white cube in the screenshot, it’s still your master mesh