Godot 4 and GDScript gotchas (2D)
godotgdscript2dphysicssignalsgamepadinput
Bugs and API surprises hit while building two 2D games in Godot 4 (Fall on 4.6.1 with the GL Compatibility renderer, and Fight on 4.4). Most are one-liners once known. Shader-specific issues are in Godot canvas shaders for organic 2D visuals.
API names and types
- 2D shapes are
RectangleShape2D/CapsuleShape2D;BoxShape2Ddoes not exist (that is 3DBoxShape3D). sinf()/cosf()do not exist in GDScript; usesin()/cos(). But the typed helpersabsf,clampf,lerpfdo exist.- A parameter named
showshadowsNode.show(); pick another name. var x := a * b + ccan fail type inference when the arithmetic mixes types; declare it (var screen_pos: Vector2 = ...).- Pass enum values (
WeaponType.SHOTGUN), never raw integers, or reordering the enum breaks callers. - A declared flag that is never checked (e.g.
has_double_jump) is a common audit find: grep each exported or declared flag for its readers.
Scene tree, signals and timing
- Signals emitted in
_ready()can hit listeners that are not ready yet. The player node earlier in the tree emittedhealth_changedbefore the HUD's@onreadyvars existed ("Invalid access to property 'value' on Nil"). Emit initial state deferred:health_changed.emit.call_deferred(...). - Look up other nodes by group with
call_deferredin_ready(), not via@export NodePath, when the other node may register later. - Use autoloads directly (
GameState.property), not via a node path. - Don't hardcode resource UIDs; let Godot generate them.
is_instance_valid()whenever iterating nodes another system might free.- Programmatic spawning:
Area2D.new(),set_script(),add_child(), theninit(...)._ready()runs duringadd_child, beforeinit, so_ready()must not depend on init parameters. - When a reset clears state, it must also emit the matching signals (e.g.
speed_boost_changed(false)), or listeners like a HUD bar stay stale. Expose public getters instead of letting UI read private vars.
Physics
- A
StaticBody2Dmoved from code does not carry bodies standing on it. UseAnimatableBody2Dfor moving platforms. body_enteredcan fire for several bodies in one physics frame beforequeue_freetakes effect, so a projectile damaged twice. Guard with ahas_hitflag.- Duplicate shared collision shapes before resizing them per instance (
shape.duplicate()), or every instance resizes. - Use
move_toward()for acceleration instead of snapping velocity. - External forces (wind) must be additive (
velocity.x += f), nevermove_towardon another object's velocity, which stomps the player's own movement. - Forces and anything frame-rate dependent belong in
_physics_process; smooth values with1.0 - exp(-rate * delta), not a fixed lerp factor. - Spawn hazards relative to the player position, not the screen or level center.
Input and gamepad
- Gamepad UI navigation is not automatic: the built-in
ui_acceptdoes not reliably include joypad buttons. At startup, add A toui_accept, B toui_cancel, and the D-pad toui_up/down/left/rightin the InputMap from code. - Handle
ui_cancelin menus so B backs out (settings_input, pause menu_unhandled_input). - Remapping UI: when a new binding conflicts, swap the two actions rather than refusing.
- Persist bindings and settings in a
ConfigFileunderuser://. - Clear input buffers on
NOTIFICATION_UNPAUSED(pausable nodes receive it).
Rendering and layers
Sprite2Dwithout a texture is invisible; useColorRectfor placeholders.- With GL Compatibility there is no
WorldEnvironmentglow or post-processing; do bloom and vignette in a canvas shader on aCanvasLayer. - Put the HUD
CanvasLayerabove any full-screen overlay layer (Fall: overlay 1, HUD 11). - Despawn distances must exceed the screen size (600 px popped visibly at 1080p; 1000 px fixed it).
Audio
- Kill the active tween before starting a new fade, or two fades race.
- Looping OGG tracks break fade-out logic; export non-looping tracks for crossfaded music.
Project file
- A duplicated
[configuration]section inproject.godot(strayconfig_version) must be removed by hand.
See also
- Godot canvas shaders for organic 2D visuals
- Architecture for a 2D action game in Godot 4
- Platformer movement feel (coyote time, jump buffering, wall jumps)
- Game juice checklist (feedback layers that make a game feel good)