Reputation: 11
I have multiple "interactable" objects in my game all extend the InteractableBody class that sets some stuff up so the player can interact with the object. I am trying to add an outline shader to the sprite so I can outline objects that are able to be interacted with. This is my code right now that does this. However, it currently just applies the outline to the first object with the shader, no matter what object. I had a previous version working where all objects had the outline and were all being changed when only one object was being updated.
This is my current code for applying the shader:
class_name InteractableBody
extends CharacterBody2D
@export var string_name : String
@export var icon : Texture2D
@export var interact_range : float
@export var interactable_sprite : Sprite2D
@export var outline_shader : Shader
var player_node : Player = null
func _ready():
add_to_group("interactable")
var material_instance = ShaderMaterial.new()
var new_shader = outline_shader
material_instance.shader = new_shader
interactable_sprite.set_material(material_instance)
hide_outline()
func setup_player(_player_node):
if _player_node:
player_node = _player_node
func _process(delta):
if player_node:
if player_node.global_position.distance_to(global_position) < interact_range:
if player_node.interactable_objects.has(self) == false:
show_outline()
player_node.add_interactable(self)
else:
if player_node.interactable_objects.has(self) == true:
hide_outline()
player_node.remove_interactable(self)
func show_outline():
var shader_mat : ShaderMaterial = interactable_sprite.material
shader_mat.set_shader_parameter("color", Color(1.0, 1.0, 1.0, 1.0))
func hide_outline():
var shader_mat : ShaderMaterial = interactable_sprite.material
shader_mat.set_shader_parameter("color", Color(1.0, 1.0, 1.0, 0.0))
I tried to apply an outline shader to each object in a group and then be able to turn the shader on/off on each node one at a time. What actually is happening is only the first node with the shader has an outline and that outline is being updated no matter what node I am interacting with.
Upvotes: 1
Views: 383
Reputation: 77
Here are some solutions, which may work depending on your specific setup:
Upvotes: 0