Technology Sharing

79. UE5 RPG Create Skill Cooldown and Consumption

2024-07-12

한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina

In this article, we will continue to optimize skills. Now the active skills added by the character can be synchronized to the UI. In this article, we will improve the skill consumption (releasing skills reduces mana) and cooling mechanism.
We can see that in the skill class default value, you can set its cooldown and consumed GE
insert image description here
So, next, we will use GE to set the cooldown and consumption of skills.

Added skill consumption GE

We first create a blueprint and select the parent class as Gameplay Effect
insert image description here
Name it GE_Cost_FireBolt, which means the cost of Fireball.
insert image description here
To consume our duration, just use Instant
insert image description here
Then we add a Modifiers to reduce the character's mana, reducing it by 20 each time a skill is used.
insert image description here
A simple consumption GE is made, and we apply it to the skill
insert image description here
Next, we need to call the Commit Ability node in the skill. This node will deduct the mana required for the skill. If there is a cooldown, it will put the skill into cooldown time.
insert image description here
Commit Ability is a unified node for deducting mana and cooling. Of course, you can also separate it and have separate nodes for deducting consumption and cooling.
insert image description here

Run the test results
insert image description here

Analyzing CommitAbility

First, we determine whether the skill will be activated if there is not enough mana. Before processing the consumption, we print a data
insert image description here
If the mana is sufficient, it can be printed successfully, but if the mana cannot support the release of the skill, it will not be printed. This proves that if there are not enough resources, the skill will not be entered, instead of being judged by Commit Ability. Commit Ability is just a node used to consume resources.
insert image description here
We look at the source code and find that the code it calls is K2_CommitAbility
insert image description here
CommitAbility is called to implement the logic, then check whether the consumption is enough to provide the skill, and call the function to deduct resources
insert image description here
CommExecute is a resource deduction function, which internally calls resource deduction and cooling
insert image description here
Look at the application cooldown, you will get the skill level and apply GE to ASC
insert image description here
The same is true for application resource consumption, which also obtains skill levels.
insert image description here

Set skill levels to consume different mana

Now, we know that the mana consumption can be modified by skill level. We can define the mana consumption of different levels through a table.
So we create a new curve table to set the mana consumption of skills at different levels.
Create a curve table, select Constant as the type, and set the mana consumption at each level to a fixed value
insert image description here
Name it CT_Cost
insert image description here
Added mana consumption for Fireball from level 1 to level 10 internally
insert image description here
Then use the curve table here in GE, select the row you created, and set the value to -1
insert image description here
Then modify the initial skill level in the code for testing
insert image description here
After releasing a skill, the mana is three times as much as before, proving that there is no problem with the settings.
insert image description here

Added skill cooldown

The principle of skill cooldown is to apply a cooldown tag to ASC during the cooldown phase. The skill cannot be used again during the duration of the cooldown tag.
So, if we want to implement cooling, we first create a label for cooling.

FGameplayTag Cooldown_Fire_FireBolt; //火球术冷却标签
  • 1

Then register it with the tag manager

	GameplayTags.Cooldown_Fire_FireBolt = UGameplayTagsManager::Get()
		.AddNativeGameplayTag(
			FName("Cooldown.Fire.FireBolt"),
			FString("火球术冷却标签")
			);
  • 1
  • 2
  • 3
  • 4
  • 5

Creates a Fireball cooldown GE
insert image description here
Modify GE to have time validity Has Duration
insert image description here
As you can see here, the cooling time can be set using the various methods we used to modify the values ​​​​before. Here we use the simplest one and directly set it to 1s
insert image description here
Here we don't need to modify the character-related content, we just need to add a cooldown tag to ASC when this GE takes effect. We add a Target Tags Gameplay Effect Component
insert image description here
Use this component to add a cooldown tag to the Actor and add the cooldown tag we created to it.
insert image description here
Then apply the cooldown in the skill
insert image description here
Running the test, we found that we can only release a skill once per second.

For character skills, we usually only generate one instance for one character. In order to prevent the client from turning off the skill before it is triggered, we will end the skill when the montage animation of the skill ends or the blend is forcibly canceled, and the montage can continue to play when the skill ends.
We also need to turn on the Retrigger Instanced Ability, which will turn off the previous trigger when it is triggered again.
insert image description here

Follow-up: Now we have consumption, and it can be correctly displayed on the UI, but the cooling has not yet been implemented. After using the skill, the skill UI cannot display the progress of the skill cooling. We will monitor the changes in cooling skills and display them on the UI in the future.