using variables from gdscript in shader

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By websterek
:warning: Old Version Published before Godot 3 was released.

How can i access to a variable from my gdscript inside of shading language?

I have found some information in docs about “uniforms” but i don’t known how to use it.

For example in my gdscript “” i have variable “length = 10”. How to use it inside od shader script?

:bust_in_silhouette: Reply From: ericdl

You can pass variables from gdscript to shader script using the CanvasItemMaterial’s set_shader_param method.

In your shader:

uniform float my_value;

In your gdscript:

var value = 2.5
var Mat

func _ready():
	Mat = self.get_material()
:bust_in_silhouette: Reply From: jarlowrey

In 3.0


uniform float my_value;

GD Script

self.material.set_shader_param("my_value",Color(0.26, 1, .9, 1))

If you set a parent material, make sure the child has updated with the new material/shader. Otherwise will encounter null errors

Could you please elaborate? I am trying to send my Player Position to my shader, and I am getting null errors.

My Particles2D shader is a child of my Player Kinematics2D.

self.material.set_shader_param(“player_position”, position)

I am getting error
"Attempt to call function ‘set_shader_param’ in base ‘null instance’ on a null instance

AybeeCruz | 2021-05-18 03:15

Also in version 4.0.2 (stable)

self.get_material().set_shader_parameter("my_value",Color(0.26, 1, .9, 1));

RufusWein | 2023-04-19 18:37