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