- Home /
Managing different weapons in scripting.
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!
Perhaps you could create a class called RangedWeapon with properties like accuracy, recoil, delay etc and attach that to the weapons
Answer by Jamora · Aug 13, 2013 at 12:58 PM
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.
really nice! - Really good use of interfaces. I actually shared him a part of the weapons, in my items system I'm working on for my game. I've used interfaces but not with weapons, they're really cool. But since my game isn't very much weapon-oriented like an FPS, the use of basic inheritance covered my weapon usage. If the OP could learn to reach a level where he understand your solution, it would be great!
Answer by vexe · Aug 13, 2013 at 11:58 AM
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.
The above solution is helpful, But I personally, wouldn't include the 'automatic' and 'semi-auto' classes. This should be considered 'properties of a type of weapon'...not a type itself. Be strict and stick to 'types of weapon'.
for what @daniel.mori said.
For anyone reading this looking for a system to copy directly into their game, definitely sticking to "types of weapon" is better than dividing into small properties like "semi-automatic" and "automatic."
I guess that @vexe was separating it to illustrate his point, and what a brilliant example he gave!
Answer by Coyote · Aug 18, 2013 at 11:41 PM
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
{
}
O$$anonymous$$, first you put the common stuff that all your weapons will share in your "Weapon" class, like damage, name, etc. Next what all the firearms will share in the "Firearm" class. Note you don't need to put name, damage, etc in the firearm, because it's inheriting from "Weapon", which means it gets to have its protected and public members. After you're done putting the right stuff in their right place, all you do is just assign for example your "Pistol" script to your pistol gameobject. For that you could make an empty gameObject, and have the weapon model a child to that object, and let the script be attached to it, make it a prefab if you like.
Answer by mahdiii · Jul 08, 2016 at 11:10 AM
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
Your answer
Follow this Question
Related Questions
FPS Gun Recoil (Accuracy) 1 Answer
How to make Recoil Dampening aka. Recentering? 0 Answers
Player Weapon Scripting 1 Answer
Creating an inventory/weapon array and ability to scroll through it 1 Answer
Firearm tutorial 1 Answer