当前位置: 首页 > news >正文

虚幻引擎UE5 GAS开发RPG游戏-02 设置英雄角色-18 改成网络多人游戏

1.上一节,武器加载后,发现只能在单机游戏里生效,显然不是我想要的.

准备在网络游戏里也生效:

创建角色基类的子类:敌人类:WarriorEnemyCharacter

2.将GAS和属性集在英雄角色/敌人角色创建:

Source/Warrior/Private/Characters/WarriorHeroCharacter.cpp:

/*构造函数,初始化属性*/
AWarriorHeroCharacter::AWarriorHeroCharacter()
{/*胶囊体组件*/GetCapsuleComponent()->InitCapsuleSize(42.f, 92.f);		//初始化胶囊体半径、半高bUseControllerRotationPitch = false;	//是否使用控制器的旋转俯仰bUseControllerRotationYaw = false;		//是否使用控制器的旋转偏移bUseControllerRotationRoll = false;		//是否使用控制器的旋转翻滚/*弹簧臂组件*/CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));CameraBoom->SetupAttachment(GetRootComponent());		//设置父级(根组件)CameraBoom->TargetArmLength = 200.f;		//设置臂长CameraBoom->SocketOffset = FVector(0.f, 55.f, 65.f);		//插槽偏移CameraBoom->bUsePawnControlRotation = true;		//使用棋子控制旋转/*相机组件*/FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName);		//设置父级(弹簧臂插槽名称)FollowCamera->bUsePawnControlRotation = false;/*角色移动组件*/GetCharacterMovement()->bOrientRotationToMovement = true;		//保持旋转到位移方向GetCharacterMovement()->RotationRate = FRotator(0.f, 500.f, 0.f);		//旋转速度(转身速度)GetCharacterMovement()->MaxWalkSpeed = 400.f;		//最大位移速度GetCharacterMovement()->BrakingDecelerationWalking = 200.f;		//行走加速度//将能力系统组件添加到角色WarriorAbilitySystemComponent = CreateDefaultSubobject<UWarriorAbilitySystemComponent>(TEXT("WarriorAbilitySystemComponent"));//将属性集添加到角色WarriorAttributeSet = CreateDefaultSubobject<UWarriorAttributeSet>(TEXT("WarriorAttributeSet"));}

Source/Warrior/Private/Characters/WarriorEnemyCharacter.cpp:

AWarriorEnemyCharacter::AWarriorEnemyCharacter()
{//将能力系统组件添加到角色WarriorAbilitySystemComponent = CreateDefaultSubobject<UWarriorAbilitySystemComponent>(TEXT("WarriorAbilitySystemComponent"));//将属性集添加到角色WarriorAttributeSet = CreateDefaultSubobject<UWarriorAttributeSet>(TEXT("WarriorAttributeSet"));
}

3.GAS的复制模式

(1)英雄类采用混合模式:

WarriorAbilitySystemComponent->SetReplicationMode(EGameplayEffectReplicationMode::Mixed);	//复制模式

Source/Warrior/Private/Characters/WarriorHeroCharacter.cpp:

// Copyright @ ChenChao#include "Characters/WarriorHeroCharacter.h"
#include "Components/CapsuleComponent.h"
#include "GameFramework/SpringArmComponent.h"
#include "Camera/CameraComponent.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "EnhancedInputSubsystems.h"
#include "DataAssets/Input/WarriorDataAssetInputConfig.h"
#include "Components/Input/WarriorInputComponent.h"#include "WarriorDebugHelper.h"
#include "WarriorGameplayTags.h"
#include "AbilitySystem/WarriorAbilitySystemComponent.h"
#include "AbilitySystem/WarriorAttributeSet.h"
#include "DataAssets/StartUp/DataAsset_StartUpDataBase.h"/*构造函数,初始化属性*/
AWarriorHeroCharacter::AWarriorHeroCharacter()
{/*胶囊体组件*/GetCapsuleComponent()->InitCapsuleSize(42.f, 92.f);		//初始化胶囊体半径、半高bUseControllerRotationPitch = false;	//是否使用控制器的旋转俯仰bUseControllerRotationYaw = false;		//是否使用控制器的旋转偏移bUseControllerRotationRoll = false;		//是否使用控制器的旋转翻滚/*弹簧臂组件*/CameraBoom = CreateDefaultSubobject<USpringArmComponent>(TEXT("CameraBoom"));CameraBoom->SetupAttachment(GetRootComponent());		//设置父级(根组件)CameraBoom->TargetArmLength = 200.f;		//设置臂长CameraBoom->SocketOffset = FVector(0.f, 55.f, 65.f);		//插槽偏移CameraBoom->bUsePawnControlRotation = true;		//使用棋子控制旋转/*相机组件*/FollowCamera = CreateDefaultSubobject<UCameraComponent>(TEXT("FollowCamera"));FollowCamera->SetupAttachment(CameraBoom, USpringArmComponent::SocketName);		//设置父级(弹簧臂插槽名称)FollowCamera->bUsePawnControlRotation = false;/*角色移动组件*/GetCharacterMovement()->bOrientRotationToMovement = true;		//保持旋转到位移方向GetCharacterMovement()->RotationRate = FRotator(0.f, 500.f, 0.f);		//旋转速度(转身速度)GetCharacterMovement()->MaxWalkSpeed = 400.f;		//最大位移速度GetCharacterMovement()->BrakingDecelerationWalking = 200.f;		//行走加速度//将能力系统组件添加到角色WarriorAbilitySystemComponent = CreateDefaultSubobject<UWarriorAbilitySystemComponent>(TEXT("WarriorAbilitySystemComponent"));WarriorAbilitySystemComponent->SetIsReplicated(true);WarriorAbilitySystemComponent->SetReplicationMode(EGameplayEffectReplicationMode::Mixed);	//复制模式//将属性集添加到角色WarriorAttributeSet = CreateDefaultSubobject<UWarriorAttributeSet>(TEXT("WarriorAttributeSet"));}//当Controller(玩家或AI)获得该Pawn控制权时触发
void AWarriorHeroCharacter::PossessedBy(AController* NewController)
{Super::PossessedBy(NewController);if (CharacterStartUpData.IsNull()) return;UDataAsset_StartUpDataBase* LoadedData = CharacterStartUpData.LoadSynchronous();if (LoadedData == nullptr) return;LoadedData->GiveToAbilitySystemComponent(WarriorAbilitySystemComponent);
}//设置玩家输入组件
void AWarriorHeroCharacter::SetupPlayerInputComponent(UInputComponent* PlayerInputComponent)
{Super::SetupPlayerInputComponent(PlayerInputComponent);checkf(DataAssetInputConfig,TEXT("DataAssetInputConfig 指针为空!"));// 获取本地玩家class ULocalPlayer* LocalPlayer = GetController<APlayerController>()->GetLocalPlayer();check(LocalPlayer);// 获取增强输入本地玩家子系统UEnhancedInputLocalPlayerSubsystem* Subsystem = ULocalPlayer::GetSubsystem<UEnhancedInputLocalPlayerSubsystem>(LocalPlayer);check(Subsystem);//添加映射上下文Subsystem->AddMappingContext(DataAssetInputConfig->DefaultMappingContext, 0);//获取战士输入组件UWarriorInputComponent* WarriorInputComponent = CastChecked<UWarriorInputComponent>(PlayerInputComponent);// 绑定回调函数WarriorInputComponent->BindNativeInputAction(DataAssetInputConfig, WarriorGameplayTags::InputTag_Move, ETriggerEvent::Triggered, this, &AWarriorHeroCharacter::Input_Move);WarriorInputComponent->BindNativeInputAction(DataAssetInputConfig, WarriorGameplayTags::InputTag_Look, ETriggerEvent::Triggered, this, &AWarriorHeroCharacter::Input_Look);
}void AWarriorHeroCharacter::BeginPlay()
{Super::BeginPlay();}//回调函数: 移动输入
void AWarriorHeroCharacter::Input_Move(const FInputActionValue& InputActionValue)
{//获取移动二维向量const FVector2D MovementVector = InputActionValue.Get<FVector2D>();// 获取移动旋转const FRotator MovementRotator(0.f, GetControlRotation().Yaw, 0.0f);//设置前进if (MovementVector.Y != 0.f){//前进方向const FVector ForwardDirection = MovementRotator.RotateVector(FVector::ForwardVector);//添加移动输入AddMovementInput(ForwardDirection, MovementVector.Y);}//设置向右if (MovementVector.X != 0.f){//向右方向const FVector RightDirection = MovementRotator.RotateVector(FVector::RightVector);AddMovementInput(RightDirection, MovementVector.X);}}//回调函数: 视角输入
void AWarriorHeroCharacter::Input_Look(const FInputActionValue& InputActionValue)
{//获取鼠标轴二维向量const FVector2D LookAxisVector = InputActionValue.Get<FVector2D>();//左右看if (LookAxisVector.X != 0.f){AddControllerYawInput(LookAxisVector.X);}// 上下看if (LookAxisVector.Y != 0.f){AddControllerPitchInput(LookAxisVector.Y);}
}

(2) 敌人类采用最小模式:

WarriorAbilitySystemComponent->SetReplicationMode(EGameplayEffectReplicationMode::Minimal);	//复制模式

Source/Warrior/Private/Characters/WarriorEnemyCharacter.cpp:

// Copyright @ ChenChao#include "Characters/WarriorEnemyCharacter.h"#include "AbilitySystem/WarriorAbilitySystemComponent.h"
#include "AbilitySystem/WarriorAttributeSet.h"AWarriorEnemyCharacter::AWarriorEnemyCharacter()
{//将能力系统组件添加到角色WarriorAbilitySystemComponent = CreateDefaultSubobject<UWarriorAbilitySystemComponent>(TEXT("WarriorAbilitySystemComponent"));WarriorAbilitySystemComponent->SetIsReplicated(true);WarriorAbilitySystemComponent->SetReplicationMode(EGameplayEffectReplicationMode::Minimal);	//复制模式//将属性集添加到角色WarriorAttributeSet = CreateDefaultSubobject<UWarriorAttributeSet>(TEXT("WarriorAttributeSet"));
}

4.初始化GAS的能力演员信息

我们什么时候初始化呢?

玩家角色:

  • ASC在角色上,在PossessedBy()和AcknowledgePossession()调用;
  • ASC在状态类上构造时,在PossessedBy()和OnRep_PlayerState()上调用.

敌人角色:

  • 在BeginPlay()调用

(1)为敌人类初始化演员能力信息:

void AWarriorEnemyCharacter::BeginPlay()
{Super::BeginPlay();WarriorAbilitySystemComponent->InitAbilityActorInfo(this,this);
}

(2)为玩家类初始化演员能力信息:

Source/Warrior/Public/Characters/WarriorHeroCharacter.h:

	//~Begin Apawn Interface.virtual void PossessedBy(AController* NewController) override;	//当Controller(玩家或AI)获得该Pawn控制权时触发//~ End APawn Interface

Source/Warrior/Private/Characters/WarriorHeroCharacter.cpp:

//当Controller(玩家或AI)获得该Pawn控制权时触发
void AWarriorHeroCharacter::PossessedBy(AController* NewController)
{Super::PossessedBy(NewController);if (WarriorAbilitySystemComponent == nullptr) return;WarriorAbilitySystemComponent->InitAbilityActorInfo(this, this);if (CharacterStartUpData.IsNull()) return;UDataAsset_StartUpDataBase* LoadedData = CharacterStartUpData.LoadSynchronous();if (LoadedData == nullptr) return;LoadedData->GiveToAbilitySystemComponent(WarriorAbilitySystemComponent);
}

Source/Warrior/Public/Controllers/WarriorHeroController.h:

public://~Begin PlayerController Interface.virtual void AcknowledgePossession(class APawn* P) override;//~ End PlayerController Interface

Source/Warrior/Private/Controllers/WarriorHeroController.cpp:

void AWarriorHeroController::AcknowledgePossession(class APawn* P)
{Super::AcknowledgePossession(P);AWarriorHeroCharacter* WarriorHeroCharacter = Cast<AWarriorHeroCharacter>(P);if (WarriorHeroCharacter) return;UWarriorAbilitySystemComponent* WarriorAbilitySystemComponent = WarriorHeroCharacter->GetWarriorAbilitySystemComponent();if (WarriorAbilitySystemComponent == nullptr) return;WarriorAbilitySystemComponent->InitAbilityActorInfo(this, this);
}

AcknowledgePossession是Unreal Engine中用于处理角色控制权确认的重要函数,通常在网络游戏开发中与GameplayAbilitySystem(GAS)配合使用。该函数的主要功能是:

  1. 服务器-客户端同步‌:当服务器将Pawn的控制权授予客户端时,客户端通过此函数确认接收
  2. GAS初始化关键点‌:必须在函数内调用InitAbilityActorInfo来初始化GAS相关的Actor信息,否则会导致能力系统无法正常工作
  3. 调用位置‌:通常在PossessedBy(服务器端)和AcknowledgePossession(客户端)两个地方成对实现

在专用服务器架构中,该函数对确保客户端和服务器端的GAS组件同步至关重要。若未正确实现,可能导致网络游戏中能力激活失败或属性同步异常等问题。

后续不会了,再学习一下

http://www.dtcms.com/a/269051.html

相关文章:

  • C++:string类(3)(string类的模拟实现)
  • 批量OCR的GitHub项目
  • Linux 进程控制:全面深入剖析进程创建、终止、替换与等待
  • UI自动化常见面试题
  • qt-C++笔记之QSplitter
  • PyTorch笔记3----------统计学相关函数
  • AI PPT探秘
  • ARMv7单核CPU上SWI(软件中断)验证
  • 策略与工厂的演进:打造工业级Spring路由框架
  • window显示驱动开发—X 通道解释
  • 如何远程管理Linux服务器
  • Rust 内存结构:深入解析
  • DPDK 网络驱动 之 UIO
  • 如何使用 Renode(快速入门)
  • 二进制安全-汇编语言-03-寄存器(内存访问)
  • cuda编程笔记(6)--流
  • PowerQuery逆透视之二维表转一维表
  • 50天50个小项目 (Vue3 + Tailwindcss V4) ✨ | ContentPlaceholder(背景占位)
  • 电动汽车的传导发射仿真
  • navicate如何设置数据库引擎
  • RabbitMQ在SpringBoot中的使用详解
  • 2025光学成像与机器视觉国际会议 (OIMV 2025)
  • 用Python制作华夫图:从零开始
  • ShortGPT: Layers in Large Language Models are More Redundant Than You Expect
  • delphi,c++程序 阻止Win11 用户更改系统时间
  • 电子防抖(EIS)技术概述
  • Springboot 如何加密数据库连接相关配置信息
  • 特伦斯T1节拍器,突出综合优势与用户体验
  • AI建站工具对决:Wegic、腾讯云、Hocoos、Typedream深度测评,谁是国内用户的首选?
  • MySQL Galera Cluster企业级部署