See docs: Running Code in the Godot Editor
Overview
To achieve this, you can include the .tool
parameter to the @Godot
macro, like this:
You can use it for doing many things, but it is mostly useful in level design for visually presenting things that are hard to predict ourselves. Here are some use cases:
- If you have a cannon that shoots cannonballs affected by physics (gravity), you can draw the cannonball’s trajectory in the editor, making level design a lot easier.
- If you have jump pads with varying jump heights, you can draw the maximum jump height a player would reach if it jumped on one, also making level design easier.
- If your player doesn’t use a sprite, but draws itself using code, you can make that drawing code execute in the editor to see your player.
.tool
extension
.tool
extensions run inside the editor, and let you access the scene tree of the currently edited scene. This is a powerful feature which also comes with caveats, as the editor does not include protections for potential misuse of@Godot(.tool)
scripts. Be extremely cautious when manipulating the scene tree, especially viaNode/queueFree
, as it can cause crashes if you free a node while the editor runs logic involving it.
Detecting the Editor
In your code, you can call the isEditorHint()
to determine whether the code is running inside the editor or in the game, and adjust your various methods accordingly.