Cannot separate Race Cars since Shader code addition

Godot Version

4.2

Question

Hi All, doing the ‘learning of Godot’ by remaking Super Cars for fun.

Having trouble understanding why @export var My_Modulate is being shared over all race cars?

  • Old Situation: Before Shader addition. Cars were all different colours via Self_Modulate. All good.

  • New Situation: Shader added to handle a simple sunshine effect to make more 3d looking. Self_Modulate no longer works, as the shader takes control of the image. To fix, Add in colour paramater to visual shader, add in @export var My_Modulate to submit any picked colour to shader (via set_shader_parameter call). All good. Car is a different colour when game is run.

  • Now. New, extra cars added, made Local, but the last car’s My_Modulate colour takes over All the cars.

So, I cannot fathom what is taking control here, or how to remedy it.
All cars have a script, which pulls the colour from My_Modulate, which is set by a colour picker, per ‘each’ car.

Does anyone know what is happening here? Or better yet, how to remedy? I’m lost, as I thought making each car local was enough.

Basic Code in the gdscript node of the car.

@export var My_Modulate: Color = Color(1.0,1.0,1.0,1.0) # Set the car's colour
func _ready():
	#-----------------------------------------------------------
	# Set Car Colour
	#-----------------------------------------------------------
	Car_Image.material.set_shader_parameter("Car_Color",My_Modulate)

Materials are Resources and Resources are shared by default. You’ll need to make them unique doing any of the following:

  • Enable Local to Scene in the Resource. This will make unique the resource when instantiating the scene (if the resource is shared between nodes in the same scene the copies won’t be made unique)
  • Calling Resource.duplicate() in code. For example in your _ready() function.

Yay Working
That worked after deleting the current cars and re-adding them in with that switch. Thank you so much Mrcdk! :smile:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.