Tool script - How to get selected resource?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Mordi

Using a tool script, is there any way to get the currently selected resource in the FileSystem widget?

It would be really neat to have some sort of callback for “OnResourceSelected” in the FileSystem, but so far I’ve found no way to get this information. In addition, I don’t really see any workarounds, other than creating my own resource browser.

Specifically, what I’m looking to do is create a resource based on selecting audio streams. The created resource should take its name from one of the selected audio files, and use them to populate its own list of audio streams.

:bust_in_silhouette: Reply From: Wakatta

Hmmmmmmmmmmmmmmmmm (pulls on Gandolf sized beard)

Honestly can only see this being possible with an EditorPlugin or Editorscript

tool
extends EditorScript

var editor_interface = get_editor_interface()
var last_path = ""

func _on_selection_changed(path):
	if path.get_extension():
		# its a file selected
	else:
		# its a folder

func _run():
	if last_path != editor_interface.get_current_path():
	    last_path = editor_interface.get_current_path()
	    _on_selection_changed(last_path)

Oh, this actually works! Sweet.

I ended up using the _process function instead of _run, and extended EditorPlugin instead of EditorScript - as I already had the beginnings of a plugin set up.

Do you know how I would extend this so it takes into account multiple selected resources? Right now I only know how to get the last selected one.

That said, I can already do what I want based on your help so it’s greatly appreciated.

Mordi | 2023-03-05 20:13

Yeah while writing the above had EditorPlugin with signals in mind but could not remember all the syntax.

To get all the files of a selected folder path you can use a recursive function as seen here

To get multiple selections really not sure though how Godot handles that so some testing may be necessary.

For example in your EditorPlugin you can use the _input func to pool the last_path in an array

var multiple_selection = Array()
var pool = false

func _input(event):
    if event.pressed:
        if event is InputEventKey:
            if even.scancode == KEY_CONTROL:
                pool = true
        if event is InputEventMouseButton:
            If not last_path in multiple_selection and pool = true:
                multiple_selection.push_back(last_path)
                # do related stuff here
    else:
        if event is InputEventKey:
            if even.scancode == KEY_CONTROL:
                pool = false
                multiple_selections.clear()

Wakatta | 2023-03-05 21:04