No announcement yet.

Something like a switch statement for static meshes?

  • Filter
  • Time
  • Show
Clear All
new posts

    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.

    Click image for larger version

Name:	nlpMzHL.png
Views:	1
Size:	384.5 KB
ID:	1225506

    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?
     - S P A G H E T T I: Ninja Swords, Guns & Monsters & Other S**t! - Join Our Discord