I have a project I am working on in which I have to read a file name after it’s dragged into the UE4 game window. I know how to do this in a very basic C++ app, using the following code:
#include <iostream>
using namespace std;
int main(int argc, char* argv[]) {
cout << "argc = " << argc << endl;
for (int i = 0; i < argc; i++)
cout << "argv[" << i << "] = " << argv[i] << endl;
std::cin.ignore();
return 0;
}
It’s pretty simple, I’m just parsing through the argument and printing the output. I’m curious what the best approach would be to implement something like this within UE4. Ideally it would be great to also get some information such as the mouse position for UI hit checks, but that shouldn’t be too difficult. I saw there were some new Blueprint nodes for reading command line args, but figured this is optimally built in code!
You can read command line that UE4 got executed from here:
But i’m not sure if it’s gonna change due to file drop and you still don’t have event when this is happening, you definitely won’t be able to hook up in to main() of the engine without modifying the engine code.
But why not use UI code for this via Slate? Slate widget is giving events for drag and drop events
in event you are giving FDragDropEvent
from which oyu can get dragdrop operation which has diffrent types:
and you are interested with this one:
And as you see you can handle both text and files, FDragDropEvent is also FPointerEvent so you can read it like normal mouse event:
Now to do that you need to make Slate widget, if you don’t know Slate that you need to learn slate which documentaion is quite minimal:
Best way is too look on code of existing widget, you can just extend existing widget you want to use just with that event, there possibility that widget already handles external files, but i would not be so sure.
You make UI in Slate by making widget based from SCompoundWidget, so there hsould be tutorials around
But in your case if you want to make a widget that only detects file drop you can just use more lite SLeafWidget
Now once you got Slate widget working, best way to implment it in to the game is to make UMG widget out of it, if you not realized it yet UMG is just a wrapper of Slate so they are usable in blueprint, but make UWidget insted of UUserWidget since oyu not making composition of UMG widgets but Slate
UMG widget generate Slate widget in RebuildWidget() function, where you just return description of slate widget… and thats it you got it in UMG, UMG code should handle the rest, only thing you need to do is make functions for blueprint so you detect drop in blueprint. Same as Slate best way to learn how to make UMG widget is just watch code of existing widgets, again they just Slate wrappers so they quite simple and a lot easier to understand then Slate widgets
, thank you so much for all this information! Out of curiosity, aren’t the UMG functions designed for internal drag / drop operations? For example, moving an inventory menu around on the screen rather than dragging a file into the game window.
That too, but you can use those to hook up to them, they are virtual and protected so they are intended to be overrideable by farther widgets for some reason, in the end intention does not matter what matter is then this function is called which you can use. It’s saves a lot of time as other way you would need to do things longer way i mentioned above.
Just remember to call super like Super::NativeOnDrop etc. because override will replace the code from original function and indeed is needed for rest of the code to work, so you need to call original overrided function to not lock up the code, same as you do with BeginPlay and Tick.
Ah I see, so essentially just using it as a signal for the dropping action itself? I am trying to figure the best way of opening up the widget to read commandline information after that has happened. One of the issues I’m running into already is that the actual UE4 game window doesn’t seem to accept drag / drop operations externally (at least not natively). So far, this appears to be the closest bit of code I can find relating to this kind of operation (thanks for the link!)