I'm trying to make a tool script in 3.0 with the following requirements:
- Parts of the script run in the editor only. In 2.0 you could check if you were in the editor with
get_tree().is_editor_hint()
, but this has been removed in 3.0 and I can't see any replacement in the SceneTree class.
- Some way to get the current 2D snap settings. Doesn't look like this is possible.
- Test if the node is currently selected in the editor. You can do this with
self in EditorPlugin.new().get_editor_interface().get_selection()
, but it seems like there should be a way to do this without creating an EditorPlugin.
- Handle input events. I tried
set_process_input(true)
, but it doesn't receive input events in the editor.
I can do #1, #3, and #4 if I use an EditorPlugin with a custom node. First I give my custom node script this function:
func plugin_input(xform, event):
print(xform, event)
Then my EditorPlugin forwards any input events to the currently selected node if that node is the custom node. The only way I've found to test if the selected object in handles(object) is the custom node is to check if it has the custom node script.
tool
extends EditorPlugin
var current_selection
var selection_is_custom_type
func _enter_tree():
add_custom_type("MyCustomType", "Node2D", preload("custom_script.gd"), preload("icon.png"))
func _exit_tree():
remove_custom_type("MyCustomType")
func handles(object):
current_selection = object
selection_is_custom_type = object.get_script() == preload("custom_script.gd")
func forward_canvas_gui_input(xform, event):
if selection_is_custom_type:
current_selection.plugin_input(xform, event)
This is fine but I would sometimes like to be able to create simple tool scripts without making an EditorPlugin or a custom type. I think testing if a node is selected in the editor and then handling inputs is pretty essential for making tool scripts so maybe there's a way to do this that I'm not seeing?