There are unlimited examples. This keyword just makes your script run in the editor. It may be mostly used to automate tasks though.
You may want to update the CollisionPolygon2D
of a body to automatically match a Polygon2D
.
When tools is enabled, the Canvasitem._draw
method is also called in the editor, allowing you to preview your drawing code in the canvas editor.
Another example could be automating 9-Slice when drawing tiles on a TileMap. Supposing TileMap emits a signal when the tile data changes (which sadly it doesn't), I can listen to that signal and update the tile data:
tool
extends TileMap
func _ready():
connect("tiles_changed", self, "_update_9slice()"
func _update_9slice():
# All the logic to update the tiles goes here
update()
You can also use the Scene -> Run Script option to run a script in the editor. This requires your script to extends EditorScript
and to have tool
enabled. You must override the method EditorScript._run
and add your logic there (similar to _ready
).