Blueprint to C++ code

Hello!

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;

.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.

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

.h

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

.cpp

#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)
{
Super::PlayerTick(DeltaTime);

YourGameInstance* MyGI = Cast<USolusGameInstance>(GetGameInstance());
if(MyGI)
{ 
	MyGI->dummyPersistantValue++;
	ClientMessage(FString::FromInt(MyGI->dummyPersistantValue));
}

}

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))
{
      DoSomethingWhenPressed();
}

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)'