ID:152976
 
I was reading chapter 10 in the dm guide,I found lists very hard and when wizkidd coded the curse seal and I tried to study it I still did not get it.I was wondering if somone was to explain this to me step by step.
Think of it lists like pieces of paper with bullet points on it, like a shopping list:

var/list/shopping = list("apples","oranges","bananas") //create a new list with information about fruits


Thats all you need to make a list.

Now lets say you remember something suddenly before going shopping, you'll have to add to the list:

var/list/shopping = list("apples","oranges","bananas")
shopping += "grapes"


So now lets say you're shopping and you look at your list and need to figure out how many things are in your list:

var/list/shopping = list("apples","oranges","bananas")
shopping += "grapes"
world << shopping.len //In this case it'll say 4 to the world.

//If you checked the len (the length) before adding grapes, it'd be 3.


Now you must decide which item to buy first:

var/list/shopping = list("apples","oranges","bananas")
shopping += "grapes"
world << shopping.len
var/buyfirst = pick(shopping) //picks something at random from the list
world << "Lets first go buy [buyfirst]"


Ok so lets say you found it on the shelf:

var/list/shopping = list("apples","oranges","bananas")
shopping += "grapes"
world << shopping.len
var/buyfirst = pick(shopping)
world << "Lets first go buy [buyfirst]"
shopping -= buyfirst //take away the thing we were going to buy first


Now you decide that you've run out of time and the only important things you need to get are located at the bottom and top of the list:

var/list/shopping = list("apples","oranges","bananas")
shopping += "grapes"
world << shopping.len
var/buyfirst = pick(shopping)
world << "Lets first go buy [buyfirst]"
shopping -= buyfirst
var/topoflist = shopping[1] //the 1 tells it the location of the item
var/bottomoflist = shopping[shopping.len] //the length of the list will also be the position of the last item


Now then, lets add everything we bought into our shopping trolley:

var/list/shopping = list("apples","oranges","bananas")
shopping += "grapes"
world << shopping.len
var/buyfirst = pick(shopping)
world << "Lets first go buy [buyfirst]"
shopping -= buyfirst
var/topoflist = shopping[1]
var/bottomoflist = shopping[shopping.len]
var/list/mytrolley = list(buyfirst,topoflist,bottomoflist)


And now at checkout, the worker needs to empty the trolley:

var/list/shopping = list("apples","oranges","bananas")
shopping += "grapes"
world << shopping.len
var/buyfirst = pick(shopping)
world << "Lets first go buy [buyfirst]"
shopping -= buyfirst
var/topoflist = shopping[1]
var/bottomoflist = shopping[shopping.len]
var/list/mytrolley = list(buyfirst,topoflist,bottomoflist)
world << "Thank you for buying:"
for(var/x in mytrolley) //create a variable x for every "item" in the list.
world << x //display x to the world.
del(mytrolley) // delete the list
del(shopping) //delete the list


Study all the different kind of things I've tried to show in one shopping adventure =P
In response to DeathAwaitsU
Hey whats does this ! do, I get confused everytime I see it.
In response to Broly103
Broly103 wrote:
Hey whats does this ! do, I get confused everytime I see it.

I recommend reading the section that I've written about the ! operator in my Hello Operator BYONDscape article. It should clear things up for you! I would actually recommend reading the entire article. =)
In response to Wizkidd0123
Also, you can sort of index lists ( The posted code doesn't really show indexing ):

Ex.

mob
var
chars[]
proc
addChars()
chars[ 1 ] = "Wizzkidd"
chars[ 2 ] = "Me"
chars[ 3 ] = "Audeuro"
/*
Then, you should be able to access each name with the indexing
operator [], just as in C++
( I use indexing for memory management, where I use the new memory's address as the index value,
and a pointer struct to the File, Line, and size of the allocation ( In C++ ) ).
*/

world << chars[ 1 ] // "Wizzkidd"
world << chars[ 2 ] // "Me"
world << chars[ 3 ] // "Audeuro"


Feel free to correct me if I made a mistake, or add something as you see fit.
In response to Audeuro
Audeuro wrote:
Also, you can sort of index lists ( The posted code doesn't really show indexing ):

DeathAwaitsU wrote:

Now then, lets add everything we bought into our shopping trolley:

var/list/shopping = list("apples","oranges","bananas")
shopping += "grapes"
world << shopping.len
var/buyfirst = pick(shopping)
world << "Lets first go buy [buyfirst]"
shopping -= buyfirst
var/topoflist = shopping[1]
var/bottomoflist = shopping[shopping.len]
var/list/mytrolley = list(buyfirst,topoflist,bottomoflist)


Notice the indexing at the third last and penultimate line.

But yeah yours was probably a better example. Perhaps not the most practical though.
In response to DeathAwaitsU
DeathAwaitsU wrote:
Audeuro wrote:
Also, you can sort of index lists ( The posted code doesn't really show indexing ):

DeathAwaitsU wrote:

Now then, lets add everything we bought into our shopping trolley:

> var/list/shopping = list("apples","oranges","bananas")
> shopping += "grapes"
> world << shopping.len
> var/buyfirst = pick(shopping)
> world << "Lets first go buy [buyfirst]"
> shopping -= buyfirst
> var/topoflist = shopping[1]
> var/bottomoflist = shopping[shopping.len]
> var/list/mytrolley = list(buyfirst,topoflist,bottomoflist)
>

Notice the indexing at the third last and penultimate line.

But yeah yours was probably a better example. Perhaps not the most practical though.

Ahh, didn't read your thoroughly, sorry. ( On an unrelated note, associative lists don't seem as useful in DM as they are in C++ to me )
Broly103 wrote:
I was reading chapter 10 in the dm guide,I found lists very hard and when wizkidd coded the curse seal and I tried to study it I still did not get it.I was wondering if somone was to explain this to me step by step.

Let's say we have a shopkeeper in our game:

mob/npc/shopkeeper


Our shopkeeper has items that he holds in his for_sale list:

Now, before continuing, let me explain something about lists.

When you define any variable, that variable is automatically initialized to null unless the programmer chooses to initialize it to something else:

//my_var starts out equal to null
var/my_var

//my_text_var will initially be equal to "foo"
var/my_text_var = "foo"


One basic way of initializing a list is to use the list() proc. While defining a list is equivalent to telling the program what the list is, initializing a list is equivalent to actually creating the list. Therefore, if lists were grocery bags:

//The knowledge of a specific grocery bag.
//This grocery bag does not yet actually
//exist as part of the world.
var/grocery_bag_a

//grocery_bag_b, on the other hand, is equivalent
//to an empty grocery bag:
var/grocery_bag_b = list()

//Finally, grocery_bag_c has a chicken in it
var/grocery_bag_c = list("chicken")


One good rule to remember in DM is that lists should only be initialized at the point when they're needed. Following that rule is a good way to save precious system resources.

Now, getting back to our shopkeeper:

mob/npc/shopkeeper
//I actually shouldn't be initializing for_sale
//yet because it isn't needed yet. However, for
//the sake of keeping our example small, I'll
//break that rule.
var/list/for_sale = list("Sword","Shield")


We run into problems here. When the player trys to buy an item, he'll need to know its price, right? In our current system, there's no really efficient way of doing that. Therefore, we'll decide to make for_sale an associative list in the format, item == price:

mob/npc/shopkeeper
//I actually shouldn't be initializing for_sale
//yet because it isn't needed yet. However, for
//the sake of keeping our example small, I'll
//break that rule.
var/list/for_sale = list("Sword" = 10,"Shield" = 15)


Now, if we want to access the prices, we can do this:

mob/npc/shopkeeper
//I actually shouldn't be initializing for_sale
//yet because it isn't needed yet. However, for
//the sake of keeping our example small, I'll
//break that rule.
var/list/for_sale = list("Sword" = 10,"Shield" = 15)
verb/Check_Prices()
set src in oview(1)
var/item = input(usr,"Which item's price do you want to check?","Check Prices") in for_sale
usr << "[item] costs [for_sale["[item]"]] gold."


You see, to access the value of an element in an associated list, you use the format, associated_list["element"]. Therefore, in our for_sale list, for_sale["Sword"] is equal to 10, and for_sale["Shield"] is equal to 15.

In atom.vars, the pre-defined associated list which I used in the other thread, the format is var = var_value. Try out the following example:

mob/verb/See_Vars()
for(var/element in src.vars)
src << "[element] == [vars["[element]"]]"
In response to Audeuro
Audeuro wrote:
( On an unrelated note, associative lists don't seem as useful in DM as they are in C++ to me )

You must not be making full use of them then:

Associative lists are one of the biggest time and resource-savers in DM. Very, very useful. I recommend reading Lummox's article on the subject.
In response to Wizkidd0123
Wizkidd0123 wrote:
Broly103 wrote:
I was reading chapter 10 in the dm guide,I found lists very hard and when wizkidd coded the curse seal and I tried to study it I still did not get it.I was wondering if somone was to explain this to me step by step.

Let's say we have a shopkeeper in our game:

mob/npc/shopkeeper

Our shopkeeper has items that he holds in his for_sale list:

Now, before continuing, let me explain something about lists.

When you define any variable, that variable is automatically initialized to null unless the programmer chooses to initialize it to something else:

> //my_var starts out equal to null
> var/my_var
>
> //my_text_var will initially be equal to "foo"
> var/my_text_var = "foo"
>

One basic way of initializing a list is to use the list() proc. While defining a list is equivalent to telling the program what the list is, initializing a list is equivalent to actually creating the list. Therefore, if lists were grocery bags:

> //The knowledge of a specific grocery bag.
> //This grocery bag does not yet actually
> //exist as part of the world.
> var/grocery_bag_a
>
> //grocery_bag_b, on the other hand, is equivalent
> //to an empty grocery bag:
> var/grocery_bag_b = list()
>
> //Finally, grocery_bag_c has a chicken in it
> var/grocery_bag_c = list("chicken")
>

One good rule to remember in DM is that lists should only be initialized at the point when they're needed. Following that rule is a good way to save precious system resources.

Now, getting back to our shopkeeper:

> mob/npc/shopkeeper
> //I actually shouldn't be initializing for_sale
> //yet because it isn't needed yet. However, for
> //the sake of keeping our example small, I'll
> //break that rule.
> var/list/for_sale = list("Sword","Shield")
>

We run into problems here. When the player trys to buy an item, he'll need to know its price, right? In our current system, there's no really efficient way of doing that. Therefore, we'll decide to make for_sale an associative list in the format, item == price:

> mob/npc/shopkeeper
> //I actually shouldn't be initializing for_sale
> //yet because it isn't needed yet. However, for
> //the sake of keeping our example small, I'll
> //break that rule.
> var/list/for_sale = list("Sword" = 10,"Shield" = 15)
>

Now, if we want to access the prices, we can do this:

> mob/npc/shopkeeper
> //I actually shouldn't be initializing for_sale
> //yet because it isn't needed yet. However, for
> //the sake of keeping our example small, I'll
> //break that rule.
> var/list/for_sale = list("Sword" = 10,"Shield" = 15)
> verb/Check_Prices()
> set src in oview(1)
> var/item = input(usr,"Which item's price do you want to check?","Check Prices") in for_sale
> usr << "[item] costs [for_sale["[item]"]] gold."
>

You see, to access the value of an element in an associated list, you use the format, associated_list["element"]. Therefore, in our for_sale list, for_sale["Sword"] is equal to 10, and for_sale["Shield"] is equal to 15.

In atom.vars, the pre-defined associated list which I used in the other thread, the format is var = var_value. Try out the following example:

> mob/verb/See_Vars()
> for(var/element in src.vars)
> src << "[element] == [vars["[element]"]]"
>


Its still a blur in my head.
In response to Audeuro
They are actually quite useful, I've seen more reason to use them in DM(game design, anyhow) than to use them in C++. Administration, shopping, banking, and grouping alike variables are just the tip of the iceburg on some of the reasons I've used them.

P.S.

Keep in mind, I've only just learned C++ and haven't any hands-on experience of writing programs of my own in it.
In response to SSJ2GohanDBGT
I was wondering if you would use them for learining new attacks and how do I do that,please an example not the full code so I can learn to do it myself for once :P.
In response to Broly103
How about no code at all? Pseudocode is a term that, if you havn't taken any formal education in computer programming, you may never have heard. The word-root "pseudo", in case you don't know, generally means "not quite". Pseudocode is like code, but lacks the specific nature of code that is actually compiled and run.

Pseudocode is often used by programmers who want to communicate ideas with other programmers who do not write using the same language. It is also used by programmers who want to document the flow of a program before they actually write the real code out.

Here is some pseudocode that might help you allow learning of new attacks using lists.



players have:
"attacks", a list
each element "attackname" has a value of attackdamage (a number)
on creation of a new player:
learn ("karate chop", 1)
procedure "learn" ("name",damage)
add "name" to list "attacks"
set list element "name" equal to damage
procedure "attack"
ask player "which attack do you use?"
player picks attack from list "attacks"
player's choice saved in variable "attack"
ask player "which target do you choose?"
player picks target from opponents in range
player's choice saved in variable "target"
reduce "target"'s health by "attack"'s value in list "attacks"
display to everyone, "target" is attacked!


The purpose of this code is to outline a general structure for code without going into the code's specifics. I hope that my pseudocode is understandable enough that you can visualize a system where players can learn new attacks. In your game you wouldn't want players to be able to type "learn SUPERCHOP 5000" and get an unrealistically good attack. However, for demonstration purposes, I think you can understand.

I challenge you to write BYOND code using the pseudocode I wrote above. If you need help with specific things, post your questions along the way. The application of lists is simple here, but simple applications will surely lead to better ones.
In response to Broly103
Well, it might be pretty confusing until you understand basic lists, but once you do, read over that post again!

For future reference, Broly, please don't use the "Reply with Quote" button unless you're uing it for a reason, and even if you are, omit all of the quoted post with the exception of the relevant material.
Rather than giving you a bunch of code, I'll just explain it.

Okay, here's the scoop on associative lists. An associative list is basically a big list of strings with a second tier of values tied to those strings.

You know how you access indexes of a normal list, right? With the position of the value you want? Isn't it a bit tough to keep track of where an object is, rather than being able to name every single value? You can.

A normal list would look like this:
var/list/list = list("red","blue","green")
usr << list[1] //outputs "red" to the user.


An associative list looks like this:
var/list/list = list("red"=rgb(255,0,0),"blue"=rgb(0,0,255),"green"=rgb(0,255,0))
//the list contains a number of colours and colour strings.
usr << list[1] //outputs "red" to the user
usr << list[list[1]] //outputs #FF0000 (rgb(255,0,0)) to the user.


How is this useful? It's useful in hundreds of ways! I can't touch upon hardly any of them! It's just useful for everything that you need precision and speed of access to.
In response to Wizkidd0123
I'm just use to pressing the quote button XD.

EDIT:I'm going to pause my game for now,I'm going to study the dm guide till july and begin there.Its better then just asking how to do something,then in the end someone gives me the code and I learn nothing but just copy and paste the code -_-.When I need help in something in the dm Ill be sure to come to you guys for help :), tommrow the journey begins :P.