Blueprint to C++ code


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.

Thanks in advance.

Might help…

.h file

// 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;


//Include the UserWidget header from engine
#include "Blueprint/UserWidget.h"

//Beginplay override
void MyClass::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
		//Show the Cursor.
		bShowMouseCursor = true;

To get keyevents use the below example

 if (!bCanTick)
 if (currentPlayerController != NULL)
     if (currentPlayerController->WasInputKeyJustPressed(EKeys::E))

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.

Keyevent should be inside Tick function, right?

‘CreateWidget’ : none of the 3 overloads could convert all the argument types

'AddToViewport' : is not a member of 'UClass'

I need to get game instance somehow.

In that case yes cause it checks if the key was just pressed. They are a few other ways if you google “key input unreal engine c++”

That comes from game statistics. If you want to create a game instance in C++, the following


#pragma once
//Include gameInstance engine header file
#include "Engine/GameInstance.h" 
#include "YourGameInstance.generated.h"
class YourGameGameInstance : public UGameInstance
     YourGameGameInstance(const FObjectInitializer& ObjectInitializer);
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category=GameInstanceExamples)
	int32 dummyPersistantValue; 


#include "YourGame.h"
#include "YourGameInstance.h"
YourGameGameInstance::YourGameInstance(const FObjectInitializer& ObjectInitializer)
	: Super(ObjectInitializer)

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

void YourGamesPlayerController::PlayerTick(float DeltaTime)

YourGameInstance* MyGI = Cast<USolusGameInstance>(GetGameInstance());


IF your trying to access it from a widget since you talked about widget try this

 static UGameInstance *UGameplayStatics::GetGameInstance(UObject* WorldContextObject)

This static function needs a world context object to get the game instance. you could try:

 UGameInstance* yourGI = UGameplayStatics::GetGameInstance(GetWorld());

Since UUserWidget has a GetWorld() method in it, the best answwer to m e seems:

 UGameInstance* yourGI = GetWorld()->GetGameInstance();

Instead of using

if (currentPlayerController->WasInputKeyJustPressed(EKeys::E))

I think you should use something like:

InputComponent->BindAction("<yourinputname>", IE_Pressed, this, &<yourobject>::<themethodyouwanttobecalled>);

in the class of your pawn, in the method void ATPSProjectCharacter::SetupPlayerInputComponent(class UInputComponent* InputComponent)

Alright, widget is working like a charm :slight_smile: Now, keyevent. I think binding will be more economic cause there’ll be probably hundreds of my-class-based objects in game.

InputComponent->BindAction(“InteractionKey”, IE_Pressed, this, &APointOfView_System::ObjectUsed);

cannot convert argument 4 from 'void (__cdecl APointOfView_System::* )(void)' to 'void (__cdecl ARFG1Character::* )(void)'