Implement Leap Motion Controller for gesture handling

Hey guys,

I’m struggling with Leap Motions C++ API to get gesture handling in my UE4 project to work. By now, the gesture handling works but the approach seems incredibly hacky to me and i’m pretty sure thats not the best way.

I’ve got this so far inside a tick function:

    Controller controller;
    controller.enableGesture(Gesture::TYPE_CIRCLE);
    controller.enableGesture(Gesture::TYPE_SWIPE);
    if (controller.isConnected()) {
    		Frame currentFrame = controller.frame();
    		GestureList gestures = currentFrame.gestures();
    		SwipeGesture swipe = Gesture::invalid();
    		CircleGesture circle = Gesture::invalid();
    		for (Gesture g : gestures)
    		{
    			switch (g.type())
    			{
    			case Gesture::TYPE_CIRCLE:
    				debug(FString::Printf(TEXT("CIRCLE WOOHOOO %d"), currentFrame.id()), FColor::Green);
    				if (g.isValid()) {
    					circle = CircleGesture(g);
    					this->LeapCircleEvent(circle);
    					//custom function
    				}
    				break;
    			case Gesture::TYPE_SWIPE:
    				debug(FString::Printf(TEXT("SWIPE YAAAR %d"), currentFrame.id()), FColor::Blue);
    				if (g.isValid()){
    					swipe = SwipeGesture(g);
    					this->LeapSwipeEvent(swipe, getSwipeDirection(swipe));
    					//custom function
    				}
    				break;
    			default:
    				break;
    			}
    		}
       }

Because I derived from the LeapMotionControllerComponent, i have no “BeginPlay” or similar functions where I could set my controller and its settings once at start, instead i have to set it inside of my tick function over and over again.


Setting up Controller as a variable in header file is not possible aswell, due to its … strange “instant” constructor trying to directly declare it (-> crash)


Is there any way to do this in a smarter way?
And is it somehow possible to set up a Controller object globally, to use it in different functions and somehow set settings at startup? (without a beginplay method)

**

Any help or examples of actual real code (not leapmotion doc snippets) are appreciated.

**

I would create a pointer to a Controller and initialise it in your constructor, you can do this in the plugin class if you’re implementing one or else have a singleton class. I did this in my Leap blueprint plugin before the official implementation was available and it worked without issues.

//in class declaration as private member variable
Leap::Controller* Controller;

//In class constructor
//LeapListener was a custom class inheriting from Leap::Listener    
Listener = new LeapListener(); 
Controller = new Leap::Controller(*Listener);

You could use OnRegister() instead of the class constructor. Just make sure you call Super::OnRegister() in your overriden body as well.

Thanks for your suggestions twiddle, i’ll try to use the OnRegister function and report back!

Concerning the initialization in my constructor… ye, that’s what I initially wanted to do, but like i already mentioned, this does not work for me :frowning: Just double checked… It compiles successfully but then throws this crash if I try to start the level in editor
Unknown exception - code c06d007e (first/second chance not available)

KERNELBASE + 109229 bytes
UE4Editor_MyProjectname!__delayLoadHelper2() + 393 bytes [f:\dd\vctools\delayimp\delayhlp.cpp:315]
UE4Editor_MyProjectname!_tailMerge_Leap_dll() + 63 bytes
[...]

If I debug my level, VS’ additional info gives “0xC06D007E: Module not found”…

Have you encountered something similar? I think this one gets thrown because this “early” declaration of Leap stuff cant’t find/communicate with the plugin at this level…

If I could fix this issue, this would solve all my problems!

Looks like the plugin module isn’t loaded at the time you’re wanting to access the functionality, or, the plugin module isn’t being found on the path for the process… Did you say if you initialise it in your Tick that this works? If so, that would seem to preclude the latter from being the cause, and maybe it’s simply a matter of the plugin module not being loaded into the process at the time the constructor is called. Unfortunately I was making a plugin that consumed the native C++ leap dll rather than using any sort of official integration, so perhaps somebody that’s used the integration can offer further comment here…

I think that’s the reason… it seems it’s getting loaded later on and the constructor of my class tries to access it too early.

Yes, If i initialize it in my tick function (which obviously is getting called in editor itself for the first time) it works like a charm…

I also tried to change the loading behaviour somehow in uplugin/uproject files, but that did not change anything and i’m not sure if i put the right LoadingPhases. (I have no experience in modifying these kind of files)

At a guess, I’d say the loadingPhase is what you want to be investigating, my advice would be to take a peek at the documentation and see if that has the info, otherwise, maybe try looking at the LoadingPhase that the leap integration module uses?

Hi,I want to use the Leap Motions C++ API in my UE4 C++ project.But I don’t know how to include it.Could you help me?