不得不说最近的事真的多,我也不给写的短找理由了,我就说下我是九点多才开始写的而且我这人十一点过后就困得不行
1 实现思路设计
UI显示部分,由于这个界面是显示角色属性信息的,一种比较简单的方法就是直接获取文件里的信息,只在显示界面的时候获取一次信息存到UI里,升级的时候修改UI里FSaveAttributes,然后再存到AS和文件里,这么做有一个隐患,就是CurrentValue有风险,但是Demo里的升级行为只能在基地里,不会涉及到战斗行为,所以总结一下今天需要写的大致流程:
1.新建一个UserWidget的子类,定义一个FSavedAttributes变量,建一个对应蓝图,做好UI界面
2.玩家类中加一个按键绑定打开强化界面,设置可视性的同时读取信息到FSavedAttributes里,同时用这个值绑定界面里的元素,到这就能实现信息的显示了(根据需求就确定了这里的数据不用实时读取)
3.升级就是修改这个临时的FSavedAttributes,先保存,再用上一篇写好的UMyPlayerAttributeSet::InitPlayerAttributeSet更新AS中的值
2 实现过程
2.1 UUserWidget_StrengthenPad类
新建一个UUserWidget_StrengthenPad类,实现下边这几个函数,具体的作用看注释
UPROPERTY(EditAnywhere,BlueprintReadWrite)
FSavedAttributes TempSavedAttributes;//记录读取的属性值
AMyPlayer* MyPlayer;//记录玩家的指针
UFUNCTION(BlueprintCallable)
void GetSavedAttributesData();//读取文件中信息
UFUNCTION(BlueprintCallable)
void StrengthenByName(FString AttributeName,float value);//强化指定参数,幅值为value
UFUNCTION(BlueprintCallable)
float GetAttributeByName(FString AttributeName);//获取信息接口
UFUNCTION(BlueprintImplementableEvent)
void UpdatePadInfo();//手动更新信息节省资源,避免Tick绑定
UFUNCTION(BlueprintCallable)
void SaveToSlot();//将信息保存到文件的接口
具体的实现贴在这里了,逻辑都很简单,因为是测试只写了一个参数的:
void UUserWidget_StrengthenPad::GetSavedAttributesData()
{
UMyPlayerData* LoadedPlayerData= Cast<UMyPlayerData>(UGameplayStatics::LoadGameFromSlot(FString("1"), 0));
TempSavedAttributes=LoadedPlayerData->GetSavedAttributes();
}
void UUserWidget_StrengthenPad::StrengthenByName(FString AttributeName,float value)
{
if(AttributeName.Equals(FString("BaseAttack")))
{
TempSavedAttributes.BaseAttack=TempSavedAttributes.BaseAttack+value;
UE_LOG(LogTemp,Warning,TEXT("UUserWidget_StrengthenPad::StrengthenByName-->BaseAttack==%f"),TempSavedAttributes.BaseAttack);
}
else
{
UE_LOG(LogTemp,Warning,TEXT("UUserWidget_StrengthenPad::StrengthenByName-->其他参数"));
}
MyPlayer=Cast<AMyPlayer>(UGameplayStatics::GetPlayerController(GetWorld(),0)->GetPawn());
if(MyPlayer!=nullptr)
{
Cast<UMyPlayerAttributeSet>(MyPlayer->GetAttributeSet())->InitPlayerAttributeSet(TempSavedAttributes);
}
else
{
UE_LOG(LogTemp,Warning,TEXT("UUserWidget_StrengthenPad::StrengthenByName-->MyPlayer is NULL"));
}
//测试先在这更新
UpdatePadInfo();
}
float UUserWidget_StrengthenPad::GetAttributeByName(FString AttributeName)
{
if(AttributeName.Equals(FString("BaseAttack")))
{
return TempSavedAttributes.BaseAttack;
}
return TempSavedAttributes.BaseAttack;
}
void UUserWidget_StrengthenPad::SaveToSlot()
{
UMyPlayerData* LoadedPlayerData = Cast<UMyPlayerData>(UGameplayStatics::CreateSaveGameObject(UMyPlayerData::StaticClass()));
LoadedPlayerData->SetSavedAttributes(TempSavedAttributes);
UGameplayStatics::SaveGameToSlot(LoadedPlayerData, FString("1"), 0);
}
2.2 创建蓝图并做好测试用的UI元素
UI结构,其实测试阶段怎么弄都行,能显示信息就行
蓝图内实现刚才声明的蓝图实现事件,手动更新信息
人物蓝图内DebugKey后边接这三个函数:
实际测试一下,数值也是对的,就是记得实际应用的时候要在合适的时机先更新好数据
同时我又加了一句输出看下AS里的值对不对: