I was looking for a way to turn a string value into a Material Instance. I had hoped that Unreal Engine would have a node for that, but alas it does not. Nevertheless, I needed something that like a key/value lookup and came across Map Variables. Those work exactly like a mini-database right inside the node graph, and they were exactly what I needed for my project. I’ve called mine Material Map, here’s what it looks like:
Notice the two coloured icon in front of it. This means that it takes two types of objects in the form of an array (the colours depend on the types of variables you use). Over in the Details Panel we can see the two types.
Further down we get to specify (or inspect) the contents of our Map. In my case, that’s a string value on the left, with a corresponding Material Instance on the right (selectable by a drop down).
With this contents setup, I can now do all kinds of things to my variable, many of which I haven’t even explored yet. All I needed is the Find Node: I supply a string value, and out comes my corresponding Material, ready for use:
Other Map nodes that may come in handy to supply/extract values programmatically include:
More info in the Further Reading section below.
Creating a Map Variable
To create a Map Variable, start with the first type you need for keys (integer, string, etc). Note that some types don’t work, like booleans, but Unreal Engine will tell you this in an error message should you attempt it anyway. Once created, head over to the Details Panel and click on the little icon next to the variable name, the pick the bottom one that looks like a list icon (hover to see the descriptions).
This will give you a second drop down to select your value type. I find it incredibly difficult to find the correct type in that huge list, even when searching. I find it helpful to briefly promote the type of value I need from another node to a variable, then inspect that variable for a type. For example, my relatively straightforward Material Instance was actually a variable type of Material Interface. That made it much easier to pick it from the list than examining error messages later.
Once defined, you can click the little plus icon in the bottom section to define entries in your Map, or you can do so programmatically.