+1 vote

I want to get a list of Area2Ds overlapping a certain area/shape immediately without waiting for a physics update or the next frame. I want to do this purely in GDScript.

What I tried is instancing an Area2D scene which then triggers signals. It is then removed in the next frame. The problem is that I'm not quite sure when these triggers will fire. I want to know exactly when these triggers happen since they modify the game state. I don't want my aoe damage to inflict damage the next frame instead of immediately.

in Engine by (23 points)

1 Answer

0 votes

I settled on using Physics2DDirectSpaceState.intersect_shape:

# initialize parameters for query
var params = Physics2DShapeQueryParameters.new()
params.set_shape(shape) 
params.transform = # set position/rotation/scale of shape
params.collision_layer = # collides with objects in same layer
params.collide_with_bodies = false
params.collide_with_areas = true

# do query
var space_state = get_world_2d().direct_space_state
var query_results = space_state.intersect_shape(params)
for result in query_results:
    var actual_collider_object = result.collider
    # do stuff

I only tested this inside a _physics_process context. The collision_layer parameter seems to also act as a mask. So you can add different layers together to allow checking in multiple layers. I'm not sure though.

by (23 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.