0 votes

I have a script Mouse_Over.gd 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.

Mouse_Over:

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.highlight_meshes.append(self.get_node(path));

    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):
        self.left_click();

    if Input.is_mouse_button_pressed(BUTTON_RIGHT):
        self.right_click();

func _on_mouse_enter() -> void:
    self.moused_over = true;
    print(self.highlight_meshes);
    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:
    pass

func right_click() -> void:
    pass

Item_3d.gd:

extends "res://Mouse_Over.gd"

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

Item_3d.tscn:

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

Chest.gd:

extends "res://Mouse_Over.gd"

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:
        Globals.UI_layer.add_child(self.container);
        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);
        self.animation_player.play("ArmatureAction");
        self.played_animation = true;
        self.container.visible = true;

func _on_container_closed() -> void:
    if self.played_animation:
        self.animation_player.play_backwards("ArmatureAction");
        self.played_animation = false;

Chest.tscn:

Spatial
+--- 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.

in Engine by (94 points)

1 Answer

0 votes
Best answer

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.

by (94 points)
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.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.

Categories