I have a dialogue system where the contents are written in plain text, previously I could double click .txt and .md files inside the FileSystem panel to open and edit them. After creating an EditorImportPlugin for the dialogue resources I cannot open the text files by double clicking them in the FileSystem panel, instead it opens the inspector for my read-only resource, which is nice but I’d like to also open the Script screen with the source file’s contents.
I’ve tried these functions:
# Shows in Inspector, similar to double-clicking
EditorInterface.edit_resource(load(last_dialog_path))
# Does nothing, presumably because my Resource is not a Script
EditorInterface.edit_script(load(last_dialog_path))
# Poked around but found nothing of interest, looks like
# an up hill battle to insert my own text edit
var se := EditorInterface.get_script_editor()
for s in se.get_open_script_editors():
s.print_tree_pretty()
Really hoping there is a EditorInterface.open_text() tucked away somewhere. I can still click-and-drag the text files from the FileSystem panel into a specific spot in the Script screen, but that’s not the same behavior I want.
I don’t think it’s possible. When you use an EditorImportPlugin the txt file is imported and recognized as a Resource and not a text file. When opening a file, the editor first checks if the file is a Resource and, if it’s not, then tries to open it as a text file:
ScriptEditor::open_file() does a similar thing.
The TextFile resource is not exposed to scripting so it’s not possible to force the editor into opening it as a text file. And there’s no ResourceFormatLoader registered that loads a TextFile so ResourceLoader.load(<path>, "TextFile") also fails.
If you don’t have any extra configuration like exported properties in your imported resource then you could try to use a ResourceFormatLoader instead and register it only at runtime. This way the editor will still see it as a text file but it will be loaded as your resource at runtime.
I have found a semi-solution abusing the editor and one accessability feature emulating drag-and-droping the resource into the script editor. This is wildly fragile relying on child order of the script editor which may change in the future and the accessibility system may change too. I would certainly prefer if the ScriptEditor.open_file method was exposed to GDScript.
func _on_open_in_editor_pressed() -> void:
if last_dialog_path.is_empty():
return
var se := EditorInterface.get_script_editor()
#se.print_tree_pretty()
# This is actually evil and probably a terrible idea; path based on print_tree above
var items: Control = se.get_child(0).get_child(1).get_child(0).get_child(0).get_child(1)
drop_data_temp = {
"type": "files",
"files": [last_dialog_path],
}
self.accessibility_drag() # _get_drag_data
items.accessibility_drop()
drop_data_temp = {}
EditorInterface.set_main_screen_editor("Script")
# Still need to switch to the text file in the script screen
# and re-running this function opens a duplicate [unsaved*]
var drop_data_temp: Dictionary
func _get_drag_data(_at_position: Vector2) -> Variant:
if drop_data_temp.is_empty():
return null
return drop_data_temp