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]"
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, |
Feb 2 2014, 6:52 pm
Best response
|
|
A loop would be sufficient, I think.
|
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() 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] |
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! >:)
|