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

本文介绍了如何在UnrealEngine项目中集成AuraPlayerState、AuraAbilitySystemComponentBase和AuraAttributeSet,以及在Build.cs、Enemy.cpp和AuraCharacter.h/cpp中设置网络复制模式,确保游戏中的能力系统组件和玩家状态在网络同步中的正确处理。
摘要由CSDN通过智能技术生成

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

  • 14
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值