Who Can Have Ability System Component (ASC)
- Any actor can have ability system component that includes things like chests, breakable boxes, turrets etc.
- It is recommended to create the base class in C++ because we need to implement the “
AbilitySystemInterface”, it is not needed but it is recommended because with this interface you can use some functions from Ability system library.
Best Place to have ASC for Players
- For a simple single player game, you can have your ASC attached to your player character and initialize with the owner and avatar with player character actor as well.
- The best actor that works for multiplayer is
Player State. - Player state exists on server but is replicated to clients as well unlike controllers which only exist on server, if your game is single player you can opt to have your ASC only on controller.
- For AI bots in multiplayer games all you need to do is set
bWantsPlayerStateof AI Controller to true. - Player State is persistence across respawns so all of your de-buffs, cooldowns will remain as they were when you respawn. You will need to manually remove and reset them.
Owner And Avatar
- Owner actor is what persistently represents your character like Controller or Player state, where as Avatar is what physically represents you like Pawn or Character.
- Things like Turret and Boxes can be both Owner and Avatar themselves.
Where to call Init Or Refresh Ability Actor Info
This must be called on both server and client to work.
To Guarantee that PC exists client-side before calling Init is to do this in the Player state’s On Rep function inside the PC, this is done in Lyra as well:
void ALyraPlayerController::OnRep_PlayerState()
{
Super::OnRep_PlayerState();
BroadcastOnPlayerStateChanged();
// When we're a client connected to a remote server, the player controller may replicate later than the PlayerState and AbilitySystemComponent.
if (GetWorld()->IsNetMode(NM_Client))
{
if (ALyraPlayerState* LyraPS = GetPlayerState<ALyraPlayerState>())
{
if (ULyraAbilitySystemComponent* LyraASC = LyraPS->GetLyraAbilitySystemComponent())
{
// Calls InitAbilityActorInfo
LyraASC->RefreshAbilityActorInfo();
LyraASC->TryActivateAbilitiesOnSpawn();
}
}
}
}
The other part how I handled It was through another function inside the PC, this will do it for client.
// For Client, Must be used for initializing the ability system
virtual void AcknowledgePossession(APawn* InPawn) override;
void ARHN_PlayerController::AcknowledgePossession(APawn* InPawn)
{
Super::AcknowledgePossession(InPawn);
ARHN_CustomCharacter* C = Cast<ARHN_CustomCharacter>(InPawn);
if (C)
{
C->GetAbilitySystemComponent()->InitAbilityActorInfo(C, C);
// Client / Call this On Server as well
C->InitializeAttributes();
}
}
For server we would use the character’s Possessed By function:
void ARHN_CustomCharacter::PossessedBy(AController* NewController)
{
Super::PossessedBy(NewController);
if (ASC)
{
ASC->InitAbilityActorInfo(this, this);
}
SetOwner(NewController);
// Give Ability to Player on the server
if (ASC && AbilityToGive && AbilityToGive2)
{
ASC->GiveAbility(FGameplayAbilitySpec(AbilityToGive.GetDefaultObject(), 1, 0));
ASC->GiveAbility(FGameplayAbilitySpec(AbilityToGive2.GetDefaultObject(), 1, 1));
}
// Call this when you get respawned
//ASC->RefreshAbilityActorInfo();
// In the future if players rejoin an ongoing session and we don't want to reset attributes, then change this
// Server / Call this on Client as well
InitializeAttributes();
}
Replication Modes
There are three replication modes: Full, Mixed and Minimal.
Most of time you would be either using Full or Mixed, Mixed is even more common as in Mixed the owning client will receive full Gameplay Effect information where as other clients will only get the tag.
A Good example of using Full is when other clients need to be able to see the remaining gameplay effect durations that are applied on other clients or bots.
Top comments (0)