body_entered fires every time any physics body enters the area, so a scare connected to it plays again each time the player steps back in, and also for enemies and props that wander through. In our Godot 4.7 test an enemy walked through once and then the player walked in and out three times: the scare played 4 times. Connecting with CONNECT_ONE_SHOT played it once, but for the enemy, so the player never saw it. Checking that the body is in the player group, remembering that it fired and turning monitoring off played it once, for the player.
The code
Save it as once_trigger.gd, add your player to the group player (Node panel, Groups), and in the level's script call preload("res://once_trigger.gd").arm($ScareZone, func(body): $Scare.play()). Pass another group name as the third value, or &"" to accept any body. reset($ScareZone) arms it again, for example when the player retries from a checkpoint.
extends RefCounted
## Turns an Area3D into a trigger that fires once, and only for the player:
## a jump scare, a cutscene, a door slamming, a checkpoint message.
## body_entered fires every time any body walks in, enemies and props too.
## OnceTrigger.arm($ScareZone, func(body): $Scare.play())
## OnceTrigger.arm($Door, slam_door, &"player") # group the body must be in
## Put your player in the "player" group (Node panel > Groups), or pass &"".
static func arm(area: Area3D, action: Callable, group := &"player") -> void:
area.body_entered.connect(func(body: Node3D) -> void:
if group != &"" and not body.is_in_group(group):
return # an enemy or a crate walked in: not our trigger
if area.has_meta(&"_fired"):
return
area.set_meta(&"_fired", true)
area.set_deferred("monitoring", false) # stop watching, once this signal has finished
action.call(body))
static func reset(area: Area3D) -> void: # arm it again, e.g. when the level restarts
area.remove_meta(&"_fired")
area.monitoring = true
What we measured
An Area3D box 2 m wide. An enemy CharacterBody3D, not in any group, walked through it once, then the player, in the player group, walked in and out three times. Two runs gave the same results.
body_entered.connect(): the scare played 4 times, for Enemy, Player, Player, Player.body_entered.connect(..., CONNECT_ONE_SHOT): once, for Enemy. The player walked in three times and nothing happened.- The script: once, for Player, and monitoring was off afterwards. After reset() and one more step in: twice in total.
Why it happens
An Area3D reports every body that enters it, whatever it is, every time it enters. A one-shot connection only fixes the every time part: it disconnects after the first body, whoever that was. The script answers both questions, who and how often, and switches monitoring off so the area stops doing work for a trigger that is spent. It switches it off with set_deferred(), so the change happens after the signal has finished.
What to change
- A zone that should hurt every second the player stands in it, not once: a damage zone.
- The same scream twice in a row: a shuffle bag. Fog that still shows on the web: horror fog on Compatibility.
What the player sees when it fires: Horror Atmosphere VFX for Godot 4 has 45 ready-made horror effects in Modern and Retro PSX looks, including flickering and dying bulbs, a dying flashlight, Ground Fog, glowing eyes and ghosts.
Tested in Godot 4.7 with the Compatibility renderer.
Ready-made: Horror Atmosphere VFX for Godot 4 has 45 ready-made horror effects in Modern and Retro PSX looks, including flickering and dying bulbs, a dying flashlight, Ground Fog, glowing eyes and ghosts.
More how-tos for Horror Atmosphere VFX for Godot 4: Dark horror fog that works in Compatibility · Play random sounds and events without obvious repeats
More how-tos: Hazards, traps and attacks
- Turn a 2D character left without the flicker
- A line or rectangle attack that hits what the player sees
- An attack that hits even when a frame is slow
- Growing one cloud or aura zone grows all of them
- A parry window that is the same length on every screen
- A pair of portals that sends you through once
- An expanding shockwave that hits each enemy once
- Explosion damage that walls block, on the frame it goes off
- A fire or poison zone that keeps hurting while you stand in it
- A spike trap that hits whoever is already standing on it
- Who is inside an AoE attack circle
- Block only attacks from the front
- A sword hitbox that hits each enemy once per swing
- Slows and roots that overlap
- Blink forward without going into or through walls
- Knockback that the walk code does not cancel
- Random spots spread evenly inside a circle
- Keep a pressure plate down while anything is still on it
- Refill the guard meter only after the hits stop
- Heal from the damage that actually landed
- Let the shield take what it can and pass the rest of the hit to health
- A saw blade that slides along its path at a steady speed
- Explosion damage that reaches big enemies
- A spinning laser that hits everything it sweeps over