Imagine a few overlapping Area2D. Perhaps a deck of cards. I want to get the top card of the pile. Simple, right?
If I “click” it using intersect_point, I get all the results in a non-deterministic order. Yet the cards are drawn in SceneTree order since they are CanvasItems. From here I could…
Get the tree order of the results. But how? I cannot find such an in-built method, and walking the entire scene tree each physics frame for each result to manually calculate tree order seems like a pain.
Check Z-index. Except this requires manually assigning Z index to every item in the game and keep it correct. No thank you.
It seems like such a simple task for a game engine; “click the thing on the screen”. Surely I am just missing it in the docs or something?
Interesting idea! Index is useful for siblings, which might be good enough, even though it is not strictly tree order. Consider e.g. the following tree:
A
Aa Ab
B
Bb
The node Aa, child of A, has index 0.
The node Ab, also child of A, has index 1.
The node Bb, child of B, has index 0.
Bb has lower index than Ab, but Bb renders in front of Ab because of tree order.
I guess one could calculate all indexes going from root to bottom, and from that get tree order. Which sounds like the pain I mentioned in point 1, but perhaps it is not necessarily so bad computation wise. Using index could also work if the game is designed to exploit some special relationships (e.g. all relevant nodes are siblings).
var intersections = direct_space.intersect_point(query)
if intersections:
intersections.sort_custom(func(a, b):
var obj_a = a.collider
var obj_b = b.collider
return obj_a.is_greater_than(obj_b)
)