虚幻引擎5 GAS开发俯视角RPG游戏 P06-28 构建属性菜单小部件控制器

1.创建属性菜单小部件控制器

Source/CC_Aura/Public/UI/WidgetController/CC_AttributeMenuWidgetController.h:

cpp 复制代码
// 版权归陈超所有

#pragma once

#include "CoreMinimal.h"
#include "UI/WidgetController/CC_WidgetController.h"
#include "CC_AttributeMenuWidgetController.generated.h"

/**
 * 
 */
UCLASS(Blueprintable)
class CC_AURA_API UCC_AttributeMenuWidgetController : public UCC_WidgetController
{
	GENERATED_BODY()

public:
	//广播初始值
	virtual void BroadcastInitialValues() override;

	//依赖的绑定回调函数
	virtual void BindCallbacksToDependencies() override;
	
	
};

Source/CC_Aura/Private/UI/WidgetController/CC_AttributeMenuWidgetController.cpp:

cpp 复制代码
// 版权归陈超所有


#include "UI/WidgetController/CC_AttributeMenuWidgetController.h"

void UCC_AttributeMenuWidgetController::BroadcastInitialValues()
{
	
}

void UCC_AttributeMenuWidgetController::BindCallbacksToDependencies()
{
	
}

2.构建蓝图函数库:

添加静态函数,能随地得到小部件的控制器

Source/CC_Aura/Public/BlueprintFunctionLibrary/CC_BlueprintFunctionLibrary.h

cpp 复制代码
// 版权归陈超所有

#pragma once

#include "CoreMinimal.h"
#include "Kismet/BlueprintFunctionLibrary.h"
#include "CC_BlueprintFunctionLibrary.generated.h"

class UCC_AttributeMenuWidgetController;
class UCC_OverlayWidgetController;
/**
 * 
 */
UCLASS()
class CC_AURA_API UCC_BlueprintFunctionLibrary : public UBlueprintFunctionLibrary
{
	GENERATED_BODY()

public:
	UFUNCTION(BlueprintPure, Category="CC_BlueprintFunctionLibrary|OverlayWidgetController")
	static UCC_OverlayWidgetController* GetOverlayWidgetController(const UObject* WorldContextObject);
	
	UFUNCTION(BlueprintPure, Category="CC_BlueprintFunctionLibrary|AttributeMenuWidgetController")
	static UCC_AttributeMenuWidgetController* GetAttributeMenuWidgetController(const UObject* WorldContextObject);
	
};

Source/CC_Aura/Private/BlueprintFunctionLibrary/CC_BlueprintFunctionLibrary.cpp:

cpp 复制代码
// 版权归陈超所有


#include "BlueprintFunctionLibrary/CC_BlueprintFunctionLibrary.h"

#include "Kismet/GameplayStatics.h"
#include "Player/CC_PlayerState.h"
#include "UI/HUD/CC_HUD.h"
#include "UI/WidgetController/CC_WidgetController.h"

UCC_OverlayWidgetController* UCC_BlueprintFunctionLibrary::GetOverlayWidgetController(const UObject* WorldContextObject)
{
	APlayerController* PlayerController = UGameplayStatics::GetPlayerController(WorldContextObject, 0);
	if (!PlayerController) return nullptr;

	ACC_HUD* CC_HUD = Cast<ACC_HUD>(PlayerController->GetHUD());
	if (!CC_HUD) return nullptr;
	
	ACC_PlayerState* PlayerState = PlayerController->GetPlayerState<ACC_PlayerState>();
	if (!PlayerState) return nullptr;

	UAbilitySystemComponent* AbilitySystemComponent = PlayerState->GetAbilitySystemComponent();
	if (!AbilitySystemComponent) return nullptr;

	UAttributeSet* AttributeSet = PlayerState->GetAttributeSet();
	if (!AttributeSet) return nullptr;
	
	FWidgetControllerParams WidgetControllerParams(PlayerController, PlayerState, AbilitySystemComponent, AttributeSet);
	
	return CC_HUD->GetOverlayWidgetController(WidgetControllerParams);
}

UCC_AttributeMenuWidgetController* UCC_BlueprintFunctionLibrary::GetAttributeMenuWidgetController(const UObject* WorldContextObject)
{
	APlayerController* PlayerController = UGameplayStatics::GetPlayerController(WorldContextObject, 0);
	if (!PlayerController) return nullptr;

	ACC_HUD* CC_HUD = Cast<ACC_HUD>(PlayerController->GetHUD());
	if (!CC_HUD) return nullptr;
	
	ACC_PlayerState* PlayerState = PlayerController->GetPlayerState<ACC_PlayerState>();
	if (!PlayerState) return nullptr;

	UAbilitySystemComponent* AbilitySystemComponent = PlayerState->GetAbilitySystemComponent();
	if (!AbilitySystemComponent) return nullptr;

	UAttributeSet* AttributeSet = PlayerState->GetAttributeSet();
	if (!AttributeSet) return nullptr;
	
	FWidgetControllerParams WidgetControllerParams(PlayerController, PlayerState, AbilitySystemComponent, AttributeSet);
	
	return CC_HUD->GetAttributeMenuWidgetController(WidgetControllerParams);
}

其中,在CC_HUD类中,添加属性菜单控制器,以及获取方法:

Source/CC_Aura/Public/UI/HUD/CC_HUD.h:

cpp 复制代码
	UPROPERTY()
	TObjectPtr<UCC_AttributeMenuWidgetController> AttributeMenuWidgetController;	
	
	UPROPERTY(EditAnywhere)
	TSubclassOf<UCC_AttributeMenuWidgetController> AttributeMenuWidgetControllerClass;
	
cpp 复制代码
UCC_AttributeMenuWidgetController* GetAttributeMenuWidgetController(const FWidgetControllerParams& FWCParams);

Source/CC_Aura/Private/UI/HUD/CC_HUD.cpp:

cpp 复制代码
UCC_AttributeMenuWidgetController* ACC_HUD::GetAttributeMenuWidgetController(const FWidgetControllerParams& FWCParams)
{
	if (AttributeMenuWidgetController == nullptr)	
	{
		AttributeMenuWidgetController = NewObject<UCC_AttributeMenuWidgetController>(this, AttributeMenuWidgetControllerClass);		
		AttributeMenuWidgetController->SetWidgetControllerParams(FWCParams);		
	}
	return AttributeMenuWidgetController;
}

3.创建属性菜单小部件控制器蓝图BP_AttributeMenuWidgetController:

在HUD中配置:

4.在WBP_AttributeMenu中添加控制器:

验证:

在回调函数中,打印控制器:

效果:

相关推荐
卷无止境1 天前
C++ 的Eigen 库全解析
c++
卷无止境1 天前
现代 C++特性大盘点:一门脱胎换骨的老语言
c++·后端
郝学胜_神的一滴2 天前
CMake 27:缓存变量的特性、语法、类型与实操全解
c++·cmake
博客18003 天前
酷宝的使用方法,超好用的免费界面库,C++、MFC可用
c++·mfc·界面库·库来帮·酷宝
郝学胜_神的一滴3 天前
CMake 026:属性体系精讲、四大作用域全解 & 实战代码落地
c++·cmake
nujnewnehc4 天前
不会 py, 用 ai 写了个游戏辅助的感受
人工智能·游戏
众少成多积小致巨4 天前
JNI (Java Native Interface) 技术手册中文参考指南
android·java·c++
jump_jump4 天前
为了重玩金庸群侠传,我研究了一下 Ruffle 怎么复活 Flash
游戏·rust·github
XIAOHEZIcode6 天前
Linux系统鼠标偏移常见原因以及修复方案
linux·运维·游戏
Aloys_Code6 天前
逆向一个被遗忘的DVD游戏格式:从DES加密到Rust模拟器
游戏·模拟器·retroarch·复古游戏·native32·sunplus·赤刃·钢铁风暴