One script attached to different nodes, erroneously sharing variables?

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

I have a script that is attached to an Area that when hovered over the selected nodepaths (meshes) are toggled visible. This script is inherited in a couple other scripts for more complex interaction. Now, this script is attached through its inherited scripts to two different objects, each object has a number of meshes its suppose to toggle. Object A toggles two meshes and Object B toggles 1 mesh. But when I print out the list of meshes, both objects show they have all three meshes. They should be separate, if I hover over Object A, Object B’s meshes should not be toggled.


extends Area

export(Array, NodePath) var highlight_meshes_paths: Array;

var highlight_meshes: Array;
var moused_over: bool = false;

func _ready() -> void:
    if self.highlight_meshes_paths.size() > 0:
        for path in self.highlight_meshes_paths:

    self.connect("mouse_entered", self, "_on_mouse_enter");
    self.connect("mouse_exited", self, "_on_mouse_leave");

func _process(delta: float) -> void:
    if Input.is_mouse_button_pressed(BUTTON_LEFT):

    if Input.is_mouse_button_pressed(BUTTON_RIGHT):

func _on_mouse_enter() -> void:
    self.moused_over = true;
    for mesh in self.highlight_meshes:
        mesh.visible = true;

func _on_mouse_leave() -> void:
    self.moused_over = false;
    for mesh in self.highlight_meshes:
        mesh.visible = false;

func left_click() -> void:

func right_click() -> void:

extends "res://"

func _input(event: InputEvent) -> void:
    if self.moused_over && event is InputEventMouseButton:
        if event.pressed && event.button_index == BUTTON_LEFT:
            print("pick up")


+--- CollisionShape
+--- MeshInstance
+--- MeshInstance2 // hidden, toggled via mouse_over
+--- Area // script attached
     +--- CollisionShape

extends "res://"

export var animation_player_path: NodePath;

var animation_player: AnimationPlayer;
var played_animation: bool = false;
var container: Panel;

var container_placed: bool = false;

func _ready() -> void:
    assert(self.animation_player_path != null);
    self.animation_player = self.get_node(self.animation_player_path);
    self.container = preload("res://UI/container/Inventory_base.tscn").instance();
    self.container.connect("container_closed", self, "_on_container_closed");

func left_click() -> void:
    if !self.container_placed:
        self.container_placed = true;

    if !self.played_animation && self.moused_over:
        var animation: Animation = self.animation_player.get_animation("ArmatureAction");
        animation.track_set_interpolation_type(0, 0);"ArmatureAction");
        self.played_animation = true;
        self.container.visible = true;

func _on_container_closed() -> void:
    if self.played_animation:
        self.played_animation = false;


+--- Area // script
|   +--- CollisionShape
+--- Spatial // Referenced scene
    +--- MeshInstance
        +--- Armature
        |    +--- AnimationPlayer
        |    +--- MeshInstance2
        |    +--- MeshInstance3 // hidden, toggled via mouse_over
        +--- MeshInstance 4 // hidden, toggled via mouse_over

Both chest and item_3d are instanced into the same scene under a spatial, when I hover over 1 of them, both of the objects meshes are toggled. What should I be doing to ensure that the only meshes being toggled are the correct ones? In the editor the chest’s highlight_meshes_paths only shows the 2 correct meshes in the array, and item_3d only shows the one correct mesh it should use, yet all 3 are toggled when either are moused over.

:bust_in_silhouette: Reply From: DrewS

As soon as I shut my PC off to run some errands I realised my mistake.

So the problem is in the _ready() and var highlight_meshes: Array; at the beginning.

The highlight_meshes is instantiated as an empty array at the class level, as such every instance of the script is appending to the same class array. To solve this I initiate an empty array in the _ready() function.

By adding

self.highlight_meshes = Array();

into _ready() this problem is solved.