+1 vote

I've been trying to load in a new ShaderMaterial thru script but I think I'm doing it wrong :(
enter image description here

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 updatecam(maincam_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:
enter image description here
so It could change paths when I try making more of this in a future scene

asked Jan 14 in Projects by 19PHOBOSS98 (33 points)

1 Answer

+1 vote

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).

answered Jan 14 by Zylann (26,033 points)

so I got rid of "mat" all together:
enter image description here

I still couldn't instantiate a new ShaderMaterial to "material/0"

oh wait my bad I'm suppose to use setsurfacematerial(0, SM) brb

enter image description here
Still didn't work :( what am I doing wrong :((

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.