I’m trying to reproduce this blueprint into my C++ class, but i don’t have idea how to write “Create Widget” function and E Pressed event (input is enabled). I’ve already created some events and bCanCreate variable to make things work.
// Reference UMG Asset in the Editor
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Widgets")
TSubclassOf<class UUserWidget> pvSystemInfo;
// Variable to hold the widget After Creating it with a pointer
UUserWidget* PVSysInfo;
// Override BeginPlay()
virtual void BeginPlay() override;
.cpp
//Include the UserWidget header from engine
#include "Blueprint/UserWidget.h"
//Beginplay override
void MyClass::BeginPlay()
{
Super::BeginPlay();
if (PVSysInfo) // Check if the Asset is assigned in the blueprint.
{
// Create the widget and store it.
PVSystemInfo = CreateWidget<UUserWidget>(this, PVSysInfo);
// now you can use the widget directly since you have a referance for it.
// Extra check to make sure the pointer holds the widget.
if (PVSystemInfo)
{
//let add it to the view port
PVSystemInfo->AddToViewport();
}
//Show the Cursor.
bShowMouseCursor = true;
}
}
To get keyevents use the below example
if (!bCanTick)
return;
if (currentPlayerController != NULL)
{
if (currentPlayerController->WasInputKeyJustPressed(EKeys::E))
{
DoSomethingWhenPressed();
}
}
Don’t forget to accept an answer that best clears your question up or answers it so when the community finds your question in the future via search/google they know exactly what you did to fix it/get it going.
To just get the game instance, you could do something like the following in your player controller, since 4.4 all things inheriting from actor (which is well… basiaclly everything) has access to it
Alright, widget is working like a charm Now, keyevent. I think binding will be more economic cause there’ll be probably hundreds of my-class-based objects in game.