How To Use Material Instances In Data Tables?

Hey I’ve created a data table where I wish to have several rows each with a unique material in each.

My issue is that I have both materials and material instances that I wish to store inside of that material slot, but I cannot find a variable which allows me to do so.

If I use the Material variable type in my struct, it disables me from entering material instances. Vice versa if I use Material Instance type in the struct.

Is there a variable which will allow me to enter both? I have many entries and do not wish to manually create a material instance for each if I can avoid it.

Thanks.

Jackson.

Hey there @jacksonnexhip! I had to sit down to see if I could hash this out, but as you say you have to choose one or the other. I’d hate to say it but my best way at working it out was to add just a little bit of bloat to the struct and have an entry for material and material instance. You can make a little macro that preprocesses the entry to verify which one it has at the time with just a bit more overhead. Apologies if that’s infeasible!

1 Like

Actually, here’s something that looks like it might work – both UMaterial and UMaterialInstance are UObjects, and they both implement UMaterialInterface.

SO, I’m not entirely 100% positive this will work, as in, i haven’t tried it, but doing some additional googling around, I find this possible answer

It looks like if you give it a TSubclassOf with a meta=(MustImplement=(“MaterialInterface”)) that it would allow you to specify Materials, MaterialInstances, or anything else that implements MaterialInterface . . .

Ohhhh that’s an excellent idea, that would eliminate the table bloat with minimal work after the fact to identify which one. I hadn’t even thought about it that way. I wonder how the ref would be though. I’m excited to see how it’d work out!

I haven’t done much work with materials/materialinstances in the time that i’ve been back in Unreal, have mostly been doing ai stuff, but I would think you could just Cast<UMaterialInterface> on it, and go. However, there could be reasons to cast either way to check if you need to do something differently

1 Like

I don’t have the knowledge of C++ to try this unfortunately, but thanks anyway. I figured out a workaround similar to the marked solution.

Thanks!

1 Like