[EDIT]: ANSWER

The shader was applied to all instances. So i had to make it unique by checking the 'Local to scene' option in the Material panel:

resource_local_to_scene
If true, the resource will be made unique in each instance of its local scene. It can thus be modified in a scene instance without impacting other instances of that same scene.



Context

I'm trying to highlight (with a shader) a NPC on mouse_entered

Objective

I want to automatically connect 'mouse_entered/exited' from parent class to each child.

Problem

Here i want only 1 'Ruth' child to be highlighted at the time, on 'mouse_entered'

Tree

My obviously not working code

# Parent class

class_name NPC extends StaticBody2D

func _ready():
	var _highlight_entered = connect("mouse_entered", self, "npc_mouse_entered")
	pass

func npc_mouse_entered():
	$AnimatedSprite.material.set_shader_param("outline_width",0.3)


func npc_mouse_exited():
	$AnimatedSprite.material.set_shader_param("outline_width",0.0)
# Child

extends NPC

How can i achieve this ?

  • ThinKing_2005 replied to this.
  • Lbh That means the problem was with the shader material being shared, so you had to check the 'local to scene' option to stop it from being shared across instances of the same scene. Cool!

    On the contrary, actually, your code seems to be working, just that the signal emitted from a single 'Ruth' is connected to both 'Ruths'. I'm not sure what's the structure of the nodes you're using, but here's how it's usually done:

    1- Make a single 'Ruth' its own scene, separated from everything else.

    2- Add the same script logic to this individual scene.

    3- Make another scene (the level).

    4- The 'Ruth' scene is loaded into a variable (eg. var Ruth = preload("*pathway to your Ruth scene") or using load function). This is done in the script of the parent(/level) node (or an Autoload if you wish).

    5- For adding a single Ruth, instance the 'Ruth variable' (basically a PackedScene of Ruth scene) then add it as a child of level node. code: add_child(Ruth.instance()

    I'm assuming that method of adding the Ruth scene as a child of the level is slightly different. If so the above steps should solve your problem. Also, you could drag-and-drop the scene file into the 'level scene tree' to instance Ruth more easily, if not added during runtime.

    • Lbh replied to this.

      Megalomaniak
      Sorry, i couldn't answer sooner! I totally forgot to add my scene tree, sorry for that
      You can see it in my edited post

      Lbh Oh, I think I know the answer. Maybe the shader material is shared between both instances, so that when you change the shader settings, every shader is changed. You should make each shader unique.
      Edit: this could be caused if you DUPLICATE the ALREADY INSTANCED Ruth scene. So I want to ask, how did you make Ruth2 instance, by duplicating Ruth, or by instancing it separately?

      • Lbh replied to this.

        ThinKing_2005
        Thank you for the infos!
        I edited my post, so you can see the main scene tree.
        As you can see i already created a 'Ruth' scene, and i instantiated 2 copies of it with a simple drag'n'drop

          ThinKing_2005
          Oh that is clever, never thought about that!
          Well i think i duplicated the Ruth2 with 'control+D', just now i removed it, then drag'n'dropped it from 'res' folder, like the first instance.
          Sadly it didn't change anything, both are still highlighted at the same time
          I don't understand shader code well, would you like to see it ? Maybe there is something in its code that somehow create this overall effect?

            Lbh why are you giving a variable the value if connect, shouldn't it be self.connect?
            Edit: also, where is the code for connecting the mouse_exited signal?

            • Lbh replied to this.

              ThinKing_2005
              Yes i know! But Godot sends me an annoying warning in console if i don't put 'connect' in a variable:

              The function 'connect()' returns a value, but this value is never used.

              By the way i just solved the issue! @Megalomaniak and you were in the right path!
              I will edit my post to include the answer (spoiler: i had to check the 'local to scene' option)

              Thank you both @ThinKing_2005 and @Megalomaniak for helping a Godot newby!

                Lbh Something is strange about how the SIGNAL is connected. If shader material is not shared, then it must be how the signal is connected.
                Since the signal is emitted from Ruth, AND received by the same Ruth, the script of connecting it should be attached to the original Ruth scene. Like I stated above, the code you're using for connecting signals is incomplete... did you use the editor to connect the mouse_exited signal, and code for mouse_enterd?

                Lbh That means the problem was with the shader material being shared, so you had to check the 'local to scene' option to stop it from being shared across instances of the same scene. Cool!

                Lbh As for the warning, it doesn't really affect anything, so you could just ignore it.