How should I modify this code to differentiate between calling from the client and…
- User
How should I modify this code to differentiate between calling from the client and server? if (GetCurrentActivationInfo().ActivationMode == EGameplayAbilityActivationMode::Authority) { AGameStateBase* GameState = GetWorld()->GetGameState(); FAbilityCastBarData AbilityCastBarData; FGameplayTag AbilityGameplayTag = this->AbilityTags.GetByIndex(0); if (Cast(GameState)->GetAbilityCastBarDataFromDataTable(AbilityCastBarData, AbilityGameplayTag)) { // Key into the data using the ability level - 1. float MaxCastTimer = AbilityCastBarData.CastTimes[AbilityLevel - 1]; APlayerCharacterBase* PlayerCharacter = Cast(CharacterActor); // Start the timer on all clients Multicast_TryStartAbilityCastBar_Implementation(PlayerCharacter, this->AbilityLevel, AbilityCastBarData); PlayerCharacter->Multicast_InitCastBarUIWidget(AbilityCastBarData.CastbarColor, MaxCastTimer); // Start the timer on the server and check for completion FTimerDelegate ServerDelegate; float ServerTimerTickRate = 0.05; float ServerStartCastTimer = 0.0f;
ServerDelegate.BindLambda([=]() mutable { PlayerCharacter->Multicast_UpdateCastBarUIWidget(ServerStartCastTimer); ServerStartCastTimer += ServerTimerTickRate; PlayerCharacter->ServerCastBarTimerValue = ServerStartCastTimer; if (ServerStartCastTimer >= MaxCastTimer) { CastBarDelegate.ExecuteIfBound(); ClearAbilityCastTimer(); } }); GetWorld()->GetTimerManager().SetTimer(CastBarTimerHandle, ServerDelegate, ServerTimerTickRate, true, ServerTimerTickRate); return true; } } else { } - Assistant
You can modify the code like this to ensure that different logic is executed based on whether it's running on the server or the client:
if (GetCurrentActivationInfo().ActivationMode == EGameplayAbilityActivationMode::Authority) { // This block runs on the server AGameStateBase* GameState = GetWorld()->GetGameState(); // server-specific code goes here if (Cast<IAbilityCastBarDataInterface>(GameState)->GetAbilityCastBarDataFromDataTable(AbilityCastBarData, AbilityGameplayTag)) { // ... // Start the timer on all clients Multicast_TryStartAbilityCastBar_Implementation(PlayerCharacter, this->AbilityLevel, AbilityCastBarData); } } else { // This block runs on the client // client-specific code goes here } - User
- Assistant
It seems like the issue is related to the declaration of the
FInternalCastBarDelegate. Make sure the delegate is declared correctly, and it should not be inside a comment block. The correct syntax to declare a delegate is:DECLARE_DELEGATE(FInternalCastBarDelegate);To fix the issue, you need to make sure the
FInternalCastBarDelegatedelegate is declared outside any comment block and before the classURagnaTargetedAbility. Here is the corrected code:// Copyright 2022 Sabre Dart Studios #pragma once #include "CoreMinimal.h" #include "RagnaActiveAbility.h" #include "Templates/SharedPointer.h" #include <Skills/AbilityTypes/RagnaAbilityType.h> #include <Skills/AbilityTypes/RagnaBaseAbilityType.h> #include <Skills/NiagaraActorTypes/BaseRagnaAbilityNiagaraActor.h> #include "RagnaTargetedAbility.generated.h" DECLARE_DELEGATE(FInternalCastBarDelegate); // Make sure this declaration exists here class UAbilityTask_WaitTargetData; class ARagnaAbilTargetActor_GroundTrace; /** * */ UCLASS() class OPENWORLDSTARTER_API URagnaTargetedAbility : public URagnaActiveAbility { GENERATED_BODY() // ... rest of the code ... };