- Home /
I solved it myself
Saving different bullets in a magazine
Hello. I'm trying to learn Unity to finish a game that i have been working on, and in that game i want to have a gamemechanic, where both players have a magazine with a certain capacity, and if a player picks up a bullet-powerup the "buff" gets added in a List, where i can call it later in the reload Coroutine to put it as the current Magazine.
So i thought to myself that i could solve this by saving three values (the bullet type, the amount of shots and the startposition in the magazine) in a ListArray and then put that ListArray in another List which i called the "buffList".
For example:
if the player has a maxAmmo of 5,
and the values are: "explosiveBullet", 3, 0
then the output magazine would look like this:
explosiveShot
explosiveShot
explosiveShot
normalShot
normalShot
So that when i call the reload Coroutine i could just unpack the information from the buffList like shown above, and set it as the current magazine, and then delete the buff at the first position,
but even though Visual Studio says that there aren't any mistakes in my code, it doesn't seem to work . (What is happening right now is that it just fires the default bullet even when the player has picked up a powerup previously)
This is the code the powerups run when they add a buff in the buffList:
public void AddToBuffList()
{
ArrayList buffArray = new ArrayList(3);
buffArray.Add(nextBullets);
buffArray.Add(nextBulletAmount);
buffArray.Add(positionInClip);
nextBullets.RemoveRange(0, nextBullets.Count);
nextBulletAmount.RemoveRange(0, nextBulletAmount.Count);
positionInClip.RemoveRange(0, positionInClip.Count);
buffList.Add(buffArray);
}
And this is the code for calculating the magazine:
private void CalculateClip(in List<GameObject> bullet, in List<int> bulletAmount, in List<int> startPosition, out List<GameObject> ClipList)
{
List<GameObject> clip = new List<GameObject>();
for (int i = 0; i < maxAmmo; i++)
{
clip.Insert(i, normalBullet);
}
//DebugShowClip(clip);
if (bullet.Count > 0)
{
int[] starts = startPosition.ToArray();
int[] bulletAmountAr = bulletAmount.ToArray();
GameObject[] bulletArray = bullet.ToArray();
for (int item = 0; item < bulletArray.Length; item++)
{
for (int count = 0; count < bulletAmountAr[item]; count++)
{
GameObject[] clipAr = clip.ToArray();
if (clipAr[starts[item] + count] == normalBullet)
{
clip.RemoveAt(starts[item] + count);
clip.Insert(starts[item] + count, bulletArray[item]);
}
else
{
clip.Insert(starts[item] + count, bulletArray[item]);
}
}
}
}
DebugShowClip(clip);
ClipList = clip;
}
I should say that if there aren't any Inputs the function above just returns a mag filled with normal bullets.
And finally the code for reloading:
IEnumerator Reload()
{
enumOn = true;
if (buffList.Count == 0)
{
currentClip = CalculateClip(); // This returns the normal bullet Clip.
}
else
{
List<GameObject> bullet = new List<GameObject>();
List<int> amount = new List<int>();
List<int> startPos = new List<int>();
ArrayList bufflist0ar = buffList.ToArray()[0] as ArrayList;
foreach (var b in bufflist0ar[0] as List<GameObject>)
{
bullet.Add(b);
}
foreach (var a in bufflist0ar[1] as List<int>)
{
amount.Add(a);
}
foreach (var p in bufflist0ar[2] as List<int>)
{
startPos.Add(p);
}
buffList.Remove(buffList[0]);
List<GameObject> thisclip;
CalculateClip(bullet, amount, startPos, out thisclip);
//DebugShowClip(thisclip);
currentClip = thisclip;
}
yield return new WaitForSeconds(reloadTime);
ammo = maxAmmo;
enumOn = false;
}
I should also mention that i'm storing the values in a List so i can have multiple special shots in one mag. For example:
List<GameObject> bulletType = posion bullet, antidote bullet
List<int> bulletAmount = 1, 1
List<int> startPosition = 0, maxAmmo - 1
(This is not how i would write it, its just for the example)
So when put it in the CalculateClip(bulletType , bulletAmount, startPosition, out mag) function it should fire the poisonous bullet first, then a lot of normal bullets, and the last bullet from the mag should be the antidote bullet.
P.S. Sorry if the english is a bit broken at some points it's not my first language. Thanks for any help in advance, and if you have any questions feel free to ask.
"..saving three values (the bullet type, the amount of shots and the startposition in the magazine) in a ListArray.." why not make a class for that? class MagazineData{ ??? bulletType; int amountOfShots; int startPosition;}
@logicandchaos Yes that's how i ended up doing it and it works perfectly now. Thank you very much.
Follow this Question
Related Questions
how to reload 1 Answer
reloading? 1 Answer
reloading script problem 0 Answers
having trouble with ammo reload script. destorys extra ammo 1 Answer
How do I add ammo and reloads? 1 Answer