This is a very simple question, what is the best way to handle different Guns from a scripting prospective. For example if I have a character with a pistol and a rifle, and the rifle can use iron sights but the pistol can’t, what is the best way to script this without using a metric-ton of bools**?** I thought about using events (or actions more then likely) to determine what happens when you, for example, push down the left-mouse button. But that leaves the issue of the fact that I would then need one script for every gun to set the events/actions, which is VERY inefficient. Any ideas? Thanks.
Depends on what you want to accomplish.
If your plan is to have many guns of shared types, having a base gun class, and inherited classes per gun type works well. You can put any shared behavior in the gun class (say, shooting or reloading), and any gun type specific behavior within each type’s class. For example, right clicking with the pistol can have code to melee, and for the rifles, scope in. If you want, say ten pistols, and six rifles, you can just use these 3 scripts for each one, just tweaking the member values in the inspector, or some kind of json file. If you want a rifle with special functions, say a grenade attachment, you can always inherit the rife class and adjust accordingly.
If your plan is for each gun to be very different from each other, then you’ll probably need a different script for each gun. This really shouldn’t be too inefficient, and is much easier to read and edit the code. I would still use a base gun class, and inherit it for any new gun I needed.
You should also consider breaking it down further with components. A pistol can have reloading, firing and pistol whip components. Where a rifle might have reloading, firing and scope components.