1

Hi in my game I have an array of all guns included in the game. I also have an array of the available guns in the game. Currently I am looping through all the guns in the game and if they are unlocked I will add them to the available guns array. For some reason though I cannot figure out how to add a game object to an array.

public Gun[] guns;

public Gun[] availableGuns;

void Start(){

for (int i = 0; i < guns.Length; i++) {
            if ((GameDataManager.publicInstance.gunAvailability & 1 << i) == 1 << i) {
                availableGuns [i] = guns [i];
            }
        }

    }

so essentially I am having problems with this line of code:

  availableGuns [i] = guns [i];

Because It does not work as well as me not wanting the available gun to be in the same position it was in the guns array I just want it to be added onto the available guns array.

Note:

I did edit the guns[] in the inspector whereas I did not change the availableGuns[] at all in the inspector.

Peyton
  • 123
  • 1
  • 2
  • 8
  • Any reason you can't use generics instead, ie. List ? – scotru Dec 11 '16 at 08:41
  • well I would to prefer to keep it an array, but would it be the only way? I say this because I need to call upon this list from other scripts and if I do I will have to use System.Collections.Generic in every one @scotru – Peyton Dec 11 '16 at 08:42
  • no.............@Stefan – Peyton Dec 11 '16 at 08:46
  • No, but you'll essentially need to recreate the functionality provided by a List or an ArrayList. That is you'll need to keep track of the last element used in the Array. Array's have a fixed length set when they are declared and are not dynamic. So the length will be what you set it to when you create the array. See http://stackoverflow.com/questions/1056749/finding-the-last-index-of-an-array for finding the last used index. You may be able to use some LINQ extension methods here. – scotru Dec 11 '16 at 08:47
  • thanks a lot @scotru – Peyton Dec 11 '16 at 08:51

1 Answers1

3

Use System.Collections.Generic.List<Gun> instead of Gun[]

It gives you the ability to insert at a specific location.

List<Gun> availableGuns;
...
availableGuns = new List<Gun>();
availableGuns.Insert(gun[i], i);

If you want to keep using array:

for (int i = 0; i < guns.Length; i++) {
        if ((GameDataManager.publicInstance.gunAvailability & 1 << i) == 1 << i) {
             //shift from i to the end
             for(int j=guns.Length-2; j>=i; j--)
                  availableGuns[j+1] = availableGuns[j];

             //set the index i
             availableGuns[i] = guns[i];
         }
    }

}

Note that it does not automatically expand the size of the array.

If you want to use a limited number of guns for availableGuns:

int j = 0;
for (int i = 0; i < guns.Length; i++) {
        if ((GameDataManager.publicInstance.gunAvailability & 1 << i) == 1 << i) {
             availableGuns[j++] = guns[i];
         }
    }

}
Bizhan
  • 16,157
  • 9
  • 63
  • 101
  • would this really be the only way @Bijan as I would prefer to keep using an array? – Peyton Dec 11 '16 at 08:40
  • If you need to extend a collection, then ye List is the only rational way. You could also use an array with extra location. When adding you check whether you have empty slot and if no you would have to create a bigger array, copy all the items and so on. Or you can use a List. – Everts Dec 11 '16 at 08:56
  • I don't understand the down-vote and this is a valid answer. – Programmer Dec 11 '16 at 08:56
  • @Peyton If availableGuns array is not initialized with the length of Guns it surely throws index out of bounds exception. – Bizhan Dec 11 '16 at 09:00
  • wait would all array[ ] be the equivalent of availableGuns [ ] @Bijan – Peyton Dec 11 '16 at 09:03
  • I have initialized it – Peyton Dec 11 '16 at 09:03
  • only three? why need array then? just define three members. and I think your question needs more info. what if I unlock 4 guns? what happens to the first one? – Bizhan Dec 11 '16 at 09:04
  • I should not have said that it is irrelevant, I will create a bit flag that holds a max of three numbers at once later on so only three guns will be able to be added to availableGuns...So don't worry about that i shouldn't have said that..The user will be able to chose which of his unlocked guns to equip and add into the available guns array @Bijan – Peyton Dec 11 '16 at 09:08
  • Ok that's great. Then take a look at my final edit to the answer. – Bizhan Dec 11 '16 at 09:09
  • @Bijan is the j in your answer supposed to be an i? (the last answer), EDIT: oh wait nvm – Peyton Dec 11 '16 at 09:11