Up to date

This page is up to date for Godot 4.0. If you still find outdated information, please open an issue.

Running code in the editor

What is @tool?

@tool is a powerful line of code that, when added at the top of your script, makes it execute in the editor. You can also decide which parts of the script execute in the editor, which in game, and which in both.

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 jumppads 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.

Danger

@tool scripts 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 @tool scripts. Be extremely cautious when manipulating the scene tree, especially via Node.queue_free, as it can cause crashes if you free a node while the editor runs logic involving it.

How to use it

To turn a script into a tool, add the @tool annotation at the top of your code.

To check if you are currently in the editor, use: Engine.is_editor_hint().

For example, if you want to execute some code only in the editor, use:

GDScriptC#

  1. if Engine.is_editor_hint():
  2. # Code to execute when in editor.
  1. if (Engine.IsEditorHint())
  2. {
  3. // Code to execute when in editor.
  4. }

On the other hand, if you want to execute code only in game, simply negate the same statement:

GDScriptC#

  1. if not Engine.is_editor_hint():
  2. # Code to execute when in game.
  1. if (!Engine.IsEditorHint())
  2. {
  3. // Code to execute when in game.
  4. }

Pieces of code do not have either of the 2 conditions above will run both in-editor and in-game.

Here is how a _process() function might look for you:

GDScriptC#

  1. func _process(delta):
  2. if Engine.is_editor_hint():
  3. # Code to execute in editor.
  4. if not Engine.is_editor_hint():
  5. # Code to execute in game.
  6. # Code to execute both in editor and in game.
  1. public override void _Process(double delta)
  2. {
  3. if (Engine.IsEditorHint())
  4. {
  5. // Code to execute in editor.
  6. }
  7. if (!Engine.IsEditorHint())
  8. {
  9. // Code to execute in game.
  10. }
  11. // Code to execute both in editor and in game.
  12. }

Note

Modifications in the editor are permanent. For example, in the following case, when we remove the script, the node will keep its rotation. Be careful to avoid making unwanted modifications.

Try it out

Add a Sprite2D node to your scene and set the texture to Godot icon. Attach and open a script, and change it to this:

GDScriptC#

  1. @tool
  2. extends Sprite2D
  3. func _process(delta):
  4. rotation += PI * delta
  1. using Godot;
  2. [Tool]
  3. public partial class MySprite : Sprite2D
  4. {
  5. public override void _Process(double delta)
  6. {
  7. Rotation += Mathf.Pi * (float)delta;
  8. }
  9. }

Save the script and return to the editor. You should now see your object rotate. If you run the game, it will also rotate.

../../_images/rotating_in_editor.gif

Note

If you don’t see the changes, reload the scene (close it and open it again).

Now let’s choose which code runs when. Modify your _process() function to look like this:

GDScriptC#

  1. func _process(delta):
  2. if Engine.is_editor_hint():
  3. rotation += PI * delta
  4. else:
  5. rotation -= PI * delta
  1. public override void _Process(double delta)
  2. {
  3. if (Engine.IsEditorHint())
  4. {
  5. Rotation += Mathf.Pi * (float)delta;
  6. }
  7. else
  8. {
  9. Rotation -= Mathf.Pi * (float)delta;
  10. }
  11. }

Save the script. Now the object will spin clockwise in the editor, but if you run the game, it will spin counter-clockwise.

Editing variables

Add and export a variable speed to the script. To update the speed and also reset the rotation angle add a setter set(new_speed) which is executed with the input from the inspector. Modify _process() to include the rotation speed.

GDScriptC#

  1. @tool
  2. extends Sprite2D
  3. @export var speed = 1:
  4. # Update speed and reset the rotation.
  5. set(new_speed):
  6. speed = new_speed
  7. rotation = 0
  8. func _process(delta):
  9. rotation += PI * delta * speed
  1. using Godot;
  2. [Tool]
  3. public partial class MySprite : Sprite2D
  4. {
  5. private float _speed = 1;
  6. [Export]
  7. public float Speed
  8. {
  9. get => _speed;
  10. set
  11. {
  12. // Update speed and reset the rotation.
  13. _speed = value;
  14. Rotation = 0;
  15. }
  16. }
  17. public override void _Process(double delta)
  18. {
  19. Rotation += Mathf.Pi * (float)delta * speed;
  20. }
  21. }

Note

Code from other nodes doesn’t run in the editor. Your access to other nodes is limited. You can access the tree and nodes, and their default properties, but you can’t access user variables. If you want to do so, other nodes have to run in the editor too. Autoload nodes cannot be accessed in the editor at all.

Reporting node configuration warnings

Godot uses a node configuration warning system to warn users about incorrectly configured nodes. When a node isn’t configured correctly, a yellow warning sign appears next to the node’s name in the Scene dock. When you hover or click on the icon, a warning message pops up. You can use this feature in your scripts to help you and your team avoid mistakes when setting up scenes.

When using node configuration warnings, when any value that should affect or remove the warning changes, you need to call update_configuration_warnings . By default, the warning only updates when closing and reopening the scene.

GDScript

  1. # Use setters to update the configuration warning automatically.
  2. @export var title = "":
  3. set(p_title):
  4. if p_title != title:
  5. title = p_title
  6. update_configuration_warnings()
  7. @export var description = "":
  8. set(p_description):
  9. if p_description != description:
  10. description = p_description
  11. update_configuration_warnings()
  12. func _get_configuration_warnings():
  13. var warnings = []
  14. if title == "":
  15. warnings.append("Please set `title` to a non-empty value.")
  16. if description.length() >= 100:
  17. warnings.append("`description` should be less than 100 characters long.")
  18. # Returning an empty array means "no warning".
  19. return warnings

Instancing scenes

You can instantiate packed scenes normally and add them to the scene currently opened in the editor. By default, nodes or scenes added with Node.add_child(node) are not visible in the Scene tree dock and are not persisted to disk. If you wish the node or scene to be visible in the scene tree dock and persisted to disk when saving the scene, you need to set the child node’s owner property to the currently edited scene root.

If you are using @tool:

GDScriptC#

  1. func _ready():
  2. var node = Node3D.new()
  3. add_child(node) # Parent could be any node in the scene
  4. # The line below is required to make the node visible in the Scene tree dock
  5. # and persist changes made by the tool script to the saved scene file.
  6. node.set_owner(get_tree().edited_scene_root)
  1. public override void _Ready()
  2. {
  3. var node = new Node3D();
  4. AddChild(node); // Parent could be any node in the scene
  5. // The line below is required to make the node visible in the Scene tree dock
  6. // and persist changes made by the tool script to the saved scene file.
  7. node.Owner = GetTree().EditedSceneRoot;
  8. }

If you are using EditorScript:

GDScriptC#

  1. func _run():
  2. # `parent` could be any node in the scene.
  3. var parent = get_scene().find_node("Parent")
  4. var node = Node3D.new()
  5. parent.add_child(node)
  6. # The line below is required to make the node visible in the Scene tree dock
  7. # and persist changes made by the tool script to the saved scene file.
  8. node.set_owner(get_scene())
  1. public override void _Run()
  2. {
  3. // `parent` could be any node in the scene.
  4. var parent = GetScene().FindNode("Parent");
  5. var node = new Node3D();
  6. parent.AddChild(node);
  7. // The line below is required to make the node visible in the Scene tree dock
  8. // and persist changes made by the tool script to the saved scene file.
  9. node.Owner = GetScene();
  10. }

Warning

Using @tool improperly can yield many errors. It is advised to first write the code how you want it, and only then add the @tool annotation to the top. Also, make sure to separate code that runs in-editor from code that runs in-game. This way, you can find bugs more easily.