I'm trying to create a chess board with pieces. Obviously the pieces will need to move and that's where signals come in. I have a drag_piece script that looks like this.
extends Area2D
var selected = false
func _ready():
input_event.connect(when_held)
func _process(delta):
if selected:
followMouse()
func followMouse():
position = get_global_mouse_position()
func when_held(viewport, event, shape_idx):
if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT:
if event.pressed:
selected = true
else:
selected = false
The script works fine by itself but the problem comes when I need to create area2D nodes through script like this:
var piece_area2D = Area2D.new();
piece_area2D.name = piece_name + str(rank) + str(file)
var drag_script = load("res://scripts/drag_piece.gd")
piece_area2D.set_script(drag_script)
var piece_sprite = Sprite2D.new()
var piece_collision = CollisionShape2D.new()
var collision_shape = RectangleShape2D.new()
collision_shape.size = Vector2(64, 64)
piece_collision.set_shape(collision_shape)
var spritelocation = "res://images/pieces/" + piece_color + piece_name + ".png"
piece_sprite.texture = load(spritelocation)
piece_area2D.position = Vector2((rank * 64) - (64 / 2),(file * 64) - (64 / 2))
piece_sprite.scale = Vector2(1, 1)
piece_sprite.z_index = 1
This is not the entire script. Everything works fine besides the pieces dragging when i hold down left click.
I tried adding a print statement in the ready function that prints "works" so I know that the script is working fine, and it did print it so I really don't know what the problem is. I read the Godot documentation on signals and searched google for a good hour and something and haven't found anything.