maybe
maybe

Reputation: 27

Why are signals not working for me in Godot?

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.

Upvotes: 2

Views: 478

Answers (1)

Theraot
Theraot

Reputation: 40295

  1. You don't need to connect input_event to the same object, you can instead override _input_event.

    For example:

    func _input_event(viewport, event, shape_idx):
        if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT:
            if event.pressed:
                selected = true
            else:
                selected = false
    
  2. get_global_mouse_position is a global position, set it to global_position not position.

    Like this:

    global_position = get_global_mouse_position()
    

    The local coordinates might have been aligned with the global coordinates for you, but that is not guaranteed.

  3. You could simplify the Area2D creation by making an scene and instantiating it.

    You can instantiate an scene from code like this:

    Godot 4

    var instance = load("res://path/to/the/scene.tscn").instantiate()
    

    Godot 3

    var instance = load("res://path/to/the/scene.tscn").instance()
    

    Your scene can have the script set, the collider, the texture, etc.

  4. Did you add the Area2D to the scene? The code you showed only creates them.

    Presumably you would use add_child:

    add_child(instance)
    

Upvotes: 1

Related Questions