Skip to content

Getting Started

João Pedro Dante edited this page May 6, 2022 · 20 revisions

Requirements

  • At least one Nintendo Switch Joy Con
  • A Bluetooth adapter compatible with Joy Con
  • Unreal Engine 4.25.x (It is possible that it works in previous versions)

Known issues

  • When connecting a Joy Con in the editor it will remain connected even after the simulation/play is over and will stop working when playing again, the only solution is to resume de connection with ResumeConnection node.

First step

Cloning the repository

Clone the repository and you're ready to go to the next step.

Importing to an existing project

  1. Clone the repository
  2. Convert your project to C++ (If it is only a blueprint)
  3. Copy the Plugins folder inside the repository to your project's root folder
  4. Right click on your project's <name>.uproject and select "Generate Visual Studio project files"
  5. Open the project in Visual Studio (Recommended) or your preferred IDE
  6. Compile your project, if all goes well you are ready to proceed 😸

Second step

Now it's time to pair the joy cons with the operating system. If you experience difficulty you can follow the tutorial below.

How to connect a Nintendo Switch controller to your PC

After pairing you will need to create code for the controls to connect to the plugin.

1. Using Blueprint API

You can use the following examples:

Connect and Attach

Code example. Code for Ctrl + C and Ctrl + V

Disconnect and Detach

Code example. Code for Ctrl + C and Ctrl + V

Keep in mind that these examples are not recommended for production. In addition there is a problem with the joy cons stop working when using the editor as mentioned at the top of this page.

Using C++ API

Importing modules

Modify your <Project Name>.Build.cs as below:

PublicDependencyModuleNames.AddRange(new string[] {
    "Core",
    "CoreUObject",
    "Engine",
    "InputCore",
    // Add this as public dependency
    "JoyConDriver"
});

PrivateDependencyModuleNames.AddRange(new string[] {
    // Add this as private dependency
    "InputDevice",
});

Importing IJoyConDriverModule

// It is recommended to use this code within Begin Play
// Get JoyConDriver module
TArray<IJoyConDriverModule*> JoyConInputApis = IModularFeatures::Get().GetModularFeatureImplementations<IJoyConDriverModule>(IJoyConDriverModule::GetModularFeatureName());
IJoyConDriverModule *JoyConModule; // Add this variable to the UClass that uses the module.
for (IJoyConDriverModule* JoyConInputApi : JoyConInputApis) {
    if (JoyConInputApi == nullptr) continue;
    if (!JoyConInputApi->IsAvailable()) continue;
    JoyConModule = JoyConInputApi;
    break;
}
// ...

Using IJoyConDriverModule

Connect and Attach

// ...
if (JoyConModule != nullptr) {
    TArray<FJoyConInformation>* FoundControllers = JoyConModule->Get().SearchForJoyCons();
    bool UseImu = true; // Use accelerometer and gyroscope
    bool UseLocalize = true;
    float Alpha = 0.05f;
    for (const FJoyConInformation JoyConInfo : *FoundControllers) {
        if (JoyConInfo.IsConnected) continue;
	int ControllerId = 0;
	JoyConModule->Get().ConnectJoyCon(JoyConInfo, UseImu, UseLocalize, Alpha, ControllerId);
    }
    TArray<FJoyConInformation>* ConnectedControllers = JoyConModule->Get().GetConnectedJoyCons();
    int GripIndex = 0; // Set UE4 controller index
    for (const FJoyConInformation JoyConInfo : *ConnectedControllers) {
        if (JoyConInfo.IsAttached) continue;
	JoyConModule->Get().AttachJoyCon(JoyConInfo.ControllerId, GripIndex);
    }
}
// ...

Disconnect and Detach

// ...
if (JoyConModule != nullptr) {
    TArray<FJoyConInformation>* AttachedControllers = JoyConModule->Get().GetAttachedJoyCons();
    for (const FJoyConInformation JoyConInfo : *AttachedControllers) {
        if (!JoyConInfo.IsAttached) continue;
        JoyConModule->Get().DetachJoyCon(JoyConInfo.ControllerId);
    }
    TArray<FJoyConInformation>* ConnectedControllers = JoyConModule->Get().GetConnectedJoyCons();
    for (const FJoyConInformation JoyConInfo : *ConnectedControllers) {
        if (!JoyConInfo.IsConnected) continue;
        JoyConModule->Get().DisconnectJoyCon(JoyConInfo.ControllerId);
    }
}
// ...

Using Controller

// ...
if (JoyConModule != nullptr) {
    FRotator Rotation;
    int ControllerId = 0;
    if (JoyConModule->Get().GetJoyConVector(ControllerId , Rotation)) {
        SetActorRotation(Rotation);
    }
}
// ...

Other information

In the future I plan to add an example with the correct code and try to fix the problem with the editor.

Please do not confuse the Controller Index input with the Array Index output pin inside the for each as it does not contain the correct Device ID and will result in an error.