0 votes

I've been trying to implement RigidBody2D drag and drop features, but keep getting the error message "Invalid set index 'dragging' (on base: 'Area2D') with value of type 'bool'." How can I fix this?

I have a control node, a node for the shapes group, a rigid body with collision, and an area2d with collision.

The code for the control node goes as follows:

extends Control

var shape = null

func _input(event):
    if event is InputEventMouseButton:
        if event.is_pressed():
            shape = find_colliding_shape(event.position)
            if shape == null:
            shape.dragging = true
        elif shape != null:
            shape.dragging = false
            shape.apply_impulse(Vector2(0,0), Vector2(0,1))
            shape = null
    elif event is InputEventMouseMotion and shape != null:
        if shape.translate_by == null:
            shape.translate_by = Vector2(0,0)
        shape.translate_by += event.get_relative()

func find_colliding_shape(pos):
    var pointer = get_node("Shapes/Pointer")
    var pointer_shape = pointer.shape_owner_get_shape(0,0)
    var pointer_transform = pointer.get_transform()
    for node in get_node("Shapes").get_children():
        var shape = node.shape_owner_get_shape(0,0)
        var res = shape.collide(node.get_transform(), pointer_shape, pointer_transform)
        if res:
            return node
    return null

The code for the shape itself, or in this case, the group of shapes, goes as follows:

extends RigidBody2D

var dragging = false
var translate_by = null

func _integrate_forces(state):
    if dragging:
        if translate_by:
            var t = state.get_transform()
            state.set_transform(Transform2D(t.get_rotation(), t.get_origin() + translate_by))
            translate_by = null

Any help is appreciated, thanks!

Godot version 3.4.4
in Engine by (12 points)

Since the error indicates an Area2D, but your code shows that your shape is a RigidBody2D, I'd guess your find_colliding_shapes() method is returning the wrong object?

1 Answer

0 votes

The Area2D object must have a script attached that extends Area2D but must also contain the "dragging" and "translate_by" variables. Since these variables are common to both "shapes", you could put them in a common class and get the "shapes" to derive from it.

by (141 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 Frequently asked questions and 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 [email protected] with your username.