专项导航 · 资料索引
在两名玩家各自拥有的角色上建 ASC。让服务器授予能力,让客户端在输入时查到复制过来的能力,避免把双端的初始化误写成双端授予。
使用 UE 5.6 C++ 游戏工程,示例模块名 RelayLab。启用 Gameplay Abilities 插件并重启,在 RelayLab.Build.cs 的模块构造函数中补充依赖,保留模板已有模块。若使用 Enhanced Input 的原生类型,再保留或添加 EnhancedInput。工程名不同,替换下文的 RELAYLAB_API。
PublicDependencyModuleNames.AddRange(new string[] {
"GameplayAbilities", "GameplayTags", "GameplayTasks"
});
在游戏模块内创建 RelayTech.h/.cpp,同时完成下一章的 RelayVitals.h/.cpp 后一起编译。这里选择把 ASC 放在角色上,角色销毁即结束其技能状态。操作系统意义上的所有权、网络连接所有权、GAS 的 OwnerActor/AvatarActor 是不同问题。此例后两项 GAS Actor 都填当前角色;玩家的网络连接仍来自其控制器。
// RelayTech.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "AbilitySystemInterface.h"
#include "RelayTech.generated.h"
class UAbilitySystemComponent;
class UGameplayAbility;
class URelayVitals;
UCLASS()
class RELAYLAB_API ARelayTech : public ACharacter, public IAbilitySystemInterface
{
GENERATED_BODY()
public:
ARelayTech();
virtual UAbilitySystemComponent* GetAbilitySystemComponent() const override;
protected:
virtual void BeginPlay() override;
virtual void PossessedBy(AController* NewController) override;
virtual void OnRep_Controller() override;
UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category="GAS")
TObjectPtr<UAbilitySystemComponent> AbilitySystem;
UPROPERTY(VisibleAnywhere, Category="GAS")
TObjectPtr<URelayVitals> Vitals;
UPROPERTY(EditDefaultsOnly, BlueprintReadOnly, Category="GAS")
TSubclassOf<UGameplayAbility> RepairAbility;
};
// RelayTech.cpp
#include "RelayTech.h"
#include "RelayVitals.h"
#include "AbilitySystemComponent.h"
#include "Abilities/GameplayAbility.h"
ARelayTech::ARelayTech()
{
bReplicates = true;
AbilitySystem = CreateDefaultSubobject<UAbilitySystemComponent>(TEXT("AbilitySystem"));
AbilitySystem->SetIsReplicated(true);
AbilitySystem->SetReplicationMode(EGameplayEffectReplicationMode::Mixed);
Vitals = CreateDefaultSubobject<URelayVitals>(TEXT("Vitals"));
}
UAbilitySystemComponent* ARelayTech::GetAbilitySystemComponent() const
{
return AbilitySystem;
}
void ARelayTech::BeginPlay()
{
Super::BeginPlay();
AbilitySystem->InitAbilityActorInfo(this, this);
if (HasAuthority() && RepairAbility)
{
AbilitySystem->GiveAbility(FGameplayAbilitySpec(RepairAbility, 1));
}
}
void ARelayTech::PossessedBy(AController* NewController)
{
Super::PossessedBy(NewController);
AbilitySystem->InitAbilityActorInfo(this, this);
}
void ARelayTech::OnRep_Controller()
{
Super::OnRep_Controller();
AbilitySystem->InitAbilityActorInfo(this, this);
}
- 派生角色蓝图
BP_RelayTech,把 RepairAbility 设为稍后创建的 GA_RepairPulse。先可用一个继承 GameplayAbility 的空蓝图,在 Activate 事件打印角色名后立刻 End。把 GA 的 Net Execution Policy 明确设为 Server Only,Instancing Policy 设为 Instanced Per Actor。 - 在实验地图的 GameMode 把 Default Pawn Class 设成
BP_RelayTech。放两个相距约 300 cm、朝向相反的 PlayerStart,留好胶囊体出生空间。如果出生点朝向不便控制,直接移动或转动角色,使其正面朝向对方。要画面更好辨认,可给角色加网格和摄像机。 - 用两个玩家窗口运行。分别在
BeginPlay 的授权分支和 PossessedBy 打断点,记录 NetMode、Actor 名、Controller、IsLocallyControlled()。同一进程可能模拟多个世界,断点先看当前世界。每个新角色应由服务器授予一次,后续重新占有同一角色只刷新 ActorInfo。 - 第 04 章接入输入。若首次按键无响应,检查拥有端能力列表是否已接收授予,再查
RepairAbility 的类是否与列表一致。复制和客户端 BeginPlay 没有固定的先后保证;正式 HUD 可以在能力授予可见后再启用按钮。
迁移到 PlayerState 的分界。 需要跨重生保留资源时,第 11 章会把第一个参数换为 PlayerState,第二个仍是当前 Pawn。把上面的 this, this 直接搬进 PlayerState,会让动画、位置或目标选择找到错误 Avatar。
先完成可复制角色、ASC、AttributeSet 和服务器授予四件事。ActorInfo 可以随控制关系更新,GiveAbility 的次数要与设计中的授予来源一致。
- 为何 PossessedBy 可多次执行,GiveAbility 却放在此例的 BeginPlay?
- 主机能发动,远端玩家不能,最先检查哪三项?
- 同一个 ASC 要让外部系统通过接口查到,哪个函数必须返回什么?
- 销毁这个角色再生成一个,想保留上一个电量,本章代码会自动做到吗?
Ability System Component and Attributes · UAbilitySystemComponent · Abilities in Lyra · Getting Started with GAS
← 从双人维修任务理解GAS · 专项学习导航 · AttributeSet与属性复制 →