ID:267570
 
QUIET AT THE BACK!!! I know I'm posting to a newbie forum -_-.

Are there any demos that demonstrate how to use lists easily? I've never ever used them..... ever. I use for() as a bypass but I think using lists would be a useful thing to know.

I fiddle about with them but I always get some different kind of error....
If you always get some kind of error you're probably forgetting something like:
var/list/mylist = list()
Lists all have to be initialized. You can use list() like that or you can use new(). I don't think there's any difference between using list() and new() in that particular case. The closest thing to a list demo or tutorial that I know of is the lists chapter of the blue book. Here's a direct link to it: http://www.byond.com/docs/guide/chap10.html
In response to Jon88
Never looked at the blue book to be honest... I'm gonna get the print version at some point.
To quote Deadron:

A list is just a bag you can put stuff in. There are various ways to declare a list, but I will risk the wrath of Dan by suggesting you keep your life simple and use new() like so:

var/list/stuff = new()

Then you add things like so:

stuff += "a string"

stuff += my_object

And you remove things like so:

stuff -= my_object

You find stuff this way:

if (stuff.Find(my_object))

You iterate through it like so:

for (var/item in stuff)
world << "item = [item]"

That's a start!

Oh and you find out the length like this:

var/length = stuff.len


Back to me now,
Lists are defenitelly something you want to learn. Trying to program things without knowing how to use lists will hold you back. Lists are one of the most powerful functions BYOND has to offer, and you're missing out on a lot of possibilities by not knowing how to use them. But you should know that once you get the hang of how they work, they're really very easy. For me, I went from being list-ignorant like you to having lists as the aspect of DM programming I'm probably best at.
In response to Foomer
superb. thanx a lot.

list ignorant eh?
In response to Foomer
Foomer wrote:
Lists are defenitelly something you want to learn. Trying to program things without knowing how to use lists will hold you back. Lists are one of the most powerful functions BYOND has to offer, and you're missing out on a lot of possibilities by not knowing how to use them.

To take that further, you basically can't do any serious programming without using lists. It would be like building a house without using any nails...yeah you might be able to find clever ways to wrap a lot of string around things, but your house will never be as useful or solid as if you used nails.

Surprisingly, for all that, near as I can tell few people were using lists before I started with the system. The first major bug I found (kept me up til 5am figuring it out as I recall) was that lists were broken.
In response to Deadron
I've never ever had something I couldn't do without a list....

Mostly involved the use of:

for(var/M as mob in world:contents)

and then adding if statements everywhere....

If I need a list as text:

text = {"[text]
whatever I feel like adding"}
In response to Deadron
Lists are definently good in some places, but in like attack choosing during battle, I like to use alerts:

switch(input(alert("What do you want to use?","Fire","Ice")))

or, something like that...haven't used it in a while...
In response to Da_Rushyo
Da_Rushyo wrote:
I've never ever had something I couldn't do without a list....

Mostly involved the use of:

for(var/M as mob in world:contents)

This means you are using a list...world:contents (no idea why you use the colon there) is a list you are iterating through.
In response to Deadron
Leave the colon alone.... :P
In response to Deadron
Deadron wrote:
Surprisingly, for all that, near as I can tell few people were using lists before I started with the system. The first major bug I found (kept me up til 5am figuring it out as I recall) was that lists were broken.

Well that would explain why no one was using them.
In response to Da_Rushyo
Da_Rushyo wrote:
Leave the colon alone.... :P

No. This has to stop. It's going to create more problems for people seeking help when you advise them to use a colon where one is inappropriate. And that's pretty much everywhere, but you've managed to find cases that are even worse than that.

I can understand the impulse to use a colon inside, say, a Bump() when the type of something may not be readily known. Type casting is still the better option and still what should be advised, period. However, the temptation to use it makes sense there.

Where it doesn't make sense is after src (which always knows its type, unless you screw around by reassigning it) or world--or even after usr if the var belongs to a generic /mob type. These are not just inappropriate, but totally wrong. If you want to botch up your own code that way, you're perfectly welcome to, but do not advise people who come seeking help to do something that's going to cause worse trouble for them down the road, and do not post these one-liner huffy responses when they rightly criticize you for sharing sloppy code.

Lummox JR
In response to Foomer
Maybe you already said it but I overlooked it. I don't understand how I can choose from the obj's I put in a list. Say I have apple 1,2,3 in a list. And I want to Choose Apple 2 then change it's "value" (var) to 200. That's the part that I find confusing about lists...

Thank you for your help.
In response to SSChicken
<code>for (var/obj/apple/A in applelist) if (A.name=="Apple 2") A.value=200</code>

Edit: Eeek! Forgot to use the double equal sign there. Too much Delphi programming. Bad Crispy. =P

Checking the name like that is sort of dodgy (what if you decide on a different naming scheme? It gets broken, that's what) but you get the general idea. =)

If you've assigned a unique tag to the apple (as in, given it a unique value for its tag var) then you can use locate(), like so:

<code>var/obj/apple/A=locate("myspecialtag") in applelist A.value=200</code>
In response to SSChicken
Assuming the list only contained apples, you could modify it by using list[2], which would select the second item in the list, or the second apple.

If there are more item types in the list, then you'll have to sort out the ones you want. Like this:

<code>proc/GetType(list/list, type=/atom) var/list/typelist = list() for(var/I in list) if(istype(I, type)) typelist += I return typelist</code>

That proc will return a list containing all objects of the desired type from the list you selected. So if you want to seperate all /obj/apple types, you'd just use GetType(list, /obj/apple).

Now that you've got everything else seperated out, you can pick whichever apple you want using the method above, although you'll want to make sure that the list length supports that number before you use it.

<code>mob/verb/SelectApple(number as num) var/list/apples = GetType(src.contents, /obj/apple) if(apples.len >= number && number > 0) var/obj/apple/A = apples[number] src << "[A] is Apple [number]" else src << "Apple [number] doesn't exist!"</code>

In response to Foomer
O_O evil confusing type code
In response to Lummox JR
Fine I'll stop helping out on the forums too, whilst I'm at it why don't I just stop going on BYOND.

I think I'll do that.
In response to Deadron
that reminds me ive heard that same remark from garthor deadron, he said y do use te color, use a period instead, and when i do it comes up with an error, i use periods for everything except usr:verbs and world:contents
In response to Da_Rushyo
Da_Rushyo wrote:
Fine I'll stop helping out on the forums too, whilst I'm at it why don't I just stop going on BYOND.

I think I'll do that.

No one's asking you to give up BYOND or stop helping people. Just recognize that some things do more harm than good, and don't get in a snit when people repeatedly tell you so.

Lummox JR
In response to Da_Rushyo
Da_Rushyo wrote:
Fine I'll stop helping out on the forums too, whilst I'm at it why don't I just stop going on BYOND.

I think I'll do that.

Yea, because that's a heck of a lot easier than listening to people's advice. (I find it kind of funny that you are partially on the forums to help people, but you decide to "quit" when someone helps you)
Page: 1 2