Gameplay Ability System introduction (1)- Initial setup

Unreal four Gameplay Ability System Introduction (2)- Common attacks

Gameplay Ability System (3)-UI interaction

Gameplay Ability System introduction (4)- Cure effect

The purpose of this chapter is to realize the interaction between Health and UI, that is, when the character’s Health changes, the UI’s Health bar will also change accordingly. To achieve this, we need to take the following steps:

  1. Initializes the AttributeSet. The value of the character’s attribute was previously uninitialized and starts at 0
  2. Through a Delegate, combine HealthAttribute with the CharacterBase. CPP function bind, which handles life changes.
  3. Implement the health UI for the character.

OK, let’s get down to business.

Initialize the AttributeSet

Personally, I think there are two better ways to initialize Attributes, one is through Gameplay Effect, BeginPlay initializes the attribute value of the role in the role blueprint. The second is to initialize property values in the Ability System Component via the Datatable.

Try the first method first.

Create a New Gameplay Effect named GE_AttributeDefault. Set the Modifier as follows for Override Health and change it to 100.

Passed in beginplay ApplyGameplayEffectToSelf initialization.

And then there’s the second way.

The second method initializes the set of attributes through the DataTable

Structure selects AttributeMetaData

And then the data is

In the Ability System Component setting in the role blueprint, add attributes and DataTable.

Modify the previous test

Start the game, attack, and see that the character’s initial health has changed to 100.

Health changes are notified via a Delegate

Open Rider (feels much better than VS). Add delagte to CharacterBase. H. Then create a function that takes care of the Broadcast delegate.

DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FOnHealthChangeEvent, float, NewHealth);

UCLASS(a)class GAS_LEARN_API ACharacterBase : public ACharacter, public IAbilitySystemInterface
{
	/ /...
	// Attribute update function
	UPROPERTY(BlueprintAssignable, Category="Ability")
	FOnHealthChangeEvent HealthChangeEvent;
	
	void OnHealthAttributeChanged(const FOnAttributeChangeData& Data);
};
Copy the code

Open CharacterBase. CPP and implement function

void ACharacterBase::OnHealthAttributeChanged(const FOnAttributeChangeData& Data)
{
	HealthChangeEvent.Broadcast(Data.NewValue);
}
Copy the code

Attach OnHealthAttributeChanged Function in BeginPlay with Attribute Bind in AbilitySystem.

// Called when the game starts or when spawned
void ACharacterBase::BeginPlay(a)
{
	Super::BeginPlay(a);if(AbilitySystem)
	{
		AbilitySystem->GetGameplayAttributeValueChangeDelegate(UAttributeSetBase::GetHealthAttribute()).AddUObject(this, &ACharacterBase::OnHealthAttributeChanged); }}Copy the code

Implement the Health UI

Create a new Widget named W_HealthUI, then create a New ProgressBar named HealthBar, and then adjust the color and Percent to make it your favorite pink.

Then create a Custom Event to modify HealthBar Percent

Then open the role blueprint and add the Widget Component

Then bind Health Change to its own CustomEvent in BeginPlay to modify HealthBar Percent in HealthUI

To complete.

test

Run the game and attack the mannequin. You can see that the Health Attribute is interacting with the UI.