ID:154837
 
Ok I've spent all night destroying and rebuilding this and can't figure it out.

I want to create a list with a main category, and all the objects of that category would fall under that.

E.g.

POTION = list(potion, potion, potion)
COIN = list(coin, coin, coin, coin)

However this WOULD be simple given I could just make a single associative list, but I would really like to have it flexible since there are far too many items to just manually define them.

So if I have 10 potions, I will see potion x 10, and when I select potion, it will remove one of the potions from the potion list.

Here is one of many attempts:

    proc
Generate_Usable()
var/list/usable = list()

next_obj
for(var/obj/items/usable/u in m.contents)

for(var/obj/same in usable) //only add one primary branch.
world << "same = [same]"
if(same.type == u.type)
world << "already have [u] as a primary."
continue next_obj


var/list/count = list()

for(var/obj/o in m.contents)
if(u.type == o.type) //found another of the same, add it to the count
count += o

usable += u
world << "usable added another primary branch: [u.name]"
usable[u] = count


for(var/a in usable)
world << "a is [a]"
var/list_contents = usable[a]
for(var/b in usable[a])
world << " contains: [b]"


This appeared to work but it doesn't actually, since I can't take item_list[var] and use it as a list itself, since its a list inside a list... ffs.... idk... It's 5am and I literally spent all night doing this
Maybe this example with describe it better?

mob
verb
list_learning()
new/obj/items/usable/potion(src)
new/obj/items/usable/potion(src)
new/obj/items/usable/potion(src)
new/obj/items/usable/potion(src)
new/obj/items/usable/potion(src)

new/obj/items/usable/antipoison(src)

var
list
primary_list = new
secondary = new

for(var/obj/o in contents)
if(o.name in primary_list) continue
primary_list += o.name




for(var/obj/o in contents)
if(o.name in primary_list)
primary_list[o.name] += o


for(var/o in primary_list)
world << "Item: [primary_list[o]]. Amount: [primary_list[o].len]"
In response to Speedro
Speedro wrote:
>           for(var/obj/o in contents)
> if(o in primary_list) continue
> primary_list += o.name
>
>
>
>
> for(var/obj/o in contents)
> if(o.name in primary_list)
> primary_list[o.name] += o
>
>
> for(var/o in primary_list)
> world << "Item: [primary_list[o]]. Amount: [primary_list[o].len]"


This is what you are doing wrong. Here is an example of what you are looking for.

>           for(var/obj/o in contents)
> if(o.name in primary_list) continue
> primary_list[o] = 1 //This is automatic. using primary_list[o] creates it if it doesn't exist.
>
>
>
>
> for(var/obj/o in contents)
> if(o.name in primary_list)
> primary_list[o] += 1
>
>
> for(var/o in primary_list)
> world << "Item: [o]. Amount: [primary_list[o]]"


If you are confused, let me know where and I will explain.
In response to Albro1
Is there anyway to instead of just adding a number, have the real object inside the list?

Instead of

            for(var/obj/o in contents)
if(o.name in primary_list)
primary_list[o] += 1


Like this:

            for(var/obj/o in contents)
if(o.name in primary_list)
primary_list[o] += o


Obviously the latter won't work because I'm treating a name like a list, but how can this be done? That's what I'm struggling with.
In response to Speedro
Uhhh...
for(var/obj/o in src) // why are you using contents directly?
if(!(o.name in primary_list))
primary_list[o.name] = list()
primary_list[o.name] += o
In response to Kaiochao
Still struggling with this, how can I access that list?

    proc
Generate_Usable()

for(var/obj/items/usable/o in m.contents)
if(!(o.name in item_list))
item_list[o.name] = list()
item_list[o.name] += o

world << "item_list = [item_list[1]]"
for(var/obj/o in item_list[1])
world << "contains: [o]"
In response to Speedro
I'm not really understanding what you're trying to do. You're trying to make and use multi-dimensional lists, and you're having problems where?
In response to Kaiochao
Maybe text2path might help you here? just a suggestion.
In response to Speedro
Here you go, if you have any questions about this example feel free to ask.

mob/var/item_list[0]

mob
verb
Generate_Usable()
for(var/r in list("name1", "name2", "name3", "name4"))
if(!item_list[r])
item_list[r] = list()
for(var/e = 1 to rand(1, 5))
item_list[r] += pick("option1", "option2", "option3", "option4")

for(var/i in item_list)
world<<i
for(var/n in item_list[i])
world<<" - [n]"
In response to Zaltron
Yes this is what I was looking for; how to access the list inside the list.

            for(var/i in item_list)
world<<i
for(var/n in item_list[i])
world<<" - [n]"


Is that the only way to do it, through for() loops?

I was trying to do

var/list/contents_of_list = item_list[1]


Since item_list[1] contains a list, however this doesn't work. So the only way is for() loops?
Instead of using lists for every type, we could limit ourselves with 3 lists.

mob
verb
Generate_Usable()
var/list/List = new
var/list/Counter = new
var/list/Selection = new
for(var/obj/items/usable/u in src)
if(!(u.name in List))
List[u.name] = u // save first item only
Counter[u.name] = 1
else
Counter[u.name] += 1
for(var/uname in List)
Selection["[uname] x[Counter[uname]]"] = List[uname]
if(!Selection.len)
Selection+="- None -"
var/obj/items/usable/select = Selection[ input(src,"message","title") as null|anything in Selection ]
if(select)
src << "You selected: \c"
src << select // this is the saved first item


We are assuming that different types of usable items have their unique names and items won't be deleted or removed from the contents while waiting for input.
In response to Speedro
mob/var/item_list[0]

mob
verb
Generate_Usable()
for(var/r in list("name1", "name2", "name3", "name4"))
if(!item_list[r])
item_list[r] = list()
for(var/e = 1 to rand(1, 5))
item_list[r] += pick("option1", "option2", "option3", "option4")

for(var/i in item_list)
world<<i
for(var/n in item_list[i])
world<<" - [n]"

world<<"----------EX1-----------"
world<<item_list[1]//should print "name1"
var/list/L = item_list[item_list[1]]
world<<L[1]//Prints what ever is in item_list["name1"][1]

world<<"----------EX2-----------"
var/list/L2 = item_list["name1"]
world<<L2[1]//Prints what ever is in item_list["name1"][1]
In response to Zaltron
Oh, yeah. The item_list is a list of names associated to lists.
var L[] = list("fruit" = list("apple", "banana", "orange"), "vegetable" = list("spinach", "broccoli", "celery"))

// L["fruit"] = list("apple", "banana", "orange")
// L[1] = "fruit"
// L[L[1]] = L["fruit"]
// L["fruit"][1] = "apple"
// L[L[2]][2] = "broccoli"