x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Gamepad Connection Events

What is the recommended way to detect gamepad events? I'm thinking of the following scenarios:

  • Gamepad is connected and gets disconnected

  • Gamepad is disconnected and gets connected

Also, battery level would be nice. I'm interested in Windows only.

Product Version: UE 4.9
Tags:
more ▼

asked Oct 07 '15 at 07:38 PM in C++ Programming

avatar image

hyperdr1ve
563 26 49 116

(comments are locked)
10|2000 characters needed characters left

4 answers: sort voted first

I did find a way to poll whether an Xbox controller is connected, although it may make your eyes bleed with terrible programming practice... (this needs to be implemented in C++)

At the top of your CPP file, add:

 #if PLATFORM_WINDOWS
     #define private public
     #include "Runtime/Core/Public/Windows/WindowsApplication.h"
     #include "Runtime/Core/Private/Windows/XInputInterface.h"
     #undef private
 #endif

Then implement a function to return whether an Xbox gamepads are connected:

 bool XboxControllerConnected()
 {
     #if PLATFORM_WINDOWS
         if (FSlateApplication::IsInitialized() && FSlateApplication::Get().GetPlatformApplication().IsValid())
         {
             TSharedPtr<FWindowsApplication> app = StaticCastSharedPtr<FWindowsApplication>(FSlateApplication::Get().GetPlatformApplication());
             if (app.IsValid())
             {
                 TSharedPtr<XInputInterface> x = app->XInput;
                 if (x.IsValid())
                 {
                     for (int i = 0; i < MAX_NUM_XINPUT_CONTROLLERS; i++)
                     {
                         if (x->ControllerStates[i].bIsConnected)
                             return true;
                     }
                 }
             }
         }
     #endif
         return false;
 }
 

Huge disclaimer here though: This is not a good/sustainable/safe approach to solving this problem. Use at your own risk, and know that future updates of the engine could easily break this.

more ▼

answered Oct 23 '15 at 04:11 PM

avatar image

HazzaBui
71 1 2 10

avatar image hyperdr1ve Oct 23 '15 at 04:18 PM

Lol, yes my eyes are bleeding, but at least it's something that works for now.

avatar image hvs_chrisgdaniel Feb 08 '16 at 08:47 PM

This solution worked for me, but I opted for the engine change at the link below because it relies less on the dark arts.

https://answers.unrealengine.com/questions/142358/question-is-there-a-way-to-detect-a-gamepad.html

avatar image HazzaBui Feb 09 '16 at 04:18 PM

Thanks for the update - that's definitely a better solution if you are willing to compile your own engine

(comments are locked)
10|2000 characters needed characters left

I've looked through the source and can say that events for gamepads connecting/disconnecting do not exist. XInput has an XInputGetBatteryInformation call that isn't plumbed. There's also no way to query the number of detected gamepads. There's quite a lot missing here.

more ▼

answered Oct 14 '15 at 06:49 PM

avatar image

hyperdr1ve
563 26 49 116

(comments are locked)
10|2000 characters needed characters left

Here is possible solution, only tested on windows so far.

File - New C++ Class Choose parent class - Blueprint Function Library, hit next Give it a name (for example GamepadDetection)

GamepadDetection.h

 #pragma once
 
 #include "CoreMinimal.h"
 #include "Kismet/BlueprintFunctionLibrary.h"
 #include "GamepadDetection.generated.h"
 
 /**
  * 
  */
 UCLASS()
 class PROJECTNAME_API UGamepadDetection : public UBlueprintFunctionLibrary
 {
     GENERATED_BODY()
     
 public:
     /* Check if gamepad is connected at Runtime. */
     UFUNCTION(BlueprintCallable, meta = (DisplayName = "GamepadConnected"), Category = "System Information")
         static bool IsGamePadConnected();
     
     
 };


GamepadDetection.cpp

 #include "GamepadDetection.h"
 #include "SlateBasics.h"
 
 bool UGamepadDetection::IsGamePadConnected()
 {
     auto genericApplication = FSlateApplication::Get().GetPlatformApplication();
     if (genericApplication.Get() != nullptr && genericApplication->IsGamepadAttached())
     {
         return true;
     }
     return false;
 }
 

Then finally uncommment/enable slate module depenecies within your projectname.Build.cs and reboot you editor before hitting compile.

YourProjectName.Build.cs

 // Uncomment if you are using Slate UI
 PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore" });

Now you have a node inside blueprint to check if the gamepad is connected,

more ▼

answered Sep 13 '17 at 01:46 PM

avatar image

RhinoCode
121 1 4 4

(comments are locked)
10|2000 characters needed characters left

If you connect to the FCoreDelegates::OnControllerConnectionChange delegate then you can be notified whenever a gamepad is connected/disconnected. Just make a function with the following signature:

void OnControllerConnectionChanged(bool bIsConnected, FPlatformUserId UserID, int32 UserIndex);

more ▼

answered Nov 06 '18 at 07:03 PM

avatar image

Justin.Grant
1 2 2

(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question