How do I load instantiated resources(shaders and textures) to nodes using the .set() method?

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

I’ve been trying to load in a new ShaderMaterial thru script but I think I’m doing it wrong :frowning:

here’s the actual code:

extends MeshInstance

onready var dummy_cam = $DummyCam

onready var mirror_cam = $View/Camera

onready var SM = ShaderMaterial.new()

onready var W =ViewportTexture.new()

onready var mat = self.get(“material/0”)

func _ready():

add_to_group("mirrors")

self.set("material/0",SM)

mat.set("shader",load("res://Mshader.shader"))

W.set_viewport_path_in_scene("Mi2/View")

mat.set("shader_param/refl_tx",W)

View.size = Vector2(2000,1000)

func update_cam(main_cam_transform):

scale.y *= -1

dummy_cam.global_transform = main_cam_transform

scale.y *= -1

mirror_cam.global_transform = dummy_cam.global_transform

mirror_cam.global_transform.basis.x *= -1

on top of that I still have yet to find a way to “.set()” this:


so It could change paths when I try making more of this in a future scene

:bust_in_silhouette: Reply From: Zylann

Godot is telling you mat is null. null means the variable contains nothing.

onready var mat = self.get("material/0")

onready code will run just before your _ready function. You did not assign material 0 at this time, so mat will contain null.

self.set("material/0",SM)

Later you are assigning material 0, but the mat variable will still be null. You have to write mat = self.get("material/0") AFTER you set the material slot, or better, just write mat = SM.

Also, you don’t have to use set or get. Properties are described in the doc to do this. Just write mat.shader = load("res://Mshader.shader"), or set_surface_material(0, SM) (no need for self either).

so I got rid of “mat” all together:

I still couldn’t instantiate a new ShaderMaterial to “material/0”

19PHOBOSS98 | 2020-01-15 08:26

oh wait my bad I’m suppose to use set_surface_material(0, SM) brb

19PHOBOSS98 | 2020-01-15 12:21


Still didn’t work :frowning: what am I doing wrong :((

19PHOBOSS98 | 2020-01-15 12:26

In your last screenshot, you are showing the Local scene tree. This is the scene as it is before it runs. If you want to see properties of your node while it is running, select Remote at the top of the scene tree (and wait for a bit, it can take some time to update).

Also, you are getting an error later in your script. You are calling load with a ViewportTexture, but load expects a path to a resource instead.

Zylann | 2020-01-15 18:43