How to make a canvaslayer shader invisible when a key is pressed?

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

Hello,
I’ve been trying to figure out how to make the crt shader I have in my 3D game invisible when a key is pressed for accessibility reasons. I tried to code what I thought would work and the code is simply doing nothing not even creating errors. If someone could help me out I would really appreciate it.
the code:

onready var crtshader = $CanvasLayer/ColorRect
var crtshadervisible = true

func turnshaderoff():
	if Input.is_action_just_pressed("ShaderOffOn"):
		if crtshadervisible == true:
			crtshader.hide()
			crtshadervisible = false
			
		elif crtshadervisible == false:
			crtshader.show()
			crtshadervisible = true

The example code posted looks fine; are you sure the turnshaderoff function is called?

spaceyjase | 2023-03-20 12:11

I’m fairly certain. I’ve put C in the input map as the “ShaderOffOn” action and I was pressing C but it did nothing. I feel like I’ve forgotten a crucial step perhaps?

YuleCrow | 2023-03-20 12:15

How are you calling the turnshaderoff function, from process?

spaceyjase | 2023-03-20 12:18

I’m sorry I don’t follow. What do you mean?

YuleCrow | 2023-03-20 12:20

turnshaderoff is a user-defined function, it should be called from somewhere in your code such as _process. Here’s an example, does this work?

onready var crtshader = $CanvasLayer/ColorRect
var crtshadervisible = true

func turnshaderoff():
    if Input.is_action_just_pressed("ShaderOffOn"):
        if crtshadervisible:
            crtshader.hide()
        else:
            crtshader.show()
        crtshadervisible = !crtshadervisible

func _process(delta):
    turnshaderoff()

(I trimmed the code slightly, hope it still makes sense)

spaceyjase | 2023-03-20 12:35

Ah I didn’t do that at all. Thank you so much.

YuleCrow | 2023-03-20 12:38