Search code examples
c++unreal-engine4oculus

How Can i detect plugging in/out Oculus touch controllers?


I'm editing it again, to make it as simple as I can. I want to have ability in my game (made in unreal engine 4) to detect if Oculus touch controllers are plugged in or not. Below you can find what i did so far.

First of all what i did so far: In OculusInput.cpp

  bool FOculusInput::IsControllerConnected(int32 ControllerIndex) const
    {
        for (const FOculusTouchControllerPair& ControllerPair : ControllerPairs)
        {
            if (ControllerPair.UnrealControllerIndex == ControllerIndex)
            {
                const FOculusTouchControllerState& ControllerState = ControllerPair.ControllerStates[(int32)EControllerHand::Left];

                if (ControllerState.bIsConnected)
                {
                    return true;
                }
                return false;
            }
            return false;
        }
        return false;
    }

Then I've added this in IMotionController.h :

virtual bool IsControllerConnected(int32 ControllerIndex)const
    {
        return false;
    }

Finally in MyGameMode I've added this:

UFUNCTION(BlueprintCallable, Category = "Controller")
    bool IsTouchConnected(int32 ControllerIndex);

TSharedPtr<IMotionController> MotionController;

and in .cpp

bool AMyGameMode::IsTouchConnected(int32 ControllerIndex)
{

    if (!MotionController.IsValid())
    {
    MotionController.Get(); //this doesn't work
    }

    if (MotionController.IsValid())
    {
        return MotionController->IsControllerConnected(ControllerIndex);
    }
    return false;
}

So this is how it should work: 1.I'm calling function AMyGameMode::IsTouchConnected(); 2.Then it calls IMotionController::IsControllerConnected via MotionController pointer. 3.Then due to being overriten in OculusInput, Oculus Function is called and returns my main target.

What is wrong now:

MotionController pointer seems to always be nullptr. I have no idea how to make it work.


Solution

  • So I've finally made it, so i wanted to post it here for anyone searching for it.

    First of all what you want to do is add to IMotionController new function:

    virtual bool IsControllerConnected(int32 ControllerIndex)const
        {
            return false;
        }
    

    Then you go to OculusInput and add this:

    .h

    virtual bool IsControllerConnected(int32 ControllerIndex)const override;
    

    .cpp

    bool FOculusInput::IsControllerConnected(int32 ControllerIndex) const
    {
        for (const FOculusTouchControllerPair& ControllerPair : ControllerPairs)
        {
            if (ControllerPair.UnrealControllerIndex == ControllerIndex)
            {
                const FOculusTouchControllerState& ControllerState = ControllerPair.ControllerStates[(int32)EControllerHand::Left];
    
                if (ControllerState.bIsConnected)
                {
                    return true;
                }
                return false;
            }
            return false;
        }
        return false;
    }
    

    Finally in your class you are adding this:

    .h

    #include "MotionControllerComponent.h"
    
    
    UFUNCTION(BlueprintCallable, Category = "Controller")
        bool IsTouchConnected(int32 ControllerIndex);
    
    
    TSharedPtr<class UMotionControllerComponent> MCComponent;
    

    .cpp

    bool YourClass::IsTouchConnected(int32 ControllerIndex)
    {
        TArray<IMotionController*> controllers = IModularFeatures::Get().GetModularFeatureImplementations<IMotionController>(IMotionController::GetModularFeatureName());
    
        for (IMotionController* MotionController : controllers)
        {
            return MotionController->IsControllerConnected(ControllerIndex);
        }
        return false;
    }
    

    With this code I can successfully check if my touch controller (left in this case) is connected or not :) Hope i could help anyone with it!