ID:179040
 
i am starting to use lists, i am practicing with a small spells list and having problems with it. i am doing something that seems very simple but gives me errors and warnings. if u could tell me why it does this it would help much. it looks like this:

var/spells[4] //line126
spells[1]="fire" //line127
spells[2]="ice" //line128
spells[3]="wind" //line129
spells[4]="water" //line130

126::warning:use of spells precedes its definition
127:spells:warning:definition is here
128:error:spells:duplicate definition
129:error:spells:duplicate definition
130:error:spells:duplicate definition

this makes no sence to me, because this is the same way it is done in the help.why does it think i am defining spells in like 127 when i am clearly defining it in line 126? then why does it think i am also defining it in lines 128, 129, and 130?
Loduwijk wrote:
i am starting to use lists, i am practicing with a small spells list and having problems with it. i am doing something that seems very simple but gives me errors and warnings. if u could tell me why it does this it would help much. it looks like this:

var/spells[4] //line126
spells[1]="fire" //line127
spells[2]="ice" //line128
spells[3]="wind" //line129
spells[4]="water" //line130

126::warning:use of spells precedes its definition
127:spells:warning:definition is here
128:error:spells:duplicate definition
129:error:spells:duplicate definition
130:error:spells:duplicate definition

this makes no sence to me, because this is the same way it is done in the help.why does it think i am defining spells in like 127 when i am clearly defining it in line 126? then why does it think i am also defining it in lines 128, 129, and 130?

Question: Are you defining this var for an obj or mob, or is this within a proc? Within a proc, this form should work; otherwise, you should use this:
var/list/spells=list("fire","ice","wind","water")

Bear in mind that if you define an obj or mob's var as a particular list at compile time, all objs or mobs of that type will share the same list (that is, changes to one will change all of them).

Lummox JR
In response to Lummox JR
i cant seem to use lists properly, is there any tutorial or similar document that would teach me how to manipulate lists? it seems that lists are not taught well. in the turoials that i see that use lists they dont explain how to use them, they just put them in.
In response to Loduwijk
Loduwijk wrote:
i cant seem to use lists properly, is there any tutorial or similar document that would teach me how to manipulate lists? it seems that lists are not taught well. in the turoials that i see that use lists they dont explain how to use them, they just put them in.

I don't know of any list-specific tutorials, so I'll just try to go over the basics.

Lists in BYOND are very flexible: You don't have to declare a set size in advance, you can add to it (or subtract from it) as much as you like, and you can use items of any type (even mixed types) within the list. Arrays in most other languages aren't as forgiving.

For BYOND, like Java, a list is a special kind of data structure. Whenever you create a list, what you're really doing is telling BYOND to create the list and give you its "address". Every list var you have is a reference to a list, not the list itself. Think of it like the difference between owning a book, and having a card catalogue reference from a library where you can look in that book any time you want.

What does this mean for you? Well, basically, if you call a proc with a list var, that proc has a reference to the list, not a copy of it, and that means it can go and change the original list. For example, say you have a Sort() proc:
var/list/mylist=list("oranges","apples","bananas")

proc/Sort(list/L)
...

You could call Sort(mylist) and it would alter the contents of mylist. If that sounds a little confusing, think of it like this: If you send an obj to a proc, that proc can alter vars that belong to the obj like obj.HP and obj.icon. Same thing.

Now if you want for your list not to be altered, what you have to do is make a copy. In DM, there's a Copy() proc to do just that. That makes a brand new list identical to the original, but with a different address/reference; so if another proc changes the list's contents, it's really only changing a copy.

This is the most important thing to understand about lists.

Lists have indexes--numbers--for all their items. The first item in the list is list[1], the next is list[2], etc. Lists also have a length: list.len.

The next most important thing about BYOND lists is that you can associate items with other values. Just like you can do this:
list[index]=item

You can also do this:
list[item]=value

Each unique item in the list can have an associated value that you retrieve by using list[item]. (Exception: It won't work if the item is a number, because then DM thinks you mean an index.)

Lummox JR