I am currently planning and working on a FPS project. My question is: What is the best way to manage information about weapons in my script. For example the accuracy, recoil, delay before you can fire again and so on. I was thinking about setting up multiple arrays, and use the array IDs as IDs for different weapons. So I would create an array with all my weapons, so the Element 0 would be the weapon of ID 0. Then I would create another array for my accuracy. And again, the Element 0 would be the accuracy for the weapon with the ID 0. Is this a good way to do this? Is there any other way. Can I make a new script for each weapon containing the variables? Any help will be appreciated!
vexe’s solution works, brilliantly, but may cause you to create empty classes because the extending class might not add any functionality. Additionally, when doing deep inheritance like this you need to keep the Liskov Substitution Principle in mind; especially if none of the classes are abstract. It prevents sometimes hair-pullingly strange bugs where gameplay breaks in seemingly random places.
It basically says don’t change the behavior of methods in subclasses. For example, if the Reload() of class Firearm only works correctly when the weapon is empty, then the SMG class should not override the method such that it works with a bullets left in the clip.
Depending on your project and its needs, a more favorable approach might be to apply Composition over Inheritance. Basically, you decide what behavior your weapons have as interfaces and then implement all the appropriate interfaces for your weapon classes. Basically something like this:
This way you will not have any classes just to support a hierarchy, only classes that are needed and do not really depend on any other classes. Of course this doesn’t exclude you from inheriting classes. You can of course have a base Firearm class that your firearms extend if all firearms truly share any (exactly the same)behavior.
You obviously don’t know about OOP. The way you’re approaching is really not the way to go.
I suggest you first take the time and learn OOP (what’s a class, inheritance, polymorphism, interfaces abstraction, virtualizim, etc and a good programming language well. C# preferably and then get back to Unity)
You basically need to make a “Weapon” class, and inherit all the other weapon types from it. Put the common stuff of all weapons inside that class, then you could branch to “FireArm” and “Melee” - I will share a simple design with you, have a look:
In your child classes, you override what you need from the parents, like Reload for example, because not all weapons reload in the same way, and also the UseAnimation which is dif in each weapon. Once you setup this whole system, you just attach the appropriate script to your weapon gameobject. You could also let your player have a list of weapons, then you can dynamically add/remove weapons.
Once you know OOP, you will clearly see the right path. Good luck.
So I learned about OOP. One thing I don’t understand is, how do I assign a weapon to a class? Like if I have a Pistol. How do I make it use the Pistol class?
What I currently have:
using UnityEngine;
using System.Collections;
public class Weapons : MonoBehaviour
{
}
public class Melee : Weapons
{
}
public class Firearm : Weapons
{
}
public class SemiAutomatic : Firearm
{
}
public class Automatic : Firearm
{
}
// SEMI-AUTOMATIC
public class Pistol : SemiAutomatic
{
}
// AUTOMATIC
public class Rifle : Automatic
{
}
OOP approach is not good alone. It has been explained several times before.
Use component based approach + OOP In unity.
Write health component,damage component, weapon component,etc