Does anybody know how the get_overlapping_bodies() function determines the order of the list (of bodies) it generates?

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

Hello! I am currently trying to create a system in which the player can choose between multiple bodies within an area 2D. I am currently using the get_overlapping_bodies() function to do this, and was wondering how it determines the order of the list of bodies.

For insight, this is the code I am using right now -I am currently assigning an ID to each of the bodies, so I can select based on the value of a counter which changes with keyboard input-

func assignIDs():
        var ID = 0
		for bodies in Area.get_overlapping_bodies():
			bodies.ID = ID
			ID += 1
		maxID = ID

func select():
	owner.movement_velocity = Vector2.ZERO
	canHit = whipRange.get_overlapping_bodies()
	if Input.is_action_just_pressed("Right"):
		targetBody += 1
	elif Input.is_action_just_pressed("Left"):
		targetBody -= 1
	targetBody = clamp(targetBody, 0 , maxID)
	for bodies in canHit:
		if targetBody == bodies.ID:
			bodies.modulate = Color(1,0,0,1) #Visual aid to show which body is selected
			bodies.modulate = Color(1,1,1,1)

I am currently using these two snippets of code (first is in a separate function, unimportant right now) to select a body out of the list, but would like to know how the function determines the order so I can have it be more polished in the final product :slight_smile:


:bust_in_silhouette: Reply From: Inces

I believe the first body to get into area gets listed first in overlapping_bodies, so it all depends on a time of collision detection.