Ue4 C++ select and switch equivalent?

I use the select and switch nodes a lot in blueprint. What’s the C++ equivalent to these?

To accomplish a select-style assignment, use a ternary operator.

FString BoolAsString = bMyBool ? "true" : "false";

It’s shorthand for if bMyBool is true, then this string will be set to “true” and if it’s not, it will be set to “false.” This can be used for virtually anything. Basically a statement which resolves to true or false comes before the ? and the true outcome comes right after. The false statement comes after the : .

Switches in C++ are switches, they just have a different syntax.

FString EnumAsString;

switch (EnumType)
{
    case Enum_Type1 :
        EnumAsString = "Type 1";
        break;

    case Enum_Type2 :
        EnumAsString = "Type 2";
        break;

    case default :
        checkNoEntry();
}

In this scenario we have a switch and the condition being tested, which is asking “which enum value is assigned to the variable named ‘EnumType’?”

We then write a case for each enum type and if the condition matches the case, we execute the code underneath the case which is assigning a value to a string in this case.

You have to be sure to break after executing a case in order to get out of the switch otherwise you’ll execute all code below as well.

‘case default’ refers to what the switch should do if the the condition doesn’t match any of the cases specified (so you can skip certain cases or create a catch-all method for anything that doesn’t match). In this case I’ve put a checkNoEntry() macro that will intentionally crash the game because this enum only has two types and I’ve accounted for both cases in the switch, so if it’s neither of these cases something has gone very, very wrong. But you could do anything in the default case you want to, just be sure to break afterward. I didn’t break in this case specifically because checkNoEntry() stops the game from executing any further.

2 Likes

Thank you for the response! If I wanted to use an enum or int in my select, could I do that or is this limited to bools?

Also useful that checkNoEntry() stops sessions. Appreciate you!

1 Like

Switch statements work with basically anything that has an integer equivalent. Enumerations, const values, etc.

Ternary operators only work on booleans, but those booleans can be calculated at runtime. Like, assume we have an EVeggieType enum, with two possible values: “Potato” and “Tomato”. Then let’s say we’ve got an instance of that enum called ThisVeggie:

FString VegetableName = (ThisVeggie == EVeggieType::Potato) ? TEXT("Potato") : TEXT("Tomato");

Because ThisVeggie == EVeggieType::Potato will also resolve as a boolean value, you can use it in a ternary operator.

It’s worth noting that in C or C++, = is an assignment, while == is for testing equality. So this would assign the value Potato to ThisVeggie:

ThisVeggie = EVeggieType::Potato

While this would be a boolean value of either true or false based on whether or not ThisVeggie already is a potato:

ThisVeggie == EVeggieType::Potato

One of the most common careless mistakes when starting out is to make a typo and miss that second equals sign. Thankfully, most compilers will catch that and warn you, but some folks prefer to flip the equality test:

EVeggieType::Potato == ThisVeggie

That will return the same true or false value as previously – both sides are equal! – but if you made a typo and omitted the equals sign:

EVeggieType::Potato = ThisVeggie

…will not compile, because you cannot assign a new value to what “potato” actually is.

This does, however, have the downside of making code feel like it was written by Yoda when experienced programmers read it. (“A potato, this veggie is. Mm, yes.”)

If you’re diving into C++, I highly recommend picking up a couple of books to start out in it. I’m told that Packt Publishing’s “Beginning C++ Game Programming” is not a bad one, as I guess all the examples are towards building a simple 2D game. But having never flipped through it, I’ve got no personal opinion one way or the other.

2 Likes

Packetdancer pretty much covered it, but to expand on this you can nest ternary statements if you really want to, like this:

FString VegetableName = (ThisVeggie == EVeggieType::Potato) ? TEXT("Potato") : 
    (ThisVeggie == EVeggieType::Tomato) ? TEXT("Tomato") : TEXT("Something else");

But if you have a lot of cases it’s more readable and easier to scale if you use a switch instead.

2 Likes

thank you both for the answers. Ive been using BP for about 4 years now so i have a grasp of programming BP wise and its transferred over to C++ pretty well so far, but would it still be worth it to relearn the basics in terms of C++ with this in mind?

I have seen a piece of embedded firmware that did an entire state machine in one giant ternary operator.

It was like looking into the abyss; I felt a chill in my soul. Whoever wrote that code was some form of ancient evil merely adopting a human guise, bent on inflicting torment and suffering on engineers who would one day inherit that codebase.

Please, anyone reading this thread… never be that person.

2 Likes

I’m going to frame this post

2 Likes

Yes, definitely. There’s so much that differs between C++ and BP under the surface, things like garbage collection, dangling pointers, memory management, all that annoying crap. Without some formal instruction (even from a lynda tutorial) you’re gonna find yourself crashing a LOT and not really know why.

2 Likes

*quiet traumatized sobbing*

That aside… Klught, I’m going to second Jared’s recommendation here. It is absolutely worth it.

I have a fun story; I was helping to write and run an online multiplayer game at one point. Another person – who’d taken programming courses, though they were all in java – started to pitch in. The sandbox version of the game he was using kept crashing, though, and he was really puzzled as to why. We sat down to look over the code with him… and his use of pointers was… confusing. When we asked him about his logic, he admitted “I don’t really know why sometimes stuff has an & or an * with it, so I’ve mostly been guessing.”

Java, after all, does not distinguish pointers because everything is a pointer.

So, yeah; knowing programming in one environment is not always enough to really have a full handle on it in another. It is definitely worth some quality time with a C++ book; that’s a chunk of time spent now to avoid a much larger chunk of time spent tracking down weird crashes later.

1 Like

Book has been bought. Thank you both!

1 Like