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
//Get the Local Player
//Get the Vieport Client (for Key Input purposes)
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
You need to set your custom viewport class as default viewport class, you can do it in Edit->Project Settings->General Settings->Default Classes
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…
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?
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
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
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;
I now only need a way to access that “PressedChar”-Variable in my HUD-Class.
Ah ok then first expose PressedChar like this (we dont need to write from HUD so we make it read-only):
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”
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!
Ah you want c++ thats not a problem you just cast it to your class