虚幻引擎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中添加控制器:

验证:

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

效果:

相关推荐
在路上看风景6 小时前
19. 成员初始化列表和初始化对象
c++
zmzb01036 小时前
C++课后习题训练记录Day98
开发语言·c++
念风零壹7 小时前
C++ 内存避坑指南:如何用移动语义和智能指针解决“深拷贝”与“内存泄漏”
c++
孞㐑¥7 小时前
算法——BFS
开发语言·c++·经验分享·笔记·算法
_风华ts9 小时前
创建并使用AimOffset
ue5·动画·虚幻·虚幻引擎·aimoffset
子春一9 小时前
Flutter for OpenHarmony:构建一个 Flutter 四色猜谜游戏,深入解析密码逻辑、反馈算法与经典益智游戏重构
算法·flutter·游戏
MZ_ZXD0019 小时前
springboot旅游信息管理系统-计算机毕业设计源码21675
java·c++·vue.js·spring boot·python·django·php
前端不太难9 小时前
HarmonyOS 游戏上线前必做的 7 类极端场景测试
游戏·状态模式·harmonyos
A星空12310 小时前
一、Linux嵌入式的I2C驱动开发
linux·c++·驱动开发·i2c
凡人叶枫10 小时前
C++中智能指针详解(Linux实战版)| 彻底解决内存泄漏,新手也能吃透
java·linux·c语言·开发语言·c++·嵌入式开发