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"

C++ Newbie: Reference of custom ViewportClient class in custom HUD class

Hello there. I would like a reference of my custom UGameViewportClient class inside of my custom HUD class to access all the input of keystrokes for an input textfield.

I did the following...

In the SomHUD.h:

         //Get my Player Controller
         APlayerController* SomPC;
         //Get the Local Player
         ULocalPlayer* LocalPlayer;
         //Get the Vieport Client (for Key Input purposes)
         UGameViewportClient* SomViewportClient;

and in the SomHUD.cpp Constructer:

     //Get the PC
     SomPC = GetOwningPlayerController();
     //Get the Local Player
     ULocalPlayer* LocalPlayer = Cast<ULocalPlayer>(SomPC->Player);
     //Get the Viewport Client
     UGameViewportClient* SomViewportClient = LocalPlayer->ViewportClient;

Also I did set the custom viewport class as a default in the editor. Did I do something wrong? Because I seam to get only the Base Class of the viewport (UGameViewportClient) in my SomViewportClient Variable...

Thanks in advance

Product Version: Not Selected
more ▼

asked Jul 29 '14 at 10:53 PM in C++ Programming

avatar image

421 37 47 143

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

1 answer: sort voted first

You need to set your custom viewport class as default viewport class, you can do it in Edit->Project Settings->General Settings->Default Classes

more ▼

answered Jul 29 '14 at 11:50 PM

avatar image

35.8k 926 169 1104

avatar image Pseudocorpus Jul 30 '14 at 08:03 AM

Hey Shadowriver. Thanks for your answer. I did that already:

"Also I did set the custom viewport class as a default in the editor."

But I can't access the viewport class in code somehow...

avatar image Pseudocorpus Jul 30 '14 at 11:15 PM

Ok, let's say it this way: I have a class ASomHUD (deriving from AHUD) and a class USomGameViewportClient (deriving from UGameViewportClient).

How can I access the Function "InputChar" in my USomGameViewportClient-class from my ASomHUD-class?

avatar image Shadowriver Jul 30 '14 at 11:43 PM

You can't, because that function does not work like that, it a event function like BeginPlay or Tick, you need to override that function in USomGameViewportClient and it will be called on every key press, that function also return bool, true if you consume the event false if you ignored it. You will aslo need to place Super::InputChar(...) so parent classes can process event too, without it lot of input systems in engine won't work.

In other words you need to opposite way, call HUD from Viewport about key press or keep pressed keys in some varable which hud can use

avatar image Pseudocorpus Jul 31 '14 at 12:10 AM

Thanks again. But thats the thing. I did override the function and I am storing the Pressed key in a "PressedChar" String....

 bool USomGameViewportClient::InputChar(FViewport* InViewport, int32 ControllerId, TCHAR Character)
     // should probably just add actor to FString that takes a TCHAR
     FString CharacterString;
     CharacterString += Character;
     bool bResult = false;
     ULocalPlayer* const TargetPlayer = GEngine->GetLocalPlayerFromControllerId(this, ControllerId);
     if (TargetPlayer && TargetPlayer->PlayerController && TargetPlayer->PlayerController->IsA(ASomPlayerController::StaticClass()) &&
         Character >= 0x20 && Character < 0x100)
         bResult = Cast<ASomPlayerController>(TargetPlayer->PlayerController)->InputChar(ControllerId, CharacterString);
     if (!bResult && Character >= 0x20 && Character < 0x100)
         Super::InputChar(Viewport, ControllerId, Character);
     PressedChar = CharacterString;
     return bResult;

I now only need a way to access that "PressedChar"-Variable in my HUD-Class.

avatar image Shadowriver Jul 31 '14 at 01:51 AM

Ah ok :) then first expose PressedChar like this (we dont need to write from HUD so we make it read-only):

 FString PressedChar;

Now get GameClientViewport in blueprint cast it to your class and then drug it and drop it in empty space and you will get all possible connections, find "Cast to" your class (if you see it add BlueprintType specifier in UCLASS()) and then drug ourput form cast and find get "Get Pressed Char" :)

avatar image Pseudocorpus Jul 31 '14 at 09:35 AM

Blueprints can be a solution. However I was hoping to manage this "get something from another class" directly in code, because I am probably going to need that a lot more in different classes. I know it is possible and that it is fairly easy but I can't figure it out for toffee. :-/

Maybe I'll just contact Epic about this one. Thanks though!

avatar image Shadowriver Jul 31 '14 at 12:16 PM

Ah you want c++ thats not a problem you just cast it to your class


(comments are locked)
10|2000 characters needed characters left
Viewable by all users
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