Godot gdscript

From j4flmao/agent-skills · View interactive version

Godot gdscript is an AI agent skill from the GitHub repository j4flmao/agent-skills. Use when the user asks about advanced GDScript, typed GDScript performance, Godot servers (RenderingServer, PhysicsServer), signals, threading in Godot 4 or bypassing the SceneTree for speed. Produces low-level Godot 4 techniques and profiled optimisations. Not for general Godot project setup (use g

How to use this skill

Skill file

Godot Engine Internal Architecture & GDScript Deep Patterns

Purpose

Godot's performance and architecture come from its split between a high-level SceneTree (Nodes) and low-level Server singletons (RenderingServer, PhysicsServer3D, AudioServer). GDScript rides on top of a core that is C++-driven, with a message-passing signal system.

Agent Protocol

Trigger

"gdscript", "godot servers", "RenderingServer", "PhysicsServer", "godot signals", "godot threading", "godot performance".

Input Context

Output Artifact

GDScript code and architecture guidance. No file unless requested.

Response Format

Godot: {4.x}
Layer: {SceneTree | Server}
Code: {snippet}
Verify: {profiler / monitor numbers}

1. Server Architecture (The Core Split)

SceneTree (high-level)
  Node2D/Node3D/Control...  -> translate gameplay to server commands
      |  node.add_child(),  set_transform(),  play()
      v
Servers (C++ singletons; most calls are safe from worker threads — see the "Thread-safe APIs" doc page)
  RenderingServer  -> BVH culling, batching, Vulkan/OpenGL backend
  PhysicsServer3D  -> solver, step
  AudioServer      -> mixer, spatialization
  NavigationServer3D

Nodes do NOT render. A MeshInstance3D only holds an RID (resource ID) pointing into the server's storage; rendering calls become server commands queued and flushed in a controlled order. Decoupling keeps gameplay threads isolated from GPU work.

1.1 RIDs (Resource IDs)

Every server object is referenced by an opaque RID:

var rdr = RenderingServer
var mesh_id = rdr.mesh_create()
# rdr.camera_create(), rdr.instance_create()

Advanced systems bypass the Node layer by talking directly to servers — essential for massive 2D particles, custom culling, or pre-render batching.

1.2 Multi-Viewport & Rendering Priority

2. The Signal / MessageQueue System

Signals implement the Observer pattern at the engine core.

# emit
node.emit_signal("damage_taken", amount)
# connect
node.damage_taken.connect(func(dmg): _hp -= dmg)

2.1 Deferred (Queued) Signals — Safety

When two frames' iteration can't be interleaved (physics callbacks modifying the tree), CONNECT_DEFERRED pushes the payload into a MessageQueue:

body_entered.connect(_on_entered, CONNECT_DEFERRED)

The queue flushes at the end of the frame in SceneTree::process, so the modification happens AFTER iteration completes — preventing mid-iteration tree corruption. Use deferred for: freeing nodes during signal callbacks, structural changes inside physics step.

2.2 Signal vs Poll

3. SceneTree Lifecycle & Queries

4. GDScript Language Deep Dive

4.1 Typing & Performance

GDScript is dynamically typed but performance-sensitive:

@export var speed: float = 10.0        # typed
var dict := {"a": 1}                    # inferred
func move(dir: Vector2, dt: float) -> void: ...

4.2 Built-in Data Types

| type | use | notes |

|------|-----|-------|

| Array | dynamic list | resize(), typed variants via Array[int] |

| PackedStringArray etc. | contiguous, fast | for hot data prefer these |

| Dictionary | keyed | avoid per-frame churn |

| Vector2/3, Transform2D/3D, Quaternion | math | built-in, fast |

4.3 Memory & Pooling

4.4 `await` & Coroutines

await get_tree().create_timer(1.0).timeout   # wait
await tween.finished
await http.request_completed

Rules: an await inside _process/_physics_process turns that callback into a coroutine, so the next frame starts a second overlapping run — keep awaits in event handlers or dedicated async functions.

5. Threads in Godot

5.1 WorkerThreadPool (4.x)

func _update_chunk(i: int) -> void:
    _results[i] = _compute(i)          # no SceneTree access here

func run_batch(n: int) -> void:
    var gid := WorkerThreadPool.add_group_task(_update_chunk, n)
    WorkerThreadPool.wait_for_group_task_completion(gid)

Use add_task() / wait_for_task_completion() for single jobs. Always wait on every task you add.

6. RenderingServer Custom Draw (Pseudowindow)

# draw a thousand quads without nodes
# inside a Node2D: attach a raw canvas item under this node's canvas item
var item := RenderingServer.canvas_item_create()
RenderingServer.canvas_item_set_parent(item, get_canvas_item())
for r in rects:
    RenderingServer.canvas_item_add_rect(item, r, Color.WHITE)
# free with RenderingServer.free_rid(item) in _exit_tree()

This bypasses hundreds of nodes → huge win for HUD overlays or particles.

7. PhysicsServer3D Direct Call

Create bodies/areas via RID, control stepping, add shapes — full control without Nodes:

var shape := PhysicsServer3D.box_shape_create()
PhysicsServer3D.shape_set_data(shape, Vector3(0.5, 0.5, 0.5))   # half extents
var body := PhysicsServer3D.body_create()
PhysicsServer3D.body_set_mode(body, PhysicsServer3D.BODY_MODE_RIGID)
PhysicsServer3D.body_add_shape(body, shape, Transform3D())
PhysicsServer3D.body_set_space(body, get_world_3d().space)
# free both RIDs with PhysicsServer3D.free_rid() when done

Use when you need many bodies with low overhead (spatial hash of collision-heavy areas).

8. Common Performance Traps & Fixes (GDScript)

| Trap | Fix |

|------|-----|

| Untyped loop over big Array | typed Array[T] or Packed*Array |

| get_node() per frame | @onready var x = $path |

| instantiate/free per frame | recycle from a pool |

| signals for per-frame changes | poll local state |

| load() in a loop | preload/cache |

| heavy .grep() style regex in _process | move to events |

| many CanvasItems redraw | call queue_redraw() only on change |

| shader uniforms per frame | Material.set_shader_parameter sparingly |

9. GDScript Style Conventions

10. Decision: GDScript vs C# vs GDExtension

| | GDScript | C# | GDExtension |

|--|----------|-----|-------------|

| Iteration speed | fast to write | fast, IDE=VS/Rider | native C++ fast |

| Engine API | full | full (via .NET) | all + internals |

| Performance | good-ish | good | best |

| Use | gameplay logic | tools, teams with C# | engine features |

Rule: default GDScript; C# for heavy tooling/editor plugins; GDExtension only when profiling proves C++ needed.

11. References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.