Hello, Guys! Help me, please! (Index was outside the bounds of the array)

Zerokk

Official Terrarian
In my code there is an error related to arrays. Please help:

public void PlaceChest(int i, int j)
{
int startX = i - 10;
int startY = j - 11;

int number = 0;
while (number == 0)
{
int index = WorldGen.PlaceChest(startX + 7, startY + 1, (ushort)mod.TileType("IceChest"), false, 0);
Main.chest[index].item[1].SetDefaults(mod.ItemType("ArticDiggingPaw"));
Main.chest[index].item[1].stack = 1;
Main.chest[index].item[2].SetDefaults(mod.ItemType("ArticMolePaw"));
Main.chest[index].item[2].stack = 1;
Main.chest[index].item[3].SetDefaults(ItemID.LesserHealingPotion);
Main.chest[index].item[3].stack = Main.rand.Next(2, 8);
Main.chest[index].item[4].SetDefaults(ItemID.Rope);
Main.chest[index].item[4].stack = Main.rand.Next(50, 101);
Main.chest[index].item[5].SetDefaults(ItemID.Bomb);
Main.chest[index].item[5].stack = Main.rand.Next(8, 15);
number++;
}
Main.NewText("Ready");
}
 
If I were you, I'd start adding items at index 0. Also, why do you have a while loop with an integer while you could just use an if statement with a bool?
 
It is more convenient to use variables of type bool are not.
[doublepost=1474215246,1474215192][/doublepost]Oh, not really paying attention. Thank you.
[doublepost=1474215742][/doublepost]Lol, corrected on your suggestion, the error continues.
 
WorldGen.PlaceChest returns -1 if it failed to place the chest where you told it to. You'll need to loop and keep trying different locations until it succeeds then put items in it.
 
Back
Top Bottom