Solved List<string> error

Discussion in 'Rust Development' started by DylanSMR, Aug 20, 2016.

  1. So heres the code I am using currently:
    Code:
                void GiveAllItems()
                {
                    foreach(var player in BasePlayer.activePlayerList)
                    {
                        var createChance = Oxide.Core.Random.Range(0, 1);   
                        List<string> shorts = new List<string>();
                        for(var i = 0; i < happyData.hData[CurrentEvent].iData.Count; i++)
                        {
                            if(happyData.hData[CurrentEvent].iData[i].Rarity >= createChance) shorts.Add(happyData.hData[CurrentEvent].iData[i].ItemShortName);
                        }
                       
                        var iChance = Oxide.Core.Random.Range(1, shorts.Count);
                        var givenItem = shorts[iChance];
                        var amount = 0;                    for(var i = 0; i < happyData.hData[CurrentEvent].iData.Count; i++)
                        {
                            if(happyData.hData[CurrentEvent].iData[i].ItemShortName == givenItem) amount = happyData.hData[CurrentEvent].iData[i].Amount;
                        }                    var definition = FindItemDefinition(givenItem);
                        if (definition != null)
                        {
                            var item = ItemManager.Create(definition, amount);
                            if (item != null) player.inventory.GiveItem(item, player.inventory.containerMain);
                            SendReply(player, lang.GetMessage("GivenItem", this, player.UserIDString), amount, givenItem);
                        }
                    }       
                }
    You can see this code where the error is forming(Keeping in-mind there is something in that file upon it being called:
    Code:
    var givenItem = shorts[iChance];
    Code:
    [Oxide] 20:00 [Error] Failed to call hook 'Test' on plugin 'HappyHour v0.1.0' (ArgumentOutOfRangeException: Argument is out of range.
    Parameter name: index)
    [Oxide] 20:00 [Debug]   at System.Collections.Generic.List`1[System.String].get_Item (Int32 index) [0x00000] in <filename unknown>:0
      at Oxide.Plugins.HappyHour.GiveAllItems () [0x00000] in <filename unknown>:0
      at Oxide.Plugins.HappyHour.Test () [0x00000] in <filename unknown>:0
      at Oxide.Plugins.HappyHour.DirectCallHook (System.String name, System.Object& ret, System.Object[] args) [0x00000] in <filename unknown>:0
      at Oxide.Plugins.CSharpPlugin.InvokeMethod (HookMethod method, System.Object[] args) [0x00000] in <filename unknown>:0
      at Oxide.Core.Plugins.CSPlugin.OnCallHook (System.String name, System.Object[] args) [0x00000] in <filename unknown>:0
      at Oxide.Core.Plugins.Plugin.CallHook (System.String name, System.Object[] args) [0x00000] in <filename unknown>:0
     
  2. If it's empty, it will probably give you that exception. I just quickly looked over it, so I may be guessing wrong. Another thing you may want to change the Random Range to 0 instead of 1, since the index actually starts at 0.

    EDIT: You say there's something in the file, but just for the sake of it, try doing a foreach and print the contents of it and see if there actually is something there, or check the count. Otherwise, the error seems quite strange and I'm just not seeing it.
     
  3. Shady is on the ball, to expand on .what he said about using 0 as the first number in iChance, you also need to subtract 1 from the count. Since the first entry index is 0 the last entry index will be 1 less then the count. I know you already knew this though ;)
     

  4. Ahh, yes, that's it! I knew it was something simple, but looking at the code I couldn't quite get it, I was half way there, at least. ;)
     
  5. Thanks guys. I probably should have noticed the 1 instead of a 0 but it happens. :)