Gameplay标记
UE5 GameplayTag流量优化机制分析
c++
xxx.Build.cs
"GameplayTags"
.h
protected:
/// <summary>
/// 所有用户权限
/// </summary>
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Vince|Permission")
TMap <FString, FGameplayTagContainer>UserPrivileges;
private:
/// <summary>
/// 当前用户
/// </summary>
FString CurrentUser = TEXT("");
public:
UFUNCTION(BlueprintCallable, Category = "Vince|Permission")
/// <summary>
/// 设置当前用户
/// </summary>
/// <param name="UserName">用户名</param>
void SetCurrentUser(FString UserName);
UFUNCTION(BlueprintPure, Category = "Vince|Permission")
/// <summary>
/// 获取当前用户
/// </summary>
FString GetUserName();
UFUNCTION(BlueprintPure, Category = "Vince|Permission")
/// <summary>
/// 获取所有用户
/// </summary>
TArray<FString> GetAllUserName();
UFUNCTION(BlueprintCallable, Category = "Vince|Permission")
/// <summary>
/// 获取用户权限
/// </summary>
/// <param name="UserPrivilege">权限标签</param>
/// <param name="UserName">用户名</param>
/// <param name="bCurrentUser">是否查询的是当前用户</param>
void GetUserPrivilege(FGameplayTagContainer& UserPrivilege, FString UserName, bool bCurrentUser = true);
UFUNCTION(BlueprintCallable, Category = "Vince|Permission")
/// <summary>
/// 检查用户权限
/// </summary>
/// <param name="UserPrivilege">权限标签</param>
/// <param name="UserName">用户名</param>
/// <param name="bCurrentUser">是否查询的是当前用户</param>
/// <returns></returns>
bool CheckUserPrivilege(FGameplayTag UserPrivilege, FString UserName, bool bCurrentUser = true);
UFUNCTION(BlueprintCallable, Category = "Vince|Permission")
/// <summary>
/// 添加用户权限单一标签
/// </summary>
/// <param name="UserPrivilege">权限标签</param>
/// <param name="UserName">用户名</param>
/// <param name="bCurrentUser">是否查询的是当前用户</param>
/// <returns></returns>
bool AddOnlyUserPrivilege(FGameplayTag UserPrivilege, FString UserName, bool bCurrentUser = true);
UFUNCTION(BlueprintCallable, Category = "Vince|Permission")
/// <summary>
/// 移除用户权限单一标签
/// </summary>
/// <param name="UserPrivilege">权限标签</param>
/// <param name="UserName">用户名</param>
/// <param name="bCurrentUser">是否查询的是当前用户</param>
/// <returns></returns>
bool RemoveOnlyUserPrivilege(FGameplayTag UserPrivilege, FString UserName, bool bCurrentUser = true);
.cpp
void UPieceTogetherTools_GameInstance::SetCurrentUser(FString UserName)
{
if (!UserName.IsEmpty())
{
CurrentUser = UserName;
}
}
FString UPieceTogetherTools_GameInstance::GetUserName()
{
return CurrentUser;
}
TArray<FString> UPieceTogetherTools_GameInstance::GetAllUserName()
{
TArray<FString> nowTArrayKeys;
UserPrivileges.GenerateKeyArray(nowTArrayKeys);
return nowTArrayKeys;
}
void UPieceTogetherTools_GameInstance::GetUserPrivilege(FGameplayTagContainer& UserPrivilege, FString UserName, bool bCurrentUser /*= true*/)
{
if (bCurrentUser)
{
if (!CurrentUser.IsEmpty())
{
UserPrivilege = *(UserPrivileges.Find(CurrentUser));
}
}
else
{
if (!UserName.IsEmpty())
{
UserPrivilege = *(UserPrivileges.Find(UserName));
}
}
}
bool UPieceTogetherTools_GameInstance::CheckUserPrivilege(FGameplayTag UserPrivilege, FString UserName, bool bCurrentUser /*= true*/)
{
if (bCurrentUser)
{
if (!CurrentUser.IsEmpty())
{
FGameplayTagContainer& nowTagContainer = *(UserPrivileges.Find(CurrentUser));
return nowTagContainer.HasTag(UserPrivilege);
}
}
else
{
if (!UserName.IsEmpty())
{
FGameplayTagContainer& nowTagContainer = *(UserPrivileges.Find(UserName));
return nowTagContainer.HasTag(UserPrivilege);
}
}
return false;
}
bool UPieceTogetherTools_GameInstance::AddOnlyUserPrivilege(FGameplayTag UserPrivilege, FString UserName, bool bCurrentUser)
{
if (bCurrentUser)
{
if (!CurrentUser.IsEmpty())
{
FGameplayTagContainer& nowTagContainer = UserPrivileges.FindOrAdd(CurrentUser);
nowTagContainer.AddTag(UserPrivilege);
return true;
}
}
else
{
if (!UserName.IsEmpty())
{
FGameplayTagContainer& nowTagContainer = UserPrivileges.FindOrAdd(UserName);
nowTagContainer.AddTag(UserPrivilege);
return true;
}
}
return false;
}
bool UPieceTogetherTools_GameInstance::RemoveOnlyUserPrivilege(FGameplayTag UserPrivilege, FString UserName, bool bCurrentUser /*= true*/)
{
if (bCurrentUser)
{
if (!CurrentUser.IsEmpty())
{
FGameplayTagContainer& nowTagContainer = UserPrivileges.FindOrAdd(CurrentUser);
nowTagContainer.RemoveTag(UserPrivilege);
return true;
}
}
else
{
if (!UserName.IsEmpty())
{
FGameplayTagContainer& nowTagContainer = UserPrivileges.FindOrAdd(UserName);
nowTagContainer.RemoveTag(UserPrivilege);
return true;
}
}
return false;
}
![](https://i-blog.csdnimg.cn/blog_migrate/e5528974cf9a76ec82be60c374157602.png)
![](https://i-blog.csdnimg.cn/blog_migrate/bf2c7df331cf84d58e1f5173b5014ec3.png)
INI
![](https://i-blog.csdnimg.cn/blog_migrate/924fe40cbc7e3167a5136a6170014f54.png)
[/Script/GameplayTags.RestrictedGameplayTagsList]
RestrictedGameplayTagList=(bAllowNonRestrictedChildren=False,Tag="Feature.A",DevComment="功能A")
RestrictedGameplayTagList=(bAllowNonRestrictedChildren=False,Tag="Feature.B",DevComment="功能B")
RestrictedGameplayTagList=(bAllowNonRestrictedChildren=False,Tag="Login",DevComment="")
RestrictedGameplayTagList=(bAllowNonRestrictedChildren=False,Tag="Feature.C",DevComment="功能C")
![](https://i-blog.csdnimg.cn/blog_migrate/8f29e28220e169492762837c68dc9d23.png)