How to use the ‘Switch’ node in UE5?

What does it do?: The Switch node allows us to dynamically swap textures at runtime. In my case, I want to swap my emissive texture (currently only used by the accessories inside the car) with another one (that also includes the headlights, etc.) when the player presses the ‘T’ key, so the headlights appear brighter.”

(Optional Read:) Just in case you didn’t know, emissive textures mask the black parts (meaning no emission; the master shader will use the albedo channel in that case). However, if there is any color information in the other parts, those areas will be used as the emissive color. I am using an atlas texture for this material, and I painted the metal and other non-emissive areas black, so they don’t receive any emissive color. The screens, headlights, etc., should receive some emissive color, so I left their textures as they are in the emissive texture.


The Switch Node

Lazy Loaded Image

We can create multiple inputs in the Switch node, but in my case, I want to swap one texture for another. The first and default texture is ‘Headlights Off’, and the second one is ‘Headlights On’. The default value is set to 0, so the headlights will be off when the level loads. I created a scalar parameter and assigned it a value of 0. I will access this scalar parameter (which I named ‘SwitchValue’) and change it to 1 when the player presses the ‘T’ key, and vice versa.


Accessing the Scalar Parameter from the Vehicle Pawn

Lazy Loaded Image

What happens here?

When the player presses the ‘T’ key, it will flip the values between 1 and 0. Each time the key is pressed, the value will alternate between 1 and 0.

My target is the ‘Mesh’ in my vehicle pawn Blueprint. We then create a dynamic material instance to access the scalar parameter value. In the ‘Set Scalar Parameter Value’ node, the ‘Parameter Name’ should match the name of your scalar parameter. As shown in the screenshot, I am setting the value to 1, then to 0, and so on.

One important thing to note is that this code is not optimized. You should not create a new material instance every time the player presses the ‘T’ key. Instead, create the material instance in the ‘Event Begin Play’ and store a reference of it in your Blueprint. This way, you can access it whenever needed without creating it repeatedly.

If you encounter any issues implementing this, please let me know. Additionally, consider supporting me on Patreon so I can dedicate more time to creating these tutorials.