Encapsulation

I was wondering why the Player script handles the OnFire() event rather than the Shooter script. Both scripts are components of the Player prefab and can therefore listen to the input system callbacks. If the OnFire() event is in the Shooter script then there is no need to have the public boolean isFiring and we have a looser coupling between Shooter and Player scripts. Both methods work, as far as I have tried. I am just interested if there is a benefit that I have missed in implementing the OnFire in the Player script.

Just thinking about this further I have realized that if we want to reuse the shooter script on the enemy then they need another mechanism to start and stop firing and will not use the OnFire() event…

Just answered my own question :slight_smile:

Hi RedSandman,

You are right. We want to reuse the Shooter script for other entities our game but the Players. However, if that hadn’t been the case, adding the OnFire() method to the Shooter class would have made sense as well. How we structure our project is often a matter of personal preference. Depending on the structure, some things become possible while others are not possible anymore. :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms