February 05, 2018

Ability Lua Tutorial 6: Chaos Knight's Chaos Bolt

Views: 859 Elfansoer

Chaos Knight's Chaos Bolt

Playing with projectile

EDIT: Some links have been updated due to github repository change.

Ability Form: Precache

I've been avoiding aesthetics up until now, but finally it's unavoidable anymore. If you look at the base .txt file, everything seems normal excluding that "precache" key-value.

Okay, here's the thing about precache: the engine is just pure lazy. It won't load any resources (such as particles and sounds) unless it is explicitly required. This 'precache' is a way to ensure the particle is required 'explicitly'. Spawning a hero is another one, but even the engine won't automatically load his/her voice until they're triggered to speak.
Particle not loaded means it won't show up. Got it?

Knowing a particle's path would require a Workshop Tools or a GCFScape, so if you don't want to be bothered by those, simply use the path I set there. The most essential particle is the "chaos_knight_chaos_bolt.vpcf" one; the rest is optional.

Logic: Tracking Projectile

That fuss about precache things is none other than creating a tracking projectile. After the spell is cast, Chaos Bolt releases a "bolt" which follows the target. The target may disjoint it, though most of the time it will hit. To spawn a tracking projectile, use this snippet:
Tracking Projectile

Like ApplyDamage(), the real function is at the bottom. ProjectileManager manages all projectiles (duh!), and it has a function named CreateTrackingProjectile() which takes a table as parameter. Here's the explanation, and default value is on bracket:

That's a long list, but you won't use them all most of the time. In fact, Chaos Bolt only uses these:
Chaos Knight Chaos Bolt Lua

Ability: OnProjectileHit

Okay, a projectile is thrown, what next? When the projectile connects, Chaos Bolt will stun and damages the target, so we'll do that. The engine will call OnProjectileHit() if the ability throws a projectile, and it connects. If dodged? Well, just forget it, you failed.
Chaos Knight Chaos Bolt Lua

This function will give you 2 contexts: hTarget is the unit which got struck by the projectile, and vLocation is the position where that unit is struck. How you use them is up to you.

Logic: Randomness

Chaos Knight, my favourite hero, is notorious for his randomness, and Chaos Bolt is one of this manifestation. Chaos Bolt will deal a random damage and stun for random duration. The note says that the damage and stun's randomness is inversely related. It means that if the bolt deals max damage, then the stun would be minimal. Let's get into math.


First, we'll throw a random number. A "math.random()" function would be sufficient to produce random number between 0 and 1, say it's x. We'll use this x for the damage.
As for the stun, we'll use 1-x instead, since it is inversely related (let's call it y).
Chaos Knight Chaos Bolt Lua
Now we get x and y, we'll Expand it.


It takes some algebra to 'alter' a random number between 0 and 1 into a random number between min_value and max_value. Believe or not, it is like this:
Chaos Knight Chaos Bolt Lua

This helper function will help interpolating (incorrect term, but fair enough) number so that it produces value between min and max according to value. Zero value returns minimum, one returns maximum, 0.5 returns value between them, and so on.
Chaos Knight Chaos Bolt Lua


We have determine the values, now apply them. Previous tutorial had described how to, so I'll give you the finalized version instead:
Chaos Knight Chaos Bolt Lua

As for the modifier, simply copy-paste from Fireblast's stun modifier, and repace the name of all Fireblast functions into Chaos Bolt.


I hope this may give you insight about projectiles, specially tracking projectiles. Next time, we'll use Slardar's Slithereen Crush to simulate AOE effects. Stay tuned.