-
Notifications
You must be signed in to change notification settings - Fork 7
Input buffer #260
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Input buffer #260
Changes from 11 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
dfadd7c
setup input buffer
6390933
wip register triggered actions
4f17a39
input buffer working with one ability
726a242
code cleanup + trigger priority
83458a8
add new priority
df0db85
fix find input buffer component
72edbd4
cleanup code
ddd7262
reset + clang format
Fr0oZzFred 528ecbc
add blueprint callable
Fr0oZzFred d872059
review
b1c1266
add monitoring time verification
a9b11b0
review standards
0b3e854
sorted map
261a243
remove uproperty and ufunction
de401d7
clang format
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
Source/GameBaseFramework/Private/Animation/GBFAnimNotifyState_InputBuffer.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
#include "Animation/GBFAnimNotifyState_InputBuffer.h" | ||
|
||
#include "GBFLog.h" | ||
|
||
#include <Components/SkeletalMeshComponent.h> | ||
|
||
void UGBFAnimNotifyState_InputBuffer::NotifyBegin( USkeletalMeshComponent * mesh_component, UAnimSequenceBase * animation, float total_duration, const FAnimNotifyEventReference & event_reference ) | ||
{ | ||
Super::NotifyBegin( mesh_component, animation, total_duration, event_reference ); | ||
|
||
if ( auto * ability_input_buffer_component = GetAbilityInputBufferComponent( mesh_component ) ) | ||
{ | ||
ability_input_buffer_component->StartMonitoring( InputTagsToCheck, TriggerPriority ); | ||
} | ||
} | ||
|
||
void UGBFAnimNotifyState_InputBuffer::NotifyEnd( USkeletalMeshComponent * mesh_component, UAnimSequenceBase * animation, const FAnimNotifyEventReference & event_reference ) | ||
{ | ||
Super::NotifyEnd( mesh_component, animation, event_reference ); | ||
|
||
if ( auto * ability_input_buffer_component = GetAbilityInputBufferComponent( mesh_component ) ) | ||
{ | ||
ability_input_buffer_component->StopMonitoring(); | ||
} | ||
} | ||
|
||
UGBFAbilityInputBufferComponent * UGBFAnimNotifyState_InputBuffer::GetAbilityInputBufferComponent_Implementation( const USkeletalMeshComponent * mesh_component ) const | ||
{ | ||
if ( mesh_component == nullptr ) | ||
{ | ||
return nullptr; | ||
} | ||
|
||
const auto * owner = mesh_component->GetOwner(); | ||
if ( owner == nullptr ) | ||
{ | ||
UE_LOG( LogGBF, Error, TEXT( "Ability Input Buffer Notify : No Owner found" ) ); | ||
return nullptr; | ||
} | ||
|
||
auto * ability_input_buffer_component = owner->FindComponentByClass< UGBFAbilityInputBufferComponent >(); | ||
|
||
if ( ability_input_buffer_component != nullptr ) | ||
{ | ||
return ability_input_buffer_component; | ||
} | ||
|
||
// Check all parent | ||
auto * parent = owner->GetParentActor(); | ||
while ( parent ) | ||
{ | ||
ability_input_buffer_component = parent->FindComponentByClass< UGBFAbilityInputBufferComponent >(); | ||
if ( ability_input_buffer_component != nullptr ) | ||
{ | ||
return ability_input_buffer_component; | ||
} | ||
parent = parent->GetParentActor(); | ||
} | ||
|
||
// Check all attached actors | ||
TArray< AActor * > actors; | ||
owner->GetAttachedActors( actors, true, true ); | ||
actors.RemoveAll( []( AActor * actor ) { | ||
return !Cast< APawn >( actor ); | ||
} ); | ||
for ( auto & child : actors ) | ||
{ | ||
ability_input_buffer_component = child->FindComponentByClass< UGBFAbilityInputBufferComponent >(); | ||
if ( ability_input_buffer_component != nullptr ) | ||
{ | ||
return ability_input_buffer_component; | ||
} | ||
} | ||
|
||
UE_LOG( LogGBF, Error, TEXT( "No Ability Input Buffer Component found" ) ); | ||
return nullptr; | ||
} |
230 changes: 230 additions & 0 deletions
230
Source/GameBaseFramework/Private/Characters/Components/GBFAbilityInputBufferComponent.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,230 @@ | ||
#include "Characters/Components/GBFAbilityInputBufferComponent.h" | ||
|
||
#include "Characters/Components/GBFHeroComponent.h" | ||
#include "Characters/Components/GBFPawnExtensionComponent.h" | ||
#include "GAS/Components/GBFAbilitySystemComponent.h" | ||
#include "Input/GBFInputComponent.h" | ||
|
||
UGBFAbilityInputBufferComponent::UGBFAbilityInputBufferComponent( const FObjectInitializer & ObjectInitializer ) : | ||
Super( ObjectInitializer ) | ||
{ | ||
TriggerPriority = ETriggerPriority::LastTriggeredInput; | ||
this->PrimaryComponentTick.bStartWithTickEnabled = false; | ||
this->PrimaryComponentTick.bCanEverTick = true; | ||
} | ||
|
||
void UGBFAbilityInputBufferComponent::TickComponent( float DeltaTime, ELevelTick TickType, FActorComponentTickFunction * ThisTickFunction ) | ||
{ | ||
Super::TickComponent( DeltaTime, TickType, ThisTickFunction ); | ||
MonitoringTime += DeltaTime; | ||
if ( !ensureAlwaysMsgf( MonitoringTime <= MaxMonitoringTime, TEXT( "Ability Input Buffer didn't call Stop Monitor 5 secs after activation, please call it manually !" ) ) ) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. print the actual MaxMonitoringTime instead of 5 |
||
{ | ||
StopMonitoring(); | ||
} | ||
} | ||
|
||
void UGBFAbilityInputBufferComponent::StartMonitoring( FGameplayTagContainer input_tags_to_check, ETriggerPriority trigger_priority ) | ||
{ | ||
if ( input_tags_to_check.IsEmpty() ) | ||
{ | ||
return; | ||
} | ||
|
||
Reset(); | ||
TriggerPriority = trigger_priority; | ||
InputTagsToCheck = input_tags_to_check; | ||
BindActions(); | ||
|
||
#if !UE_BUILD_SHIPPING | ||
SetComponentTickEnabled( true ); | ||
#endif | ||
} | ||
|
||
void UGBFAbilityInputBufferComponent::StopMonitoring() | ||
{ | ||
RemoveBinds(); | ||
TryToTriggerAbility(); | ||
Reset(); | ||
} | ||
|
||
void UGBFAbilityInputBufferComponent::Reset() | ||
{ | ||
TriggeredTags.Reset(); | ||
InputTagsToCheck.Reset(); | ||
BindHandles.Reset(); | ||
MonitoringTime = 0.0f; | ||
|
||
#if !UE_BUILD_SHIPPING | ||
SetComponentTickEnabled( false ); | ||
#endif | ||
} | ||
|
||
void UGBFAbilityInputBufferComponent::BindActions() | ||
{ | ||
if ( InputTagsToCheck.IsEmpty() ) | ||
{ | ||
return; | ||
} | ||
|
||
auto * pawn = GetPawn< APawn >(); | ||
if ( pawn == nullptr ) | ||
{ | ||
return; | ||
} | ||
|
||
auto * input_component = Cast< UEnhancedInputComponent >( pawn->InputComponent ); | ||
if ( input_component == nullptr ) | ||
{ | ||
return; | ||
} | ||
|
||
const auto * hero_component = UGBFHeroComponent::FindHeroComponent( pawn ); | ||
if ( hero_component == nullptr ) | ||
{ | ||
return; | ||
} | ||
|
||
for ( auto & input_config : hero_component->GetBoundActionsByInputconfig() ) | ||
{ | ||
for ( auto & tag : InputTagsToCheck ) | ||
{ | ||
if ( const auto * input_action = input_config.Key->FindAbilityInputActionForTag( tag ) ) | ||
{ | ||
BindHandles.Add( input_component->BindAction( input_action, ETriggerEvent::Triggered, this, &ThisClass::AbilityInputTagPressed, tag ).GetHandle() ); | ||
} | ||
} | ||
} | ||
} | ||
|
||
void UGBFAbilityInputBufferComponent::RemoveBinds() | ||
{ | ||
auto * pawn = GetPawn< APawn >(); | ||
if ( pawn == nullptr ) | ||
{ | ||
return; | ||
} | ||
|
||
if ( auto * input_component = Cast< UEnhancedInputComponent >( pawn->InputComponent ) ) | ||
{ | ||
for ( auto & handle : BindHandles ) | ||
{ | ||
input_component->RemoveBindingByHandle( handle ); | ||
} | ||
} | ||
} | ||
|
||
void UGBFAbilityInputBufferComponent::AbilityInputTagPressed( FGameplayTag input_tag ) | ||
{ | ||
TriggeredTags.Add( input_tag ); | ||
} | ||
|
||
bool UGBFAbilityInputBufferComponent::TryToTriggerAbility() | ||
{ | ||
if ( TriggeredTags.IsEmpty() ) | ||
{ | ||
return false; | ||
} | ||
|
||
auto * pawn = GetPawn< APawn >(); | ||
if ( pawn == nullptr ) | ||
{ | ||
return false; | ||
} | ||
|
||
const auto * pawn_ext_comp = UGBFPawnExtensionComponent::FindPawnExtensionComponent( pawn ); | ||
if ( pawn_ext_comp == nullptr ) | ||
{ | ||
return false; | ||
} | ||
|
||
if ( auto * asc = pawn_ext_comp->GetGBFAbilitySystemComponent() ) | ||
{ | ||
for ( auto & tagged_ability_tag : InputTagsToCheck ) | ||
{ | ||
auto * tagged_ability = asc->FindAbilityClassWithInputTag( tagged_ability_tag ); | ||
asc->CancelAbility( tagged_ability ); | ||
} | ||
|
||
// Try to activate ability in priority order | ||
FGameplayTag tag = TryToGetInputTagWithPriority(); | ||
|
||
while ( tag.IsValid() ) | ||
{ | ||
if ( auto * ability = asc->FindAbilityClassWithInputTag( tag ) ) | ||
{ | ||
asc->CancelAbility( ability ); | ||
if ( asc->TryActivateAbilityByClass( ability->GetClass() ) ) | ||
{ | ||
return true; | ||
} | ||
} | ||
|
||
tag = TryToGetInputTagWithPriority(); | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
FGameplayTag UGBFAbilityInputBufferComponent::TryToGetInputTagWithPriority() | ||
{ | ||
if ( TriggeredTags.IsEmpty() ) | ||
{ | ||
return FGameplayTag::EmptyTag; | ||
} | ||
|
||
switch ( TriggerPriority ) | ||
{ | ||
case ETriggerPriority::LastTriggeredInput: | ||
return GetLastTriggeredInput(); | ||
|
||
case ETriggerPriority::MostTriggeredInput: | ||
return GetMostTriggeredInput(); | ||
|
||
default: | ||
return FGameplayTag::EmptyTag; | ||
} | ||
} | ||
|
||
FGameplayTag UGBFAbilityInputBufferComponent::GetLastTriggeredInput() | ||
{ | ||
FGameplayTag first_tag = TriggeredTags[ 0 ]; | ||
TriggeredTags.Remove( first_tag ); | ||
return first_tag; | ||
} | ||
|
||
FGameplayTag UGBFAbilityInputBufferComponent::GetMostTriggeredInput() | ||
{ | ||
TMap< int, FGameplayTag > triggered_tag_map; | ||
|
||
// Remove all to get count easily | ||
for ( auto & tag_to_remove : InputTagsToCheck ) | ||
{ | ||
int count = TriggeredTags.Remove( tag_to_remove ); | ||
triggered_tag_map.Add( count, tag_to_remove ); | ||
} | ||
|
||
// Get most triggered input | ||
int max = -1; | ||
for ( auto & input : triggered_tag_map ) | ||
{ | ||
if ( input.Key > max ) | ||
{ | ||
max = input.Key; | ||
} | ||
} | ||
|
||
FGameplayTag most_triggered_tag = triggered_tag_map.FindAndRemoveChecked( max ); | ||
|
||
// Sort to keep order if first ability fails | ||
triggered_tag_map.Remove( 0 ); | ||
triggered_tag_map.KeySort( | ||
[]( const int & a, const int & b ) { | ||
return a > b; | ||
} ); | ||
for ( auto & i : triggered_tag_map ) | ||
{ | ||
TriggeredTags.Add( i.Value ); | ||
} | ||
|
||
return most_triggered_tag; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
Source/GameBaseFramework/Public/Animation/GBFAnimNotifyState_InputBuffer.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#pragma once | ||
|
||
#include "Characters/Components/GBFAbilityInputBufferComponent.h" | ||
|
||
#include <Animation/AnimNotifies/AnimNotifyState.h> | ||
#include <CoreMinimal.h> | ||
|
||
#include "GBFAnimNotifyState_InputBuffer.generated.h" | ||
|
||
class UGBFAbilityInputBufferComponent; | ||
|
||
UCLASS( DisplayName = "Ability Input Buffer Window" ) | ||
class GAMEBASEFRAMEWORK_API UGBFAnimNotifyState_InputBuffer : public UAnimNotifyState | ||
{ | ||
GENERATED_BODY() | ||
|
||
public: | ||
void NotifyBegin( USkeletalMeshComponent * mesh_component, UAnimSequenceBase * animation, float total_duration, const FAnimNotifyEventReference & event_reference ) override; | ||
void NotifyEnd( USkeletalMeshComponent * mesh_component, UAnimSequenceBase * animation, const FAnimNotifyEventReference & event_reference ) override; | ||
|
||
protected: | ||
UFUNCTION( BlueprintNativeEvent ) | ||
UGBFAbilityInputBufferComponent * GetAbilityInputBufferComponent( const USkeletalMeshComponent * mesh_component ) const; | ||
|
||
private: | ||
UPROPERTY( EditAnywhere ) | ||
ETriggerPriority TriggerPriority; | ||
UPROPERTY( EditAnywhere, Meta = ( Categories = "Input" ) ) | ||
FGameplayTagContainer InputTagsToCheck; | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the tick would only be fired #if !UE_BUILD_SHIPPING, you can surround the function by that too, both in .h and cpp