Best way to handle overlapping Area2Ds

Godot Version



Godot beginner, working with some basic Area2D logic stuff. I have multiple instances right now with the player triggering on_area_entered() calls that change scene, adjust state, etc.

Think games like Paper Mario, where the player attacking/colliding with an enemy in the overworld changes the scene to combat with that enemy. I’m managing that part decently, but colliding with 2 overworld enemies at once makes it falls apart since it tries to load 2 scenes at the same time.

Is there a way to only send 1 on_area_entered() trigger regardless of the overlapping areas? Or make the triggers occur in sequence instead of simultaneously, so I can ignore the subsequent calls? Not sure how to approach this with my limited understanding, any feedback would be very appreciated.

They will trigger in-sequence, but in-sequence could still be multiple in the same frame. How are you changing scenes? You will have to either add a “debounce” variable or find a way to check state that the script isn’t already loading a combat scene.

1 Like

Exactly I came to say something similar as in creating a boolean that changes right when one area is triggered which prevents any other from triggering, unless theres a case where both trigger at the same time, which I’m not sure is possible but in that case maybe you can just store the triggers in a dictionary and pick the first one always disregarding the rest

1 Like

Oh they do act in sequence? I originally had it so that it emitted a signal through a SignalBus. I have a GameRoot scene that starts with a LevelScene child, and on receiving the signal it would add a CombatScene child, save the LevelScene node in a Global var, then remove the LevelScene child. So my issue was trying to do GameRoot.add_child(CombatScene) multiple times…

I’ve since changed it to set a Global boolean prepare_switch = true, and it does work! Thanks for the feedback, I think posting the question helped me rubber-duck it just enough to get there.

1 Like