OnHit

This chapter is outdated, will be updated in the future.

Now our unit can use default attacks, but what if we want to make poison or flame effect when the Unit attacks someone? For that we have OnHit modules.

Logic module.

First let's create logic blueprint inherited from RTSOnHit (same we can do in C++, but let's keep it simple)
If we want this module to be used by other units, we most likely want it to create in common folder like Content\OnHit .

Image

Call it BP_OnHitPoison

Here our parent class provides some useful events which will be triggered in a certain conditions. All here we need is to use them.

Image

Short description from our code:

/** Function called by AI controller when damage caused during melee attacks */  
OnHitMelee(TArray<AActor*> OnHitActors, AActor* DamageCauser);

/** Function called by AI controller when projectile arrived at destination point  
and damage has been caused during ranged attacks */  
OnHitRanged(TArray<AActor*> OnHitActors, AActor* DamageCauser, ARTSProjectile* Projectile);

/** This function will be called in both cases after OnHitMelee and OnHitRanged */  
OnHitTarget(TArray<AActor*> OnHitActors, AActor* DamageCauser, ARTSProjectile* Projectile);

/* Function will be called when projectile spawned and configured*/  
OnSpawnProjectile(AActor* DamageCauser, ARTSProjectile* Projectile, AActor* TargetActor);

/** Called when projectile hits any actor in the air during it's traveling to destination point */  
OnProjectileScratch(TArray<AActor*> OnHitActors, AActor* DamageCauser, ARTSProjectile* Projectile);

So basically what we want here is get OnHitMelee event and do something with a target. As we see in the code, this event provides TArray<AActor*> OnHitActors which is an array of actors that our unit hits. That made because some units can damage many targets in one attack. But in most cases in this array will be just one target entity.

Since we don't want any exact number in the logic modules, we have here a function for retrieving data that will be passed to our module (about data itself a little bit later). The function is just named GetData.

Image

And here we can look for all stuff we can get from it.

Image

Right now we are interested to get damage parameter from it.
So in this blueprint, we iterate through an array and apply damage. And make it print something for debug purposes.

Image

Data module

Second, let's create a data asset for our poison module. We need to inherit it from RTSOnHitConfig. Just remember that we create not a blueprint but a data asset from Miscellaneous tab in the editor.
Create it in our Unit's folder, as data will be specific for only our unit

Image

Open it up.

Image

For now, we have here few fields, but we will add a lot more later. Set here name, and damage.
All those fields can be used in the OnHit logic module. Just ask GetData function.
And the main part is - we need to connect this data asset to logic module. In the field "On Hit Class" set our just created OnHit logic module. It is a blueprint in our case, but in other cases programmers will provide you the needed logic modules, so you might set here a C++ class.

So, OnHit is ready, let's add it to our weapon. Open up W_UnitSword and add new OnHit config into array.

Image

Here we can add as may OnHits as we want and all they will fire their events and do all logic in-game as unit attacks any target.

Now we can test it in game and see our prints on the display and damage in log.

Image