On-Kill/On-Hit effects: Signal train vs reference passing
Wanted some opinions on ways to set up triggering "on-kill" effects.
In short: The player can have abilities that trigger effects whenever they shoot and kill an enemy. I'm also looking to extend abilities to enemies as well, so can't just hardcode a player reference.
Currently I'm doing it like this:
Enemy signals that it died -> the bullet receives this and signals that it killed Enemy -> the player's weapon receives this and signals that it's bullet killed Enemy -> the player receives this and triggers it's on-kill abilities.
It simultaneously feels like a good way to go about it but also a long mess. The other option I've considered is:
Each bullet has a reference to it's owner (eg. player). When an enemy dies to a bullet, it looks to the bullet's owner reference and tells it to trigger it's on-kill effects.
This way is a lot simpler to write, but is error prone eg. In cases where the bullet's owner is killed while their attack is mid-air.
I have little experience with Godot, so I'll just write the general concept and you'll have to translate.
I'd go with #2, but also have the bullet subscribe to an event that is triggered when the shooter player dies. When that happens, set a private variable that tells the bullet that it shouldn't trigger any on-kill effects (or you could do other things like detonate or remove the bullet).
Something like that in C# (or close to C#, it's been a while):
class Bullet {
private Player _player = null;
private bool _enableOnDeathEffect = true;
private void OnPlayerDeath(object sender, EventArgs e) {
_enableOnDeathEffect = false;
}
public void OnKill(){
if (_enableOnDeathEffect) {
//trigger effect
}
}
public Bullet(player) {
_player = player;
player.OnDeath += OnPlayerDeath;
}
}
Thanks for your input! The other commenters pointed out that I can use Godot's is_instance_valid() function to check if the bullet's owner exists before attempting to call anything on it, so will be reworking the system to use #2 + that.