ID:1488102
 
(See the best response by Kaiochao.)
Problem description: Not really a problem, more of how do I situation. Say I have 10 items in a list and I want to give the player 5 random items. Would I have to assign the items a number in the list? Multiple variables? A loop maybe?

Code is just an example of what I mean, but only picking one item.
Code:
var/list/test = list(/obj/items/sword,
/obj/items/torch
/*etc,
etc*/
)

mob/verb/Test()
var/O = pick(test)
world<<"You recieve [O]"




Best response
A loop would be sufficient, I think.
//  Your list of stuff
var some_item_types[] = list(/obj/item/etc.)

// Repeat this code 5 times
for(var/n in 1 to 5)

// Pick a random type
var type = pick(some_item_types)

// Maybe you don't want to pick the same type more than once
some_item_types -= type

// Create a new item into src
new type (src)
Thanks once again Kaiochao. Quick question, what's the difference between using var/item[], var/list/item = list()? I could look this up but I'm about to go to bed.

Also is there anyway to reset a list? Say once it's looped 5 times, done its deed and reset back to launch state(not sure what to call it? Boot? Compile?).

Edit: Nevermind on the reset, I found a way using search.
I don't believe there is a difference. Both will initialize a list with zero elements.
There is a difference, his some_item_types variable isn't typecasted as a list, which is a dirty rookie mistake! =P It's not gonna hurt anything in the context of this code since it's still initialized as a list, but if he wanted to access the list procs for that list like list.Add(), list.Copy() and the sort he'd have to properly cast it.
It compiles... treats it as a list as well. However, the first example is not initialized, so there I was wrong :P

mob/verb/test()
var/A[] = new

A.Add(1,2,3)
for(var/X in A)
world << X


Try that out... Or you could do var/A[0] instead of using new to initialize it.
In response to Nadrew
The square brackets after the variable name actually does typecast it as a list variable. It also allows you to give it an initial length by simply putting a number inside.
var some_list[initial_length]

// vs. the more verbose
var/list/some_list = new /list (initial_length)
Oh right, totally forgot about that. But still, might not hurt to use /list anyways, so people reading over the code can tell at a glance what it is, since you're not really changing anything internally as far as overhead and dmb size. A little extra typing for easier to read code is always worth it in my book, especially if that extra bit of typing doesn't add any overhead.
In response to Nadrew
I know the syntax, though. And you should too! >:)