- Home /
Weapon selection system
void Update () {
if(Input.GetAxis("Mouse ScrollWheel")<0) {
if (currentWeapon + 1 <= numWeapons){
currentWeapon++;
} else {
currentWeapon = 0;
}
SelectWeapon(currentWeapon);
Debug.Log("Subtracted");
}
else if(Input.GetAxis("Mouse ScrollWheel")>0) {
if (currentWeapon - 1 >= 0){
currentWeapon--;
} else {
currentWeapon = numWeapons;
}
SelectWeapon(currentWeapon);
Debug.Log("Added");
}
}
void SelectWeapon(int index) {
for (int i=0;i<transform.childCount;i++)
{
// Activate the selected weapon
if (i == index)
transform.GetChild(i).gameObject.SetActiveRecursively(true);
// Deactivate all other weapons
else
transform.GetChild(i).gameObject.SetActiveRecursively(false);
}
}
}
Ok so this is how I have my weapon selection set up. How would I make it so it can't switch to the weapon until it is picked up and equipped from my inventory. Would an array or list system work better? And how would I do that? I tried using them but I don't understand how to load the weapon once its in the array or list.
Answer by Bovine · Aug 19, 2011 at 04:24 PM
This is potentially quite a large question, so I'll try and briefly explain how we do things:
For our RPG we use a script that is serialisable and defines an Item. In the game world a similar script ItemInstance denotes an instance of an item, contains a reference to a concrete Item and any ancillary data such as if the weapon has charges or there are 5 of 10 (considering items that stack). The Item script itself contains detail such as the item type (i.e. a Mace) and what damage or armour value the item provides.
The Item itself has GameObject members that reference prefabs. For our game we instantiate the prefab to create the 3D object in the game world. We have a backpack which displays the icon representation by copying the script from the icon prefab for that Item (we use EZGUI so we actually copy the appearance of a disabled UIButton that's used for the icon image).
As I say this is quite a large topic, but you might have:
List<Weapon> PlayerWeapons = new List<Weapon>();
[System.Serializable]
public class Weapon
{
public string WeaponName;
public Texture WeaponIcon;
public GameObject WeaponGeom;
}
And then perhaps...
[System.Serializable]
public class WeaponInstance
{
public Weapon Weapon;
public int BulletsInClip;
public int BulletsRemaining;
}
You can either set this up as an array somewhere else i.e. on an ItemManager and copy the iterms, or you could create an ItemList that subclasses a ScriptableObject that contains a list of items in your world, i.e.:
public class ItemList : ScriptableObject
{
public List<Weapons> MasterWeaponList = new public List<Weapons>();
}
You then need to be able to create this as an assert by extending the editor and using AssetDatabase.CreateAsset(). But the former may be easier - it depends on how you wish to access the data/flexibility/number of weapons.
Sorry if this is a can of worms, but I'd definitely use a List or array of Weapons somewhere and I'd have a corresponding List of either copies of those weapons or an instance that references them.
In your code above, you could always find the child item by some string from a structure that defines the weapon and thereby show/hide the appropriate ones.
Naturally we have a LOT of weapons and other items in our RPG so our system needs to be a little more full featured. You could just have an array of bools for your system and skip the weapon if the bool isn't true - to indicate the player has it. It's not very extensible however and has no real relationship to weapons you might pick up. Also, where do you store your bullets for a given weapon?
I've had it coded very similar to how you explained but where I get stuck is how do I use the mouse wheel to scroll through them and how do I make them show up based on the list.
For my inventory I have int cnt = 0 and if(cnt < inventory.Count) then it draws a button. Would it be something similar to show my weapons or no? And if it is would I just increase or decrease the int I make with the mouse wheel? I'm a bit confused on the whole process.
Also, I don't have any bullets yet. I only have melee weapons at the moment.
Arrays tutorial:
http://msdn.microsoft.com/en-us/library/aa288453(v=vs.71).aspx
Generics:
http://msdn.microsoft.com/en-us/library/ms379564(v=vs.80).aspx
These are C# so if you need JavaScript you'll have tondo some digging yourself!
GL
Answer by Bovine · Aug 19, 2011 at 09:09 PM
It really depends on how you want it to work: if you have 5 weapons then you might have
bool[] have_weapon = new bool[5];
Your scroll wheel is doing ++ or -- on your current_weapon but you would be better defining a NextWeapon() and PreviousWeapon() which look through your have_weapon array starting at your current_weapon until it finds one. NextWeapon() and PreviousWeapon() could return the index of the new weapon. If that differs from the current_weapon (they may only have 1 weapon) then you can hide current_weapon (find the relevant child if doing things as above) and show the new weapon, finally setting the current_weapon to the index of the new weapon.
Don't forget to wrap your NextWeapon() and PreviousWeapon() searches so that they go either to the end or the beginning depending on the direction. They too should stop searching when the index matches the current_weapon.
Naturally if you have a more complex weapon list/inventory, then you'll need to use something a little more verbose than a bool.
For example:
void Update()
{
if(Input.GetAxis("Mouse ScrollWheel")<0)
{
int new_weap = PreviousWeapon();
if(new_weap != m_current_weapon)
{ SelectWeapon(new_weap);
}
}
else if(Input.GetAxis("Mouse ScrollWheel")>0)
{
int new_weap = NextWeapon();
if(new_weap != m_current_weapon)
{ SelectWeapon(new_weap);
}
}
}
int NextWeapon()
{ int ndx = m_current_weapon;
do
{ ++ndx;
if(ndx == m_max_weapons) ndx = 0;
if(m_have_weapon[ndx]) break;
}
while(ndx != m_current_weapon);
return ndx;
}
I'm sure you get the idea as to how PreviousWeapon() would work and its worth noting that if all your weapons are hidden, SelectWeapon() can hide the m_current_weapon, show the new_weapon and then set m_current_weapon to new_weapon - you don't need to run through all your weapon geometry.
I've moved this to another answer because Chrome was going berserk with the code blocks and it wasn't formatting correctly.
Cheers H
Oops didn't see this answer. Thanks, I will try this out
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Button showing when array is full 1 Answer
Array of Transform[] not correctly typed in Javascript 1 Answer
how to add list after removing some 1 Answer
Make a selection 0 Answers