Copying a Material instead of replacing it?

So I’m drawing a sprite, and everything works well, but then upon attempting to use a different one that isn’t set up the same way, it doesn’t draw properly. (Not translucent, can’t be colored, etc)

What I’m trying to do is simply copy the material that renders the way I want it to, then edit the $basetexture to simply draw a different sprite instead, in the same fashion.



local copymat = Material( "sprites/light_glow02_add" )
copymat:SetTexture( "$basetexture", "particle/particle_ring_wave_7" )

(I’m not planning on using local for this once it works so don’t worry about optimization.)

This appears to work flawlessly at first… But my problem is that after doing this, ALL other instances of “sprites/light_glow02_add” are also replaced by the “particle/particle_ring_wave_7” sprite.

Essentially, I need a way to copy the material rather than replace or override it. There doesn’t seem to be a IMaterial:Copy or anything of the sort, so I’m not sure what to do.

Maybe use

CreateMaterial for each texture variation you want, and use the original material’s key-values except for $basetexture.
There might be a better alternative though.

I had the same idea, so I tried this:



local copymat = Material( "sprites/light_glow02_add" )
local pastemat = CreateMaterial( "testmat1", copymat:GetShader(), copymat:GetKeyValues())
pastemat:SetTexture( "$basetexture", "particle/particle_ring_wave_7" )


But this just does the same as simply drawing Material(“particle/particle_ring_wave_7”), which means it’s not translucent and can’t be colored.