Need help with getting random entry out of a DictionarySolved
TTRRust
Hi guys. I'm stuck. How do I get random string + ulong out of this Dictionary and add it to ItemManager? Any help would be highly appreciated. Thanks in advance. :)


Dictionary<string, ulong> item = new Dictionary<string, ulong>()
        {
        {"hoodie", 889718910},
        {"pants", 889714798},
        {"hat.boonie", 10058},
        {"mask.bandana", 889712013},
        {"burlap.gloves", 1106600389},
        {"shoes.boots", 920390242}
        };
        
        void OnEntitySpawned(BaseEntity entity)
        {
            int amount = 1;

            if (entity != null && entity is NPCPlayerCorpse)
            {
                PlayerCorpse corpse = entity.GetComponent<PlayerCorpse>();
                if (corpse._playerName == "Bob" && corpse.playerSteamID.ToString().StartsWith("76561198") != true)
                {
                    float value = Random.value;
                    if (value > 0.7f)
                    {
                        int rnd = Random.Range(0, item.Count);
                        var createitem = ItemManager.CreateByName(item[rnd].Key, amount, item[rnd].Value);
                        NextTick(() =>
                        {
                            createitem.MoveToContainer(corpse.containers[0]);
                        });
                    
                    }
                }
            }
        }​
2CHEVSKII
Plugin Developer
Use System.Linq and 
            Dictionary<string, ulong> item = new Dictionary<string, ulong>();
            var randomitem = item[item.Keys.ToList().GetRandom()];

Recommend you to make nullchecks when you get corpse component and also why to write "condition != true" when you can write "!condition"?

TTRRust
Original Poster
Thanks man. Got it working. And cause I didn't know you could do that. Thanks for the tip. :)