ID:158366
 
1.

var/list/whatever=("dude_man",/mob/cat)

What does the above do (not the code itself, it's meaningless)? It's not made on runtime; can I edit this list? I don't understand what the purpose of it is, unless it's only used for predefined lists never to be changed.

2.

var/whatever[number]=("dog_dude","rainbow",/obj/puzzle)

Ok, so what's the difference between this and number one (besides what's in the list)? The only difference I can see is that you can determine a maximum or minimum size limit, or pick from x to y.

3.

var/whatever[number]=new
&
var/list/whatever=new

Does the above just mean they can be changed on runtime?


4.

Is there any easy way to check if a list contains nothing?

if(!list) doesn't work, so I do a very tedious method of checking everything in the list and making a variable equal one or zero depending if anything was found, ex: for(var/a in list) that_var=1, and from there just check if "that_var" equals one or not. Surely this is unnecessary.

Thanks!
Speedro wrote:
1.

var/list/whatever=("dude_man",/mob/cat)

What does the above do (not the code itself, it's meaningless)? It's not made on runtime; can I edit this list? I don't understand what the purpose of it is, unless it's only used for predefined lists never to be changed.

Creates a list variable named "whatever" with two elements. The first element is the text string "dude_man", and the second element is the type path /mob/cat. The variable is not constant and so can be edited at runtime if you so desire.

2.

var/whatever[number]=("dog_dude","rainbow",/obj/puzzle)

Ok, so what's the difference between this and number one (besides what's in the list)? The only difference I can see is that you can determine a maximum or minimum size limit, or pick from x to y.

That would not compile. So, it's nothing. var/X[n] creates a list with n entries (all of which will initially be null).

3.

var/whatever[number]=new
&
var/list/whatever=new

Does the above just mean they can be changed on runtime?

The first one would not compile. The difference between var/list/whatever and var/list/whatever=new is that using new actually creates an empty list. Without the new, it would simply be null.

4.

Is there any easy way to check if a list contains nothing?

if(!list) doesn't work, so I do a very tedious method of checking everything in the list and making a variable equal one or zero depending if anything was found, ex: for(var/a in list) that_var=1, and from there just check if "that_var" equals one or not. Surely this is unnecessary.

if(!list) checks if the variable list has been initialized. To check if a list contains nothing, you have to check its len (length). A list is empty if its len is 0. Note that a list containing only null entries is not empty. So, if you want list(null,null,null) to be considered "empty", you'd need to check each element, like so:

var/E
for(E in list)
if(E)
//list contains a non-false entry
//do stuff
break



It might help if you looked up lists in the reference.
In response to Garthor
Garthor wrote:
3.

var/whatever[number]=new
&
var/list/whatever=new
The first one would not compile.

It actually compiles, it is pretty much valid syntax. However, Dream Maker catches the stupidity of the line and generates a "list initialized twice" warning (because it is).
In response to Kaioken
Oops, I had tried it out (because I figured the compiler wouldn't like the double definition), saw the warning, and assumed it was an error. Figured it wouldn't because the result is somewhat ambiguous. IE: does var/L[5]=new produce a list of length 5 or length 0?

Turns out it's 0. Which, I suppose isn't ambiguous (new is done after [5]), but is unlikely to be what a programmer wants.
In response to Garthor
Ok, thanks. One thing though:

Why don't things seem to be added to the list if I don't make it on runtime? ex: var/list/that=new works for adding and removing stuff from at my will, but without "new", if I, for example, check what's in the list, it will just spit out "/list" (or something of the sort)


What really is the difference- and purpose of:

var/list/container=new



and

var/list/container



What could I use each of the above for?
In response to Speedro
The second one isn't initialized, so you couldn't use it.
In response to Speedro
You cannot output the contents of a list with world << list. You have to loop through the contents of the list (for(var/v in list)) if you wish to act upon them.
In response to Garthor
So there's no difference between these?

var/list/container

and

var/list/container=new

In response to Speedro
There is a difference. The first declares a variable, whose value will be null. The second declares a variable and sets it equal to a new, empty list.
In response to Speedro
I was writing up a rather lengthy reply in response to your initial post in this topic yesterday, but due to certain annoying circumstances it was lost when I went to post it. In it I pointed out an important issue in your way of thinking:
You need to understand the difference between the list and the variable housing it. Both are completely separate and independent, additionally, both can exist without the other existing.
A DM variable containing a list is just like any other DM variable - it's not special. So just like any other var, it can be changed at runtime (note though that changing the list, e.g. adding an item to it, isn't actually changing the variable at all) - unless it was declared as a constant variable.

In the line "var/list/container=new", there are 2 parts. They can also be written separately:
var/list/container
container = new /list() //(the full form of new() )

The above is how that code is processed: first, create (=declare) a new variable named "container". This variable's object type is defined as /list - note this is a property of the variable itself, and at that it's not even a real property: it's a temporary one which only exists at runtime, and it's never strictly essential.
Mind you, the second line is also broken up into 2 steps: first, create a new list object, then, assign the variable "container"'s value to a reference to the newly-made list.

The line "var/list/container" contains only the first step, the creation of the variable. This line doesn't create any list, which means you couldn't work with a list using it alone (since there is no list).
In response to Garthor
Can they both be treated in the same in every way?
In response to Speedro
Obviously not, since the value of the resulting variable is different in each instance. Have you read my post?
In response to Kaioken
Ok, so from what I can understand:

var/list/container is just a variable that has the list property, and is otherwise useless?


Does that mean I should just use

var/list/container = new for everything? Since that actually makes the list work?
In response to Speedro
Speedro wrote:
var/list/container is just a variable that has the list property, and is otherwise useless?

Pretty much, except the var isn't useless, it's just "empty" - set to the default value of null, which represents 'nothing'. Like any var that's not initialized with a specific value, you can still use it later if you want.

Does that mean I should just use

var/list/container = new for everything?

Some of the time, but definitely not all of the time. The problem with doing that is that the list object is always initialized in that case, and this isn't wise to do if the variable in question is given to a broad type of objects, like in the below example:
turf
var/list/spots

Such a list should not be initialized in the definition itself, because giving every turf an initialized list is a huge, utter waste, not to mention probably most of those turfs won't use the list anyway. Instead, in such a case, the list should only be initialized when it's specifically needed (in the example, it should be initialized for individual turfs that use the list).