Lena80

Member
Hello im back to some shard ''development' i was wondering how can i do the next thing:

How to randomize items from a list then give player a max of 8 items from that list?
Post automatically merged:

 
Last edited:
If available, linq is your best friend:

C#:
YourList.OrderBy(x => rnd.Next()).Take(8)

This also will ensure a max is reached but it might be less if the same item is selected more than once, which I think is what you wanted.

non-linq solution:

set up the method:

C#:
public static List<T> GetRandomElements<T>(this IEnumerable<T> list, int elementsCount)
{
    return list.OrderBy(arg => Guid.NewGuid()).Take(elementsCount).ToList();
}

Then send the list, and the count (8 in this example) to the method.
 
Ohh linq i completely forgot about that, first i need to know how to create a list xD, lets say a list of 15 differente baseweapons. then pick 8 randomly and give them to a player
 
This is a bit rough, but will get you going.

C#:
        var pack = player.Backpack();
        var seed = randomstring.GetHashCode();
        var random = new Random(seed);
        
        var types = new List<Type>() { typeof(Shoe), typeof(Cloak), typeof(Dagger), typeof(Sword), typeof(MagicStaff), etc... };
        
        var toGive = types.OrderBy(x => random.Next())Take(8).ToList();
        foreach (Type t in toGive)
        {
            Item item = (Item)Activator.CreateInstance(t);
            if (item != null) pack.DropItem(item);
        }
 
C#:
        var toGive = types.OrderBy(x => random.Next())Take(8).ToList();

that line is throwing and error



CS1002: Line 611: ; expected



C#:
var pack = m.Backpack();
        var seed = randomstring.GetHashCode();
        var random = new Random(seed);
        
        var types = new List<Type>() { typeof(Shoe), typeof(Cloak), typeof(Dagger), typeof(Sword), typeof(MagicStaff)};       
        var toGive = types.OrderBy(x => random.Next())Take(8).ToList();
        foreach (Type t in toGive)
        {
            Console.WriteLine("Debug line Type t in toGive");
            Item item = (Item)Activator.CreateInstance(t);
            if (item != null) pack.DropItem(item);
        }
 
Last edited:
thanks, its throwing 2 errors

CS1955: Line 606: Non-invocable member 'Server.Mobile.Backpack' cannot be us
ed like a method.
CS0103: Line 607: The name 'randomstring' does not exist in the current cont
ext

C#:
var pack = m.Backpack(); //line 606

        var seed = randomstring.GetHashCode(); /line 607
 

Active Shards

Donations

Total amount
$0.00
Goal
$1,000.00
Back