Godot Version: 4.3
Continuing my first foray into Godot. So far, I’ve learned to create my own custom nodes so I can apply repeatable scripts to similar 2d sprites. However, when doing so, so odd behavior occurs.
My question is this: why do two instantiated 2d character bodies which share custom nodes move together when I’m trying to click and drag just one?
My code creates two child scenes instantiated from scenes that share the same mouse click control node and area node. Both are custom with their own scripts. When I click and drag the first sprite “cross”, it moves as it should. When I click and drag the second “plus”, same deal. But when I go to click and drag the first again, both move!
I know this must be because they are sharing the same node, but I haven’t been able to figure out why from the documentation or videos. From everything I’ve seen, creating custom nodes is a basic feature of Godot, so I must be missing something.
Below is the tree structure of Inv_Tile_Cross (one of the 2d character bodies I instantiate). I’ve connected my custom node for detecting mouse clicks/movement, and to that node I’ve connected a custom node for area 2d and a unique collision shape.
main.gd:
extends Node2D
var scene_inv_grid = preload("res://scenes/inv_grid.tscn")
var scene_inv_tile_cross = preload("res://scenes/inv_tile_cross.tscn")
var scene_inv_tile_plus = preload("res://scenes/inv_tile_plus.tscn")
# Called when the node enters the scene tree for the first time.
func _ready() -> void:
instance_inventory_grid(8,8)
instance_inventory_tile("cross",300,50)
instance_inventory_tile("plus",300,150)
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
pass
#Creates clickable inventory tiles
func instance_inventory_tile(tile_name,posx,posy):
match tile_name:
"cross":
var new_node = scene_inv_tile_cross.instantiate()
add_child(new_node)
var new_vector = Vector2(posx,posy)
new_node.set_global_position(new_vector)
"plus":
var new_node = scene_inv_tile_plus.instantiate()
add_child(new_node)
var new_vector = Vector2(posx,posy)
new_node.set_global_position(new_vector)
_:
pass
#Creates the inventory from individual grid "nodes"
func instance_inventory_grid(countx,county):
for y in county:
for x in countx:
var new_node = scene_inv_grid.instantiate()
add_child(new_node)
var new_vector = Vector2(32 + (16*x),32+ (16*y))
new_node.set_global_position(new_vector)
inv_tile_control:
extends Node2D
class_name Inventory_Tile_Control
var mouseover = false
var selected = false
var mousetiledifference : Vector2
# Called when the node enters the scene tree for the first time.
func _ready():
pass
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float):
if Input.is_action_just_pressed("MouseClickLeft"):
if mouseover:
selected = true
var mouseclick : Vector2 = get_global_mouse_position()
var tileclick : Vector2 = get_global_position()
mousetiledifference = mouseclick - tileclick
if Input.is_action_just_released("MouseClickLeft"):
selected = false
if selected:
get_parent().global_position = get_global_mouse_position() - mousetiledifference
mouse_detector:
extends Area2D
class_name Mouse_Detector
func _on_mouse_entered():
get_parent().mouseover = true
func _on_mouse_exited() -> void:
get_parent().mouseover = false