Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines

Creating a Chaining Shocking Ability for Weapons

Author: Matthew Chadwick

Date: 11/5/2023


Custom damage types in Unreal Engine can be extremely useful, but often require some setup to fully implement. Depending on the intricacies of the planned damage type, this setup can range from very complex to relatively simple. In this previous week, I implemented a shocking damage type that will chain to other nearby enemies. I ran into various issues while implementing this, but the biggest challenge was chaining the shocking effect to other enemies while limiting its spread and preventing it from chaining back to previously shocked targets. If these limiting factors would not in place, performance would drop significantly due to the large amount of shocking effects being spawned every frame. Ultimately, this issue resulted in crashes and a massive drop in frame rate.

To tackle the issue of infinite spread, I implemented a variable that keeps track of the "Charge" for the shock chain. Each charge count is specific to the individual chain of the shocking effect allowing each chain of it to be self contained. Whenever the effect begins, the charge is set to a certain amount, which was three in this instance. The charge is then paired with a timer that ticks every half second, during which the effect will also look for nearby enemies so it can spread. Every time the effect spreads, it starts the new resulting charge with one less charge than it currently has. So, for example, if an enemy is shocked with the effect's charge value at three, any resulting child shock effects created by its spread will be initialized with a charge value of two. After the effect applies its damage, it reduces the charge by one, resulting in the effect eventually being destroyed once it reaches zero.  This limited the effect's overall spread, which resulted in the performance remaining mostly unaffected. 


Now that the effect was working with a limited amount of charges to spread with, I wanted to prevent the waste of charges by preventing the effect from chaining back to an enemy that was already shocked by this chain of the effect. To do this, I created an array of actors inside of the shock effect's class. In the BeginPlay function of the effect, a pointer to the current effect instance is added to the array. Whenever the effect spreads to another enemy, the child effect's array is overridden by the parent effect, passing the record of previous recipients all the way down the chain. This, however, only kept track of the parent instance since the array is cloned to the child after the BeginPlay function has already been called. To remedy this issue, after the effect has found a valid target for spreading, that target is added to the list before actually starting the child effect. Adding the target to the array before spreading enabled an intact record of all previous recipients to be passed down the chain until the effect eventually concludes. This fix wasn't as impactful as the first fix, but it still resulted in a better experience when using the shocking effect since the charges were no longer wasted.

The flame particles are a placeholder since I didn't have an electricity effect at the time




Support this post

Did you like this post? Tell us

Leave a comment

Log in with your itch.io account to leave a comment.