Topic was automatically imported from the old Question2Answer platform.
Asked By
rgrams
Old Version
Published before Godot 3 was released.
I’ve created a simple shader to make my enemy sprites flash white when they get hit, but now they all flash when any one of them is hit. How do I create and assign dynamic instances of my material or shader to each instance of the enemy scene?
First, use the UI the set up the Material exactly as you want. In my case, I created fully functional shader materials all pointing to .shader files.
Then, save the material as a .tres file:
Now, you’re ready to import programmatically from anywhere. Below I dynamically create the target node, but this is optional can be done with nodes added via GUI as well.
# Contains a ViewportCollection with a Viewport and Node2D drawing.
const MyDynamicScene = preload('res://Scenes/MyDynamicScene.tscn')
# The shader material we just saved.
const MyShaderMaterial = preload('res://Materials/MyShaderMaterial.tres')
func _draw():
# Use get_node or $NodeName if you'd rather use GUI-managed scenes.
var dyn_scene = MyDynamicScene.instance()
dyn_scene.name = 'DynamicScene'
dyn_scene.material = MyShaderMaterial
add_child(dyn_scene)