54. Build a clean native extension
A reliable extension has four pieces: a clear entry point, owned native nodes, a public adventure interface, and cleanup. The native button and cube examples show the pattern without changing the engine’s source.
| Part | Example decision |
|---|---|
| Entry | A GUI button calls run, or a subscribed room event starts the feature. |
| State | Saved puzzle state stays in Variable; transient node references stay in a global script. |
| Ownership | Native UI is a script child; room visuals are attached to the room and cleaned up on replacement. |
| Communication | Use PCA actions/events for adventure work and signals for local nodes. |
| Cleanup | Stop timers, remove temporary nodes, and tolerate room changes invalidating references. |
| Distribution | Include actual resource files and test the exported game, not only the editor. |
Keep the GDScript record small enough to understand. When the project needs a larger file-backed component, put that component in a custom folder in the editable Godot project and leave a small PCA bridge around it.
A native extension is most useful when it adds something concrete: a special inspection view, a custom control, an external data reader or a visual effect that the authored actions do not already cover.
