Reusing ParticlesMaterial causes referencing issues

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By jarlowrey

I have a node with a particle2D attached. I instance this node multiple times in a scene. The P2D have a ProcessMaterial, which has a ColorRamp. When I try to change the color ramp to a different Resource, it changes all the colors on every ProcessMaterial to the same Resource.

This is similar to the CollisionShape error. In that question, the answer was to create a new CollisionShape2D each time, or to click “Make Unique”. That’s not working for me now. “Make Unique” doesn’t seem to do anything, and duplicate()ing my color or process material does not fix the issue.

Any ideas on how to fix this referencing issue?

$Area2D/Particles2D.process_material = preload("res://src/Gear/Bullets/BasicLaserParticleMaterial.tres")
$Area2D/Particles2D.process_material.color_ramp = GradientTexture.new()
$Area2D/Particles2D.process_material.color_ramp.gradient = load(str("res://src/UI/Color/",color,".tres"))

But all the particles are set to the same color when this code runs

another thread about this

jarlowrey | 2018-05-16 23:05

:bust_in_silhouette: Reply From: jarlowrey

I had to duplicate the material AND create a new gradient color ramp

$Area2D/Particles2D.process_material = preload("res://src/Gear/Bullets/BasicLaserParticleMaterial.tres").duplicate()
$Area2D/Particles2D.process_material.color_ramp = GradientTexture.new()
$Area2D/Particles2D.process_material.color_ramp.gradient = load(str("res://src/UI/Color/",color,".tres"))

See the asker’s comment on this answer. I have the same result: Colors for all particle instances will still always have the same color as the most recently modified instance.

Animignis | 2018-06-21 11:00

:bust_in_silhouette: Reply From: Animignis

This is the code that I finally was able to get working in Godot 3

var mat = process_material
process_material = mat.duplicate()
process_material.color_ramp = GradientTexture.new()
process_material.color_ramp.gradient = Gradient.new()
process_material.color_ramp = mat.color_ramp.duplicate()
process_material.color_ramp.gradient = mat.color_ramp.gradient.duplicate()
process_material.color_ramp.gradient.colors[i] = Color(1, 0, 0, 1)

I wish that there was a deep_duplicate option to duplicate sub-nodes as well. This is just ridiculous.