Detect individual collision objects

Godot Version

4.2

Question

Hi, I need a way to detect which are the two objects that are colliding.
The games goes something like this, I need to know the colors of balls that are touching:

balls

# ball.gd
extends Node2D

@export var color = "red"
var blue = preload("res://assets/ball_blue.png")
var red = preload("res://assets/ball_red.png")
var yellow = preload("res://assets/ball_yellow.png")

func _process(delta):
	match color:
		"yellow":
			$RigidBody2D/Sprite2D.texture = yellow
		"red":
			$RigidBody2D/Sprite2D.texture = red
		"blue":
			$RigidBody2D/Sprite2D.texture = blue

Each ball is a RigidBody2d. I activated Contact Montitor and increased Max contacts reported. Now, every time there is a collision the body_shape_entered signal is fired.

func _on_rigid_body_2d_body_shape_entered(body_rid, body, body_shape_index, local_shape_index):
	print("body_rid: ", body_rid, "\nbody: ",body, "\nbody_shape_index: ", body_shape_index, "\nlocal_shape_index: ", local_shape_index)

This gives me this output when a ball hits another ball:

body_rid: RID(665719930882)
body: RigidBody2D:<RigidBody2D#29410460898>
body_shape_index: 0
local_shape_index: 0

It’s a RigidBody2D not a Ball class. How can I access the variable color?

The code you post is ball.gd but instead of extending RigidBody2d it extends Node2D

Is ball.gd not the code for the Ball class?
It looks rather that Ball is a Node2d and contains a RigidBody2d.

So it is a little confusing (to me anyway).
May we have a look at the Ball scene tree?

This is my scene:

scene

That’s something I don’t fully understand. I can only have one script for scene, and in this case for example I need to choose which to extend (Node2D, RigidBody2D, CollisionShape2D or Sprite2D).