Download

Alternative to MANY If/Branch statements?

What is a good alternative to avoid using a lot of If statements? I basically have a series of buttons that execute when clicked on a UI. Basically it has a chain of “if statements” until a certain button is clicked. Is there an easier way instead of using a lot of if statements?

A switch maybe?

Yep, if your buttons are “ordered” as in, you have them indexed as button 0 to however many buttons you have, you could do something like this:

899697c5d2143a8d8a1fbe69f578c1d4ccf3774e.png

The bools on the left come from whatever way you are getting their clicked status, there will only ever be one clicked at any time, so finding the index of the one true bool will give you the index of the button that was clicked, you can then use a Switch On Int node, or pass that int as an argument into a function.

If all the buttons are the child of the same parent, get their index and pass that into a switch statement.

If not, create a custom widget that includes a button, give that widget an index and pass this index into a switch statement.

Or create a custom widget that includes a button.
This would allow you to use one or more dispatchers based on the logic contained in a single place (the custom widget).

Thanks for the feedback guys!