박하의 나날

[12]Battery Collector_UMG 활성화

프로그래밍/Unreal

 

 

16.Enabling UMG 

UMG: 언리얼 모션 그래픽

BatteryCollector.Build.cs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Copyright 1998-2015 Epic Games, Inc. All Rights Reserved.
 
using UnrealBuildTool;
 
public class BatteryCollector : ModuleRules
{
    public BatteryCollector(TargetInfo Target)
    {
        PublicDependencyModuleNames.AddRange(new string[] { "Core""CoreUObject""Engine""InputCore""UMG"});
        //
        PrivateDependencyModuleNames.AddRange(new string[] { "Slate""SlateCore" });
    }
}
 
cs

9)프로젝트 소스의 퍼블릭형이 필요로 하는 모듈은 PublicDependency에 추가

UMG 추가

11) UMG에 필요한 모듈 Slate, SlateCore

 

BatteryCollector.h

1
2
3
4
5
6
7
8
9
// Copyright 1998-2015 Epic Games, Inc. All Rights Reserved.
 
#ifndef __BATTERYCOLLECTOR_H__
#define __BATTERYCOLLECTOR_H__
 
#include "Engine.h"
 
#endif
 
cs

6) Engineminimal.h -> Engine.h

BatteryCollectorGameMode.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Copyright 1998-2015 Epic Games, Inc. All Rights Reserved.
#pragma once
#include "GameFramework/GameMode.h"
#include "BatteryCollectorGameMode.generated.h"
 
UCLASS(minimalapi)
class ABatteryCollectorGameMode : public AGameMode
{
    GENERATED_BODY()
 
public:
    ABatteryCollectorGameMode();
 
    virtual void Tick(float DeltaTime) override;
    //Returns power needed to win - needed for the HUD
    UFUNCTION(BlueprintPure, Category = "Power")
    float GetPowerToWin() const;
 
    virtual void BeginPlay() override;
 
protected:
    /**The rate at which the character loses power */
    UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Power", Meta = (BlueprintProtected = "true"))
    float DecayRate;
 
    /**The power needed to win the game*/
    UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Power", Meta = (BlueprintProtected = "true"))
    float PowerToWin;
 
    /**The widget class to use for our HUD screen*/
    UPROPERTY(EditDefaultsOnly, BlueprintReadWrite, Category = "Power", Meta = (BlueprintProtected = "true"))
    TSubclassOf<class UUserWidget> HUDWidgetClass;
 
    //The instance of the HUD
    UPROPERTY()
    class UUserWidget* CurrentWidget;
};
cs

17)hud에서 값을 받을 수 있도록 powertowin값이 얼마인지 반환해주는 퍼블릭 함수.
 hud는 다른클래스에 기반할것. hud는 이 함수를 호출해서 승리할때까지 필요한 powertowin값을 알게된다.
 값에 대한 변경이 없이 받아오기만 하므로 const.

37)hudwidget클래스의 복사본, 이것을 스폰하면 currentwidget과 직접 상호작용할 수 있게된다

 

BatteryCollectorGameMode.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include "BatteryCollector.h"
#include "BatteryCollectorGameMode.h"
#include "BatteryCollectorCharacter.h"
#include "Kismet/GameplayStatics.h"
#include "Blueprint/UserWidget.h"
void ABatteryCollectorGameMode::BeginPlay(){
    Super::BeginPlay();
 
    //set the score to beat
    ABatteryCollectorCharacter* MyCharacter = Cast<ABatteryCollectorCharacter>(UGameplayStatics::GetPlayerPawn(this0));
    if (MyCharacter){
        PowerToWin = (MyCharacter->GetInitialPower())*1.25f;
    }
 
    if (HUDWidgetClass != nullptr) 
    {
        CurrentWidget = CreateWidget<UUserWidget>(GetWorld(), HUDWidgetClass);
        if (CurrentWidget != nullptr){
            CurrentWidget->AddToViewport();
        }
    }
}
 
cs

15~)클래스변수에 뭔가 설정되어있는지 확인

UserWidget에서 걸리면 비주얼스튜디오 끄고 ue4->file->refresh vs 하고 다시 켜보기.