Skip to content

Instantly share code, notes, and snippets.

@LordNed
Created October 18, 2017 03:45
Show Gist options
  • Save LordNed/afdc23c14bf730a600531fcc4cea6431 to your computer and use it in GitHub Desktop.
Save LordNed/afdc23c14bf730a600531fcc4cea6431 to your computer and use it in GitHub Desktop.
Unreal Engine 4 C++ UMG Crossover
// Fill out your copyright notice in the Description page of Project Settings.
#include "MEHUD.h"
void AMEHUD::BeginPlay()
{
ResourceDisplayWidget = CreateWidget<UUserWidget>(PlayerOwner, ResourceDisplayClass);
ResourceDisplayWidget->AddToViewport();
}
void AMEHUD::EndPlay(const EEndPlayReason::Type EndPlayReason)
{
if (ResourceDisplayWidget)
{
ResourceDisplayWidget->RemoveFromParent();
ResourceDisplayWidget = nullptr;
}
}
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/HUD.h"
#include "Blueprint/UserWidget.h"
#include "MEHUD.generated.h"
/**
*
*/
UCLASS()
class AMEHUD : public AHUD
{
GENERATED_BODY()
public:
virtual void BeginPlay() override;
virtual void EndPlay(const EEndPlayReason::Type EndPlayReason) override;
protected:
UPROPERTY(EditDefaultsOnly)
TSubclassOf<UUserWidget> ResourceDisplayClass;
UPROPERTY()
UUserWidget* ResourceDisplayWidget;
};
// Fill out your copyright notice in the Description page of Project Settings.
#include "ResourceStoreWidget.h"
#include "MEPlayerState.h"
#include "MEPlayerController.h"
#define LOCTEXT_NAMESPACE "MassExtractor"
FText UResourceStoreWidget::GetCurrentResourceText()
{
FResourceStore* ResourceStore = GetPlayerResourceStore();
if (!ResourceStore)
return LOCTEXT("NULL", "Null");
int CurrentStorage = FMath::RoundToInt(ResourceStore->StorageCurrent);
int CurrentCapacity = FMath::RoundToInt(ResourceStore->StorageCapacity);
FFormatNamedArguments Arguments;
Arguments.Add(TEXT("Current"), FText::AsNumber(CurrentStorage));
Arguments.Add(TEXT("Total"), FText::AsNumber(CurrentCapacity));
return FText::Format(LOCTEXT("ResourceCurrentOfTotal", "[{Current}/{Total}]"), Arguments);
}
FText UResourceStoreWidget::GetResourceProductionSum()
{
FResourceStore* ResourceStore = GetPlayerResourceStore();
if (!ResourceStore)
return LOCTEXT("NULL", "Null");
int Sum = FMath::RoundToInt(ResourceStore->Production - ResourceStore->Consumption);
FFormatNamedArguments Arguments;
Arguments.Add(TEXT("Sign"), Sum >= 0 ? LOCTEXT("MathSignPositive", "+") : LOCTEXT("MathSignNegative", "-"));
Arguments.Add(TEXT("Sum"), FText::AsNumber(FMath::Abs(Sum)));
return FText::Format(LOCTEXT("ResourceProductionSum", "{Sign}{Sum}"), Arguments);
}
FText UResourceStoreWidget::GetProductionText()
{
FResourceStore* ResourceStore = GetPlayerResourceStore();
if (!ResourceStore)
return LOCTEXT("NULL", "Null");
FFormatNamedArguments Arguments;
Arguments.Add(TEXT("Value"), FText::AsNumber(FMath::RoundToInt(ResourceStore->Production)));
return FText::Format(LOCTEXT("ResourceProductionText", "+{Value}"), Arguments);
}
FText UResourceStoreWidget::GetConsumptionText()
{
FResourceStore* ResourceStore = GetPlayerResourceStore();
if (!ResourceStore)
return LOCTEXT("NULL", "Null");
FFormatNamedArguments Arguments;
Arguments.Add(TEXT("Value"), FText::AsNumber(FMath::RoundToInt(ResourceStore->Consumption)));
return FText::Format(LOCTEXT("ResourceProductionText", "-{Value}"), Arguments);
}
FResourceStore* UResourceStoreWidget::GetPlayerResourceStore()
{
AMEPlayerState* PlayerState = Cast<AMEPlayerState>(GetOwningPlayer()->PlayerState);
if (!PlayerState)
return nullptr;
FResourceStore* ResourceStore = nullptr;
switch (Type)
{
case EResourceType::Mass:
ResourceStore = &PlayerState->MassResources;
break;
case EResourceType::Energy:
ResourceStore = &PlayerState->EnergyResources;
break;
}
return ResourceStore;
}
\
#undef LOCTEXT_NAMESPACE
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Blueprint/UserWidget.h"
#include "GameResources.h"
#include "ResourceStoreWidget.generated.h"
/**
*
*/
UCLASS()
class UResourceStoreWidget : public UUserWidget
{
GENERATED_BODY()
protected:
UFUNCTION(BlueprintCallable, BlueprintPure)
FText GetCurrentResourceText();
UFUNCTION(BlueprintCallable, BlueprintPure)
FText GetResourceProductionSum();
UFUNCTION(BlueprintCallable, BlueprintPure)
FText GetProductionText();
UFUNCTION(BlueprintCallable, BlueprintPure)
FText GetConsumptionText();
FResourceStore* GetPlayerResourceStore();
public:
UPROPERTY(EditAnywhere, BlueprintReadWrite)
EResourceType Type;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment