UE5 C++ Gas开发 学习记录(三)

添加AuraPlayerState,AuraAbilitySystemComponentBase和AuraAttributeSet

在Build.cs里添加

// Copyright Epic Games, Inc. All Rights Reserved. using UnrealBuildTool; public class MyGas : ModuleRules { public MyGas(ReadOnlyTargetRules Target) : base(Target) { PCHUsage = PCHUsageMode.UseExplicitOrSharedPCHs; PublicDependencyModuleNames.AddRange(new string[] { "Core", "CoreUObject", "Engine", "InputCore","EnhancedInput" , "GameplayAbilities" }); PrivateDependencyModuleNames.AddRange(new string[] { "GameplayAbilities","GameplayTags","GameplayTasks" }); // Uncomment if you are using Slate UI // PrivateDependencyModuleNames.AddRange(new string[] { "Slate", "SlateCore" }); // Uncomment if you are using online features // PrivateDependencyModuleNames.Add("OnlineSubsystem"); // To include OnlineSubsystemSteam, add it to the plugins section in your uproject file with the Enabled attribute set to true } }

AuraPlayerState.h

// Fill out your copyright notice in the Description page of Project Settings. #pragma once #include "CoreMinimal.h" #include "AbilitySystemComponent.h" #include "AbilitySystemInterface.h" #include "GameFramework/PlayerState.h" #include "AuraPlayerState.generated.h" class UAbilitySystemComponent; class UAttributeSet; /** * */ UCLASS() class MYGAS_API AAuraPlayerState : public APlayerState , public IAbilitySystemInterface { GENERATED_BODY() public: AAuraPlayerState(); virtual UAbilitySystemComponent* GetAbilitySystemComponent() const override; UAttributeSet* GetAttributeSet() const { return AttributesSet; } protected: UPROPERTY() TObjectPtr<UAbilitySystemComponent> AbilitySystemComponent; UPROPERTY() TObjectPtr<UAttributeSet> AttributesSet; };

AuraPlayerState.cpp

// Fill out your copyright notice in the Description page of Project Settings. #include "PlayerState/AuraPlayerState.h" #include "AbilitySystem/AuraAbilitySystemComponentBase.h" #include "AbilitySystem/AuraAttributeSet.h" AAuraPlayerState::AAuraPlayerState() { AbilitySystemComponent = CreateDefaultSubobject<UAuraAbilitySystemComponentBase>("AbilitySystemComponent"); AbilitySystemComponent->SetIsReplicated(true); AttributesSet = CreateDefaultSubobject<UAuraAttributeSet>("AttributesSet"); //设置Player的网络同步 AbilitySystemComponent->SetReplicationMode(EGameplayEffectReplicationMode::Mixed); //复制这个Actor用来同步给客户端 NetUpdateFrequency = 100.f; } UAbilitySystemComponent* AAuraPlayerState::GetAbilitySystemComponent() const { return AbilitySystemComponent; }

修改Enemy.cpp

// Fill out your copyright notice in the Description page of Project Settings. #include "Character/AuraEnemy.h" #include "DrawDebugHelpers.h" #include "AbilitySystem/AuraAbilitySystemComponentBase.h" #include "AbilitySystem/AuraAttributeSet.h" #include "Components/SkeletalMeshComponent.h" #include "MyGas/MyGas.h" AAuraEnemy::AAuraEnemy() { GetMesh()->SetCollisionResponseToChannel(ECC_Visibility,ECR_Block); AbilitySystemComponent = CreateDefaultSubobject<UAuraAbilitySystemComponentBase>("AbilitySystemComponent"); AbilitySystemComponent->SetIsReplicated(true); AttributesSet = CreateDefaultSubobject<UAuraAttributeSet>("AttributesSet"); AbilitySystemComponent->SetReplicationMode(EGameplayEffectReplicationMode::Minimal ); } void AAuraEnemy::HighlightActor() { UE_LOG(LogTemp, Error, TEXT("HighlightActor Start")); GetMesh()->SetRenderCustomDepth(true); //在MyGas.h内自定义了一个常量CUSTOM_DEPTH_RED GetMesh()->SetCustomDepthStencilValue(CUSTOM_DEPTH_RED); Weapon->SetRenderCustomDepth(true); Weapon->SetCustomDepthStencilValue(CUSTOM_DEPTH_RED); } void AAuraEnemy::UnHighlightActor() { UE_LOG(LogTemp, Error, TEXT("UnHighlightActor Start")); GetMesh()->SetRenderCustomDepth(false); Weapon->SetRenderCustomDepth(false); } void AAuraEnemy::Tick(float DeltaSeconds) { Super::Tick(DeltaSeconds); } void AAuraEnemy::BeginPlay() { Super::BeginPlay(); AbilitySystemComponent->InitAbilityActorInfo(this,this); }

修改AuraCharacter.h

// Fill out your copyright notice in the Description page of Project Settings. #include "Character/AuraEnemy.h" #include "DrawDebugHelpers.h" #include "AbilitySystem/AuraAbilitySystemComponentBase.h" #include "AbilitySystem/AuraAttributeSet.h" #include "Components/SkeletalMeshComponent.h" #include "MyGas/MyGas.h" AAuraEnemy::AAuraEnemy() { GetMesh()->SetCollisionResponseToChannel(ECC_Visibility,ECR_Block); AbilitySystemComponent = CreateDefaultSubobject<UAuraAbilitySystemComponentBase>("AbilitySystemComponent"); AbilitySystemComponent->SetIsReplicated(true); AttributesSet = CreateDefaultSubobject<UAuraAttributeSet>("AttributesSet"); AbilitySystemComponent->SetReplicationMode(EGameplayEffectReplicationMode::Minimal ); } void AAuraEnemy::HighlightActor() { UE_LOG(LogTemp, Error, TEXT("HighlightActor Start")); GetMesh()->SetRenderCustomDepth(true); //在MyGas.h内自定义了一个常量CUSTOM_DEPTH_RED GetMesh()->SetCustomDepthStencilValue(CUSTOM_DEPTH_RED); Weapon->SetRenderCustomDepth(true); Weapon->SetCustomDepthStencilValue(CUSTOM_DEPTH_RED); } void AAuraEnemy::UnHighlightActor() { UE_LOG(LogTemp, Error, TEXT("UnHighlightActor Start")); GetMesh()->SetRenderCustomDepth(false); Weapon->SetRenderCustomDepth(false); } void AAuraEnemy::Tick(float DeltaSeconds) { Super::Tick(DeltaSeconds); } void AAuraEnemy::BeginPlay() { Super::BeginPlay(); AbilitySystemComponent->InitAbilityActorInfo(this,this); }

.cpp

// Fill out your copyright notice in the Description page of Project Settings. #include "Character/AuraCharacter.h" #include "GameFramework/CharacterMovementComponent.h" #include "PlayerState/AuraPlayerState.h" AAuraCharacter::AAuraCharacter() { //初始化弹簧臂,并且绑定在Root上 CameraBoom = CreateDefaultSubobject<USpringArmComponent>("CameraBoom"); CameraBoom ->SetupAttachment(RootComponent); CameraBoom->TargetArmLength = 400.f; CameraBoom->bUsePawnControlRotation = true; //初始化相机,并且安装到弹簧臂上 FollowCamera = CreateDefaultSubobject<UCameraComponent>("FollowCamera"); FollowCamera->SetupAttachment(CameraBoom,USpringArmComponent::SocketName); FollowCamera->bUsePawnControlRotation = false; //初始化角色移动 //控制角色是否朝着速度的方向进行旋转 GetCharacterMovement()->bOrientRotationToMovement= true; //控制旋转的速度 GetCharacterMovement()->RotationRate = FRotator(0.f,400.f,0.f); //控制移动是否在平面上 GetCharacterMovement()->bConstrainToPlane = true; //当在平面为true的时候,是否强制与平面对齐 GetCharacterMovement()->bSnapToPlaneAtStart = true; bUseControllerRotationPitch = false; bUseControllerRotationRoll = false; bUseControllerRotationYaw = false; } void AAuraCharacter::PossessedBy(AController* NewController) { Super::PossessedBy(NewController); } void AAuraCharacter::OnRep_PlayerState() { Super::OnRep_PlayerState(); InitAbilityActorInfo(); } void AAuraCharacter::InitAbilityActorInfo() { AAuraPlayerState* AuraPlayerState = GetPlayerState<AAuraPlayerState>(); check(AuraPlayerState); AuraPlayerState->GetAbilitySystemComponent()->InitAbilityActorInfo(AuraPlayerState,this); AbilitySystemComponent = AuraPlayerState->GetAbilitySystemComponent(); AttributesSet = AuraPlayerState->GetAttributeSet(); }

AbilitySystem的网络复制有三种模式

AbilitySystemComponent->SetReplicationMode(EGameplayEffectReplicationMode::Mixed);

相关推荐
君鼎11 分钟前
C++设计模式——单例模式
c++·单例模式·设计模式
令狐前生2 小时前
设计模式学习整理
学习·设计模式
刚入门的大一新生2 小时前
C++初阶-string类的模拟实现与改进
开发语言·c++
小冯的编程学习之路2 小时前
【软件测试】:推荐一些接口与自动化测试学习练习网站(API测试与自动化学习全攻略)
c++·selenium·测试工具·jmeter·自动化·测试用例·postman
湘-枫叶情缘2 小时前
解构认知边界:论万能方法的本体论批判与方法论重构——基于跨学科视阈的哲学-科学辩证
科技·学习·重构·生活·学习方法
C++ 老炮儿的技术栈3 小时前
什么是函数重载?为什么 C 不支持函数重载,而 C++能支持函数重载?
c语言·开发语言·c++·qt·算法
inputA4 小时前
【LwIP源码学习6】UDP部分源码分析
c语言·stm32·单片机·嵌入式硬件·网络协议·学习·udp
海尔辛4 小时前
学习黑客5 分钟读懂Linux Permissions 101
linux·学习·安全
猪八戒1.04 小时前
C++ 回调函数和Lambda表达式
c++
源远流长jerry4 小时前
匿名函数lambda、STL与正则表达式
c++