-
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 9 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
74 changes: 74 additions & 0 deletions
74
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,74 @@ | ||
#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 * aibc = GetAbilityInputBufferComponent( mesh_component ) ) | ||
{ | ||
aibc->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 * aibc = GetAbilityInputBufferComponent( mesh_component ) ) | ||
Frickerson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
aibc->StopMonitoring(); | ||
} | ||
} | ||
|
||
UGBFAbilityInputBufferComponent * UGBFAnimNotifyState_InputBuffer::GetAbilityInputBufferComponent_Implementation( const USkeletalMeshComponent * mesh_component ) const | ||
{ | ||
if ( mesh_component == nullptr ) | ||
{ | ||
return nullptr; | ||
} | ||
|
||
UGBFAbilityInputBufferComponent * aibc = nullptr; | ||
Frickerson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if ( const auto * owner = mesh_component->GetOwner() ) | ||
{ | ||
aibc = owner->FindComponentByClass< UGBFAbilityInputBufferComponent >(); | ||
|
||
if ( aibc != nullptr ) | ||
{ | ||
return aibc; | ||
} | ||
// Check all parent | ||
auto * parent = owner->GetParentActor(); | ||
while ( parent ) | ||
{ | ||
aibc = parent->FindComponentByClass< UGBFAbilityInputBufferComponent >(); | ||
if ( aibc != nullptr ) | ||
{ | ||
return aibc; | ||
} | ||
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 ) | ||
{ | ||
aibc = child->FindComponentByClass< UGBFAbilityInputBufferComponent >(); | ||
if ( aibc != nullptr ) | ||
{ | ||
return aibc; | ||
} | ||
} | ||
} | ||
|
||
UE_LOG( LogGBF, Error, TEXT( "No Ability Input Buffer Component found" ) ); | ||
return nullptr; | ||
} |
212 changes: 212 additions & 0 deletions
212
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,212 @@ | ||
#include "Characters/Components/GBFAbilityInputBufferComponent.h" | ||
|
||
#include "Characters/Components/GBFHeroComponent.h" | ||
#include "Characters/Components/GBFPawnExtensionComponent.h" | ||
#include "GAS/Components/GBFAbilitySystemComponent.h" | ||
#include "Input/GBFInputComponent.h" | ||
|
||
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(); | ||
} | ||
|
||
void UGBFAbilityInputBufferComponent::StopMonitoring() | ||
{ | ||
RemoveBinds(); | ||
TryToTriggerAbility(); | ||
Reset(); | ||
} | ||
|
||
void UGBFAbilityInputBufferComponent::Reset() | ||
{ | ||
TriggeredTags.Reset(); | ||
InputTagsToCheck.Reset(); | ||
BindHandles.Reset(); | ||
} | ||
|
||
void UGBFAbilityInputBufferComponent::BindActions() | ||
{ | ||
if ( InputTagsToCheck.IsEmpty() ) | ||
{ | ||
return; | ||
} | ||
|
||
auto * pawn = GetPawn< APawn >(); | ||
if ( pawn == nullptr ) | ||
{ | ||
return; | ||
} | ||
|
||
const auto * hero_component = UGBFHeroComponent::FindHeroComponent( pawn ); | ||
if ( hero_component == nullptr ) | ||
{ | ||
return; | ||
} | ||
|
||
for ( auto & input_config : hero_component->GetBoundActionsByInputconfig() ) | ||
{ | ||
auto * input_component = pawn->FindComponentByClass< UGBFInputComponent >(); | ||
Frickerson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if ( input_component == nullptr ) | ||
{ | ||
continue; | ||
} | ||
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 ( const auto * hero_component = UGBFHeroComponent::FindHeroComponent( pawn ) ) | ||
Frickerson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
if ( auto * input_component = pawn->FindComponentByClass< UGBFInputComponent >() ) | ||
Frickerson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
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 ); | ||
bool activated = asc->TryActivateAbilityByClass( ability->GetClass() ); | ||
Frickerson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if ( activated ) | ||
{ | ||
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.RemoveAll( | ||
Frickerson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
[ & ]( FGameplayTag & tag ) { | ||
return tag.MatchesTagExact( 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.RemoveAll( | ||
Frickerson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
[ & ]( FGameplayTag & tag ) { | ||
return tag.MatchesTagExact( tag_to_remove ); | ||
} ); | ||
triggered_tag_map.Add( count, tag_to_remove ); | ||
} | ||
|
||
// Get most triggered input | ||
int max = -1; | ||
for ( auto & i : triggered_tag_map ) | ||
Frickerson marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
if ( i.Key > max ) | ||
{ | ||
max = i.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; | ||
}; |
41 changes: 41 additions & 0 deletions
41
Source/GameBaseFramework/Public/Characters/Components/GBFAbilityInputBufferComponent.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,41 @@ | ||
#pragma once | ||
|
||
#include "Characters/Components/GBFPawnComponent.h" | ||
|
||
#include <CoreMinimal.h> | ||
|
||
#include "GBFAbilityInputBufferComponent.generated.h" | ||
|
||
UENUM( BlueprintType ) | ||
enum class ETriggerPriority : uint8 | ||
{ | ||
LastTriggeredInput = 0 UMETA( DisplayName = "Last Triggered Input" ), | ||
MostTriggeredInput = 1 UMETA( DisplayName = "Most Triggered Input" ) | ||
}; | ||
|
||
UCLASS( Blueprintable ) | ||
class GAMEBASEFRAMEWORK_API UGBFAbilityInputBufferComponent : public UPawnComponent | ||
{ | ||
GENERATED_BODY() | ||
public: | ||
UFUNCTION( BlueprintCallable ) | ||
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. UFUNCTION before other functions, except for constructor and inline functions |
||
void StartMonitoring( FGameplayTagContainer input_tags_to_check, ETriggerPriority trigger_priority ); | ||
|
||
UFUNCTION( BlueprintCallable ) | ||
void StopMonitoring(); | ||
|
||
protected: | ||
void Reset(); | ||
void BindActions(); | ||
void RemoveBinds(); | ||
void AbilityInputTagPressed( FGameplayTag input_tag ); | ||
bool TryToTriggerAbility(); | ||
FGameplayTag TryToGetInputTagWithPriority(); | ||
FGameplayTag GetLastTriggeredInput(); | ||
FGameplayTag GetMostTriggeredInput(); | ||
|
||
ETriggerPriority TriggerPriority; | ||
FGameplayTagContainer InputTagsToCheck; | ||
TArray< FGameplayTag > TriggeredTags; | ||
TArray< uint32 > BindHandles; | ||
}; |
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.
Uh oh!
There was an error while loading. Please reload this page.