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);

相关推荐
蜀黍@猿12 分钟前
【C++ 基础】从C到C++有哪些变化
c++
Am心若依旧40914 分钟前
[c++11(二)]Lambda表达式和Function包装器及bind函数
开发语言·c++
zh路西法24 分钟前
【C++决策和状态管理】从状态模式,有限状态机,行为树到决策树(一):从电梯出发的状态模式State Pattern
c++·决策树·状态模式
轩辰~38 分钟前
网络协议入门
linux·服务器·开发语言·网络·arm开发·c++·网络协议
lxyzcm1 小时前
C++23新特性解析:[[assume]]属性
java·c++·spring boot·c++23
蜀黍@猿1 小时前
C/C++基础错题归纳
c++
虾球xz1 小时前
游戏引擎学习第55天
学习·游戏引擎
雨中rain2 小时前
Linux -- 从抢票逻辑理解线程互斥
linux·运维·c++
oneouto2 小时前
selenium学习笔记(二)
笔记·学习·selenium
sealaugh322 小时前
aws(学习笔记第十九课) 使用ECS和Fargate进行容器开发
笔记·学习·aws