- Home /
C# Weapon class generation
Hello everybody, i am working on learning about classes and such, and ive tried to make my own. using UnityEngine; public class Weapons { private WeaponTypes TypeOfWeapon; private Rarity howRare; private WeaponKind KindOfWeapon; private int Range;
public Weapons(){
TypeOfWeapon = WeaponTypes.Melee;
howRare = Rarity.Common;
KindOfWeapon = WeaponKind.Axe;
}
}
public enum WeaponTypes{
Melee,
Ranged,
Magic
}
public enum WeaponKind{
Sword,
Axe,
Bow,
CrossBow,
Wand,
Staff
}
public enum Rarity{
Common,
UnCommon,
Rare,
UberRare
}
and it is fine for now. my question is : I want to make a weapon be generated from these information. How would i do? would i make a different script to do so? would i make a script that inherits from this class for each weapon kind i want? and attach it on script? also, how would i go around attaching diffrent models for each weaponkind enum?
Thanks
Answer by YoungDeveloper · Aug 12, 2013 at 05:35 PM
This randomly generates the item.
public WeaponType weapon;
public WeaponKind kind;
public Rarity rarity;
void Start(){
weapon = GetRandomEnum<WeaponType>();
kind = GetRandomEnum<WeaponKind>();
rarity = GetRandomEnum<Rarity>();
}
There are very many ways of how could you manage your scripts. This is where you have to think yourself, how you will manage your game system. Simplest way is to include everything for each item, all enums, classes etc. Maybe there could be one class (script) with all the information about what the weapon/item could be, and in each item script just get that script as component and generate the randomness.
I made them public so you could see them in inspector.
Ok thanks for the answer, it is helpful. Could you tell me how i would go about asigning a model to it? And make a diffrent script with stats to it? dmg and such?
I would create script for each weapon type separately, like one for melee, one for magic staffs, shields, etc. With the code like this:
//One universal script for melee weapons
public string name;
public int damage; // or float, depends on your game
public int defense;
public int weight;
public int firedmg;
public int frostdmg;
//etc;
//Still could be useful, if you will have an inventory system, you could store each items size. So you could know, can you, or can you not pick it up.
public int size_x;
public int size_y;
private int DEFAULT = 0; //We need this because, not every weapon will have fire and other damage, right? So at the start we will give everything a zero, which will mean, it don't have that ability.
void Start(){
damage=defense=weight=firedmg=frostdmg=size_x=size_y = DEFAULT;
}
So from here, you are pretty much done, and you can easily add all the variables in the inspector, first attach the script to new object, and edit. Leaving zero where you don't want anything.
You can create a new weapon, by creating a new empty game object first, call it as you want, then attach the script and any model you want, and there you have it, a weapon with its own stats.
Thanks alot, but is there a way to do like this ? (pseudocode) if(weapon = melee){ if(kind = sword){ InstatiateOneOfThese3RandomSword$$anonymous$$odels; } }
Sure, as i mentioned in my first reply, you can also add type, for each script-weapon. So if you have type, you can check what kind of type is it.
So let's say if we have these enum functions for this weapon.
public GameObject model;
public enum Weapon$$anonymous$$ind{
Sword,
Axe,
Bow,
CrossBow,
Wand,
Staff
}
public enum Rarity{
Common,
UnCommon,
Rare,
UberRare
}
public Weapon$$anonymous$$ind kind;
public Rarity rarity;
//Then you assign them in the inspector
void Start(){
//And this is how you search for what kind is it
if(kind == Weapon$$anonymous$$ind.sword){
Debug.Log("Yep, It's a sword");
if(rarity == Rarity.Rare){
Debug.Log("It's a rare sword, Yay I'm rich!");
//Here goes anything you want.
//So by this you can find anything.
//You could create a function which searches for random 3d model, but this function should be seperate script because you will have to make a long list of 3d models, array of strings or gameobject, in this example i use string.
model = new GameObject( FindRandomSword() );
//so this will make a new GO called Dwarf_Axe
}
}
}
In other script..
string FindRandomSword(){
...random number a;
..bla
..bla
..bla
return array[a]; //which lets say is "Dwarf_Axe"
}
public WeaponTypes weapon;
public $$anonymous$$eleeWeapon$$anonymous$$ind kind;
public Rarity rarity;
GameObject weaponmodeluseAxeTier1;
public GameObject[] firstTierAxe = new GameObject[2];
public GameObject firstTier1Axe;
public GameObject firstTier2Axe;
static T GetRandomEnum<T>()
{
System.Array A = System.Enum.GetValues(typeof(T));
T V = (T)A.GetValue(UnityEngine.Random.Range(0,A.Length));
return V;
}
void Start(){
weapon = GetRandomEnum<WeaponTypes>();
if(weapon == WeaponTypes.$$anonymous$$elee)
{
kind = GetRandomEnum<$$anonymous$$eleeWeapon$$anonymous$$ind>();
if(kind == $$anonymous$$eleeWeapon$$anonymous$$ind.Axe){
GameObject weapon$$anonymous$$odel;
weaponmodeluseAxeTier1 = Random.Range(0, firstTierAxe.Length);;
weapon$$anonymous$$odel = Instantiate(weaponmodeluseAxeTier1,transform.position, transform.rotation) as GameObject;
Debug.Log("Yaya");
}
else
{
Debug.Log("snap");
}
}
else if(weapon == WeaponTypes.Ranged)
{
Debug.Log("sorry, RANGED");
//kind = GetRandomEnum<RangedWeapon$$anonymous$$ind>();
}
else if(weapon == WeaponTypes.$$anonymous$$agic)
{
Debug.Log("sorry, magic");
//kind = GetRandomEnum<$$anonymous$$agicWeapon$$anonymous$$ind>();
}
rarity = GetRandomEnum<Rarity>();
}
}
I have got this so far, could you show me what to do to get a random here maybe? i know i ask alot of questions, im sorry :i
Answer by Ouija · Aug 13, 2013 at 08:51 PM
If you download this project, you might find some scripts that interest you. It has a great weapons selection script you could learn from. http://www.youtube.com/watch?v=VgY6NNGfnlA
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How to rotate shader/texture? 1 Answer
Handling different combinations of enum selection 1 Answer
Creating a Loadout 1 Answer
Ragdoll script vs Extra ragdoll model 0 Answers