How to catch an array of objects

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

Hi, guys! First of all, I’m not really sure if what I’m doing is the best way of approach a problem like this. I’m a game designer, not really a programer.

I’m trying to make a “Catch words” game. This is my layout: https://i.imgur.com/30ChRWZ.png

Each Label has a script that sorts a random letter if I don’t define one in the editor. I want the player to click on the first letter, and release at the last one.
I draw a line following the mouse cursor while the button is pressed. Once player release, it snaps to the center of the nearest Label.

My problem is:
I need to catch every letter under this line.

As Raycast just would return the first object, I added a Area2D and area_entered signals to each Label and to the line Area2D to check the overlapped Labels and make an array, but the f*cking get_overlaping_areas function just throw an empty array on my face.

I’m searching for similar situations for the last four days, but no solution. I have used a SegmentShape2D as a shape. When I print it’s A and B points they correctly match the Labels origin, so I assume the shape change as expected, but still no result on check what it is overlaping.
But what is really killing me is that if I push the Collision shape points over the container in editor, when I run it both get_overlaping_areas return the array and the signals trigger perfectly fml!

This is a list of what I’ve tried (attempt : result):

  • get_overlaping_areas : empty array
  • Clear and create another shape owner, add shape etc : empty array
  • something with direct space and physics query : bananas

I’m new to this forum, hope I give enough infos.
Thank you, people!

:bust_in_silhouette: Reply From: Thomas Karcher

Since your solution works when the collision shape is already defined in the editor, I assume the problem could be a timing problem, meaning: The overlapping areas for the new collision shape created at runtime are not yet calculated when you try to retrieve them. If that’s the case, a deferred call [1] after the mouse release event could solve your problem:

func _input(event):
  [...]
  if event.is_action_released ("left_click"):
    # draw line, create collision shape etc.
    [...]
    call_deferred ("get_all_labels")

func get_all_labels():
  var covered_labels = mouse_trail_area2d.get_overlapping_areas()
  [...]

[1] Object — Godot Engine (3.1) documentation in English

I’m deeply sorry for the delay, Thomas. I ended working around it givin the Area2D a CollisionPolygon and translating it from start to finnish. Not a good solution but it did the work.
Unfortunately I couldn’t test your solution, so I don’t know how it would work. But if we manage to refine this code I’ll surely try it.
Thanks again!

semperfoo | 2019-05-30 20:23