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

相关推荐
小刘同学32121 分钟前
C++11 特性
c++·c11新特性
真的想上岸啊35 分钟前
学习C++、QT---18(C++ 记事本项目的stylesheet)
开发语言·c++·学习
吴梓穆37 分钟前
UE5 重新编译插件版本
ue5
HECUgauss38 分钟前
UE5 使用过程遇到的问题
ue5
m0_5522008239 分钟前
《UE5_C++多人TPS完整教程》学习笔记40 ——《P41 装备(武器)姿势(Equipped Pose)》
c++·游戏·ue5
丁劲犇1 小时前
用 Turbo Vision 2 为 Qt 6 控制台应用创建 TUI 字符 MainFrame
开发语言·c++·qt·tui·字符界面·curse
rui锐rui1 小时前
大数据学习2:HIve
大数据·hive·学习
凛铄linshuo2 小时前
爬虫简单实操2——以贴吧为例爬取“某吧”前10页的网页代码
爬虫·python·学习
charlie1145141912 小时前
深入理解Qt的SetWindowsFlags函数
开发语言·c++·qt·原理分析
呜喵王阿尔萨斯2 小时前
编程中的英语
c语言·c++