from
https://godotengine.org/qa/5972/list-files-that-been-dragged-onto-games-window-their-directory
I want to create a batch processing application in godot - that takes for input files that have been dragged and dropped by the user to the application window. Is that something that is possible to do in godot? I need to get that data and store it in an array
Other possible applications of this would be for plugins - being able to drag external files onto a godot plugin window.
After some investigation, i found there's a API for using D&D from the OS exposed to GDScript, there's a MainLoop::_drop_files
virtual function, and files_dropped(StringArray files, int screen)
signal on SceneTree
.
I've made a simple example, where you can drag images into the application window, and it'll show all images in a ItemList
:
Thank you! I am going to try this out today
files_dropped(StringArray files, int screen) is missing in the documentation!
:o
@blurymind, i think this is because this feature is not available in 2.0.x versions. Only on Github HEAD.
nice! I wonder how many other hidden features there are :)
Seeing as to how this feature exists already in godot head master - do you guys think I should close this? Is there anything that is missing in the current implementation?
Compiling now btw.
Well, IMO this should be closed since we already support it in master
. If there are bugs with the implementation itself, open a new issue, but if there are issues with the way it is exposed, you might decide it is better to comment here.
I think the file @TheHX provided should be included in the demos folder. It is awesome! :)
Mayby @TheHX demo put in godot-demo-projects ?
@TheHX Do you know how to tell this action to trigger only when the cursor is over a specific node?
Right now the files get loaded when I drag them anywhere on the app window - but I want them to load only when dragged onto a specific node!
I'm not sure if there's any way to do this, without using some crazy tricks. But this id be a nice improvement for the drag and drop, but i don't know how this should be exposed.
@TheHx What does the 'screen' (string) value do? I noticed that it always returns a zero.
I'm not sure, but i think the screen parameter is index of the from where the files are dragged, but as it seems the feature is not implemented:
https://github.com/godotengine/godot/blob/master/platform/windows/os_windows.cpp#L767
https://github.com/godotengine/godot/blob/master/platform/x11/os_x11.cpp#L1531
https://github.com/godotengine/godot/blob/master/platform/osx/os_osx.mm#L330
@TheHX perhaps it would be useful if all gui related nodes get a _drop_files signal
I don't think so, actually the D&D system in Godot is based on virtual functions, as you can see in this script.
I tried doing it with focus_enter and focus_exit signals and a variable. Didnt work. Focus enter signal requires the user to click on the node - which doesnt work with drag and drop.
Can you come up with any workarounds?
We might have to reopen this bug - because drag and drop does not seem to be properly exposed to gdscript at the moment.
@vnen can you reopen the bug? I shouldnt have closed it so early. Drag and drop is missing a very key element at the moment
@blurymind it's better if you open a new issue with a proper request.
Most helpful comment
After some investigation, i found there's a API for using D&D from the OS exposed to GDScript, there's a
MainLoop::_drop_files
virtual function, andfiles_dropped(StringArray files, int screen)
signal onSceneTree
.I've made a simple example, where you can drag images into the application window, and it'll show all images in a
ItemList
:drag_n_drop_os.zip