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"

Mouse click causes cursor to unlock from game window

I started a new project and copied in some of the blueprints from the Multiplayer Shootout example with the intention of converting it to C++. I did the C++ for the player controller first and when I replaced the blueprint with it there was a bug.

When I click a mouse button the cursor unlocks from the game window. I tried binding the mouse clicks to functions and they are not being called when I click. I assume I've done something wrong in setting up the HUD. Something to do with the input mode maybe, I can't figure it out. Here is the code:

MyPlayerController.h:

 #pragma once
 
 #include "CoreMinimal.h"
 #include "GameFramework/PlayerController.h"
 #include "Blueprint/UserWidget.h"
 #include "MyPlayerController.generated.h"
 
 /**
  *
  */
 UCLASS()
 class FACTION_API AMyPlayerController : public APlayerController {
     GENERATED_BODY()
 
 public:
     AMyPlayerController();
 
     virtual void SetupInputComponent() override;
 
     UFUNCTION(Client, Reliable, BlueprintCallable)
     void ClientPostLogin();
 
     UFUNCTION(BlueprintCallable)
     void HideInGameMenu();
 
     void SetupInGameUI();
     void ShowInGameMenu();
     void ToggleInGameMenu();
 
 protected:
     bool bIsInGameMenu = false;
 
 public:
     UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "UI")
     TSubclassOf<UUserWidget> HUDWidgetTemplate;
 
     UPROPERTY()
     UUserWidget* HUDWidgetInstance;
 
     UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "UI")
     TSubclassOf<UUserWidget> InGameMenuWidgetTemplate;
 
     UPROPERTY()
     UUserWidget* InGameMenuWidgetInstance;
 };
 

MyPlayerController.cpp:

 #include "MyPlayerController.h"
 
 AMyPlayerController::AMyPlayerController() {
     bShowMouseCursor = true;
 }
 
 void AMyPlayerController::SetupInputComponent() {
     Super::SetupInputComponent();
     
     InputComponent->BindAction("InGameMenu", IE_Pressed, this, &AMyPlayerController::ToggleInGameMenu);
 }
 
 void AMyPlayerController::ClientPostLogin_Implementation() {
     SetupInGameUI();
 }
 
 void AMyPlayerController::SetupInGameUI() {
     if (this->IsLocalPlayerController()) {
         if (HUDWidgetTemplate) {
             if (!HUDWidgetInstance) {
                 HUDWidgetInstance = CreateWidget(this, HUDWidgetTemplate);
             }
             if (!HUDWidgetInstance->GetIsVisible()) {
                 HUDWidgetInstance->AddToViewport();
             }
         }
 
         if (InGameMenuWidgetTemplate) {
             if (!InGameMenuWidgetInstance) {
                 InGameMenuWidgetInstance = CreateWidget(this, InGameMenuWidgetTemplate);
             }
         }
     }
 }
 
 void AMyPlayerController::ShowInGameMenu() {
     InGameMenuWidgetInstance->AddToViewport();
     FInputModeUIOnly MenuMode;
     MenuMode.SetWidgetToFocus(InGameMenuWidgetInstance->GetCachedWidget());
     SetInputMode(MenuMode);
 
     bIsInGameMenu = true;
 }
 
 void AMyPlayerController::HideInGameMenu() {
     InGameMenuWidgetInstance->RemoveFromParent();
     FInputModeGameOnly GameMode;
     SetInputMode(GameMode);
 
     bIsInGameMenu = false;
 }
 
 void AMyPlayerController::ToggleInGameMenu() {
     if (bIsInGameMenu)
         HideInGameMenu();
     else
         ShowInGameMenu();
 }
Product Version: UE 4.21
Tags:
more ▼

asked Mar 06 '19 at 01:13 PM in C++ Programming

avatar image

mellow-
1 2

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

0 answers: sort voted first
Be the first one to answer this question
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