#get_overlapping_bodies returning empty array in _ready
1 messages · Page 1 of 1 (latest)
This isn't the best solution, but a down & dirty fix to get it running would be to have damage_enemies return a boolean and then place it in your _physics_process, setting it to some variable like initial_damage and checking if initial_damage is true before executing
Can provide pseudocode if that didn't make sense 😛
You'll want to make the return value something like: return not enemies_in_hitbox_area.is_empty()
so it only returns true when you succesfully have grabbed the overlapping nodes
doing that will essentially turn it into the one-time thing you're looking for
or I guess I don't know the full context of what you need it to do
if you call get_overlapping_bodies() in _ready there might not be any other bodies yet is where the issue lies
I believe physics_process() runs before the physics engine so it can't detect bodies either on its first call
also call_defered() just waits for the end of the curent frame
it needs the physics engine to run once
you could use the body_entered signal
There is await but I don't really use it so can't show you how to best use it
I can offer you this workarround but i think it's kinda stupid
func _ready() -> void:
await get_tree().physics_frame
await get_tree().physics_frame
damage_enemies()
yeah, that's my problem with it
probably. would need more context
like why is this thing that is apparently just spawning also checking for collisions already with other things that aren't _ready yet?
are the other things not ready?
the other things are enemies that were already added to the tree so they're ready
the area2d needs to go through a call of the physics engine to detect things
hmm. what is the reasoning behind it only colliding on the first frame and never afterward?
are you talking about the signals it emits?
its like an aoe attack
its signals only fire when it starts or stops colliding, but get_overlapping_bodies() and the other methods continue to detect stuff
oh sorry I was just asking his thought process behind why he wants that behavior 😛
I have an aoe attack that spawns and hits enemies in a game, will check the code out
if it's an aoe attack it would probably feel better if it existed for a little longer than just 1 frame
yeah but in the case that i only wanted it to exist for 1 frame
i just want the damage hitbox to be out for 1 frame, but the sprite animation is a second or so
isn't this mismatch a problem?
if you want to avoid await (which I agree with avoiding), you could just use that timer to disable the collision shape. and keep the collision in _physics_process
i don't agree with avoiding await
agree to disagree 😛
but i still like that idea more
'-'
what does that idea look like
add a timer to your aoe attack scene that disables the Area2D on timeout
have you used signals before?
this idea wouldn't be only first frame, it would be for X seconds. like 0.1 seconds in your earlier example
which might as well just be 1 frame
why is that better?
to avoid await, which basically pauses the active thread
feel free to just use await tho haha
i thought it only blocks if it's in _process or _physics_process, not _ready
i thought too
if that was the case it wouldn't work in _ready
anyway i don't mind await but idk, the vibes are off on the other solution