Skip to content

Instantly share code, notes, and snippets.

@p30virus
Last active August 31, 2024 21:10
Show Gist options
  • Select an option

  • Save p30virus/5e719bda816817b3aec65082ae608938 to your computer and use it in GitHub Desktop.

Select an option

Save p30virus/5e719bda816817b3aec65082ae608938 to your computer and use it in GitHub Desktop.
PublicDependencyModuleNames.AddRange(new string[] { "UMG", "Slate", "SlateCore" });
##############################
# MyPlayerController.cpp
##############################
// Fill out your copyright notice in the Description page of Project Settings.
#include "MyPlayerController.h"
#include "Blueprint/UserWidget.h"
// Sets default values
AMyPlayerController::AMyPlayerController()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = true;
}
// Called when the game starts or when spawned
void AMyPlayerController::BeginPlay()
{
Super::BeginPlay();
/* Create the widget and store the widget */
if (IsValid(MenuWidgetClass))
{
MenuWidget = CreateWidget<UUserWidget>(this, MenuWidgetClass->StaticClass());
}
}
void AMyPlayerController::ShowMenuWidget()
{
if ( MenuWidget != nullptr )
{
MenuWidget->AddToPlayerScreen();
}
}
void AMyPlayerController::HideMenuWidget()
{
if ( MenuWidget != nullptr )
{
MenuWidget->RemoveFromParent();
}
}
// Called every frame
void AMyPlayerController::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
##############################
# MyPlayerController.h
##############################
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/PlayerController.h"
#include "MyPlayerController.generated.h"
class UUserWidget;
UCLASS()
class EXAMPLE_API AMyPlayerController : public APlayerController
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AMyPlayerController();
protected:
/* You need to assign the widget on this variable */
UPROPERTY(EditDefaultsOnly, Category="Menu")
TSubclassOf<UUserWidget> MenuWidgetClass;
UPROPERTY()
TObjectPtr<UUserWidget> MenuWidget;
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
UFUNCTION(BlueprintCallable)
void ShowMenuWidget();
UFUNCTION(BlueprintCallable)
void HideMenuWidget();
// Called every frame
virtual void Tick(float DeltaTime) override;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment