edit material at runtime?

Hi all, I am trying to adjust a material (make it translucent) in-game based on a players proximity. I set up a blueprint that saved the current material to a variable, swapped it out temporarily for another, then swapped it back. It works, but crashes the game about 75% of the time.

So, is it possible to instead, just edit an actor’s material in blueprint?
I can only find ‘get material’ or ‘set material’, and they don’t allow me to change anything in the blueprint.

I need this to happen on every actor in the level, so i can’t edit the materials separately.

thanks!

Hi.
If I understood correctly, you want everything around the player to become translucent ? Like everything ? Does this need to happen only from the player’s point of view or even from other camera’s pov ? Like other players ?

Yeah editing every material that exists in the game sounds like a very risky idea, especially if that material becomes unused completely then it should get removed from the cache which is why your game would be crashing. I would say a material instance with some param’s on it would be a better bet

Hello,
To change material during runtime, you have to create a material dynamic instance (DMI) : A new, community-hosted Unreal Engine Wiki - Announcements and Releases - Unreal Engine Forums9-_Dynamic_Material_Instances,_Pt._1

Well, with dynamic instances, I guess you wouldn’t be able to make an opaque material turn transluscent. It means you would need to have your world made out of transluscent materials with an opacity equals to 1. That would probably be a perf killer amongst tons of other problems. That’s why I’m asking about the amount of item that needs to support this.

I also imagine that replacing an opaque material with a transluscent one on the fly might be the reason of the crash. Just as a test, can you test with your current script to replace the basic material with another opaque material to see if that’s really the problem ?

cool, thanks. Dynamic material instance looks to be what I need. Should I create one for each actor as needed, then delete it afterwards? How best to switch back to the original material as the player moves away?

I’m not sure you got everything here, or maybe it’s me. But in my mind :
1-You can’t make an opaque material become transluscent through Dynamic instance. These are just different families of materials, with totally different behaviour, especially when it comes to Z-sorting. On a dynamic instance, you can just play with the value of parameters, and a you can’t change an opaque material with a transluscent one through this.
2-That means you should work only with transluscent materials, and play with a parameter linked to opacity. But those materials would always be a transluscent type, eventhough they have an opacity equals to 1. And a fully transluscent world will be a GPU hell, probably. And you’ll probably see tons of sorting glitches.
3-It means you should stick to a variant of your basic idea, and switch an opaque material with a transluscent one only when needed, which brings us back to your crashes. You need to know why this happens.

Do I make any sense here or did I miss something ?

I made my opaque material translucent with parameter. The problem I had was that it lost shadows. So I duplicated the mesh and assigned a basic opaque material to it. I then set it as hidden, but casts shadows. Then the original material I can make transparent or not just by controlling the parameter and it still gets shadows.

Any progress on this? I’m trying to do something similar as the OP, but there’s no way to change an opaque material and turn it into a translucent one as far as I’m aware. Any progress on this front?

Using a translucent material with an opacity of 1 never gives good results in terms of Z-pass and shadows. Switch the materials when you need to make it translucent, it saves ressources and removes bugs. The only way to do that is with a SetMaterial. You can maybe experiment with overriding the blend mode at runtime, but I don’t think that is possible.

You cannot switch from opaque to translucent or anything, However you can use a masked material as it accepts lighting information just remember to lower the clipping point in your mask settings or you will end up with sudden clips (Use something low like 0.001 or even lower).

Why don’t you let your objects store 2 materials:
One Opaque, normal version and one translucent with the same maps and you switch between these two depending on your needs.

You could probably do this in the material entirely using the Pixel Depth (I think) node to control the alpha in a blend or lerp. As above, this would kill perf if needed on all materials but if you only needed this on a few then it could work fairly nicely.