Invalid set index 'dragging' (on base: 'Area2D') with value of type 'bool'. How can I fix?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Berflatta

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!

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?

jgodfrey | 2022-07-15 15:40

:bust_in_silhouette: Reply From: zenbobilly

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.