ID:1881862
 
(See the best response by Lummox JR.)
Im looking for a way to make this maybe a faster way to add to the list or maybe even cleaner in sense of a better form of coding. I dont know if there is a better way to do it but it kinda seems like alot of code just to add stuff to a grid/list

Code:
mob/verb/showpas()
usr << output(null,"passives.paslist1") // clear the grid.
for(var/obj/O in usr.CraftPassives)
usr << output(O,"passives.paslist1:1,[usr.CraftPassives.Find(O)]")

mob/var/list/CraftPassives = list()
mob/verb/Learnpassives()
var/obj/Passives/Crafting/Blacksmithing/Blacksmithing = locate() in usr.CraftPassives

Blacksmithing= new() // create a new fireball object
usr.CraftPassives.Add(Blacksmithing)




Best response
Looping through a list and then calling Find() on it is a really bad idea. Better to use a loop counter.

Also, sending null to the grid does not clear the grid; it only clears the current cell.

mob/verb/showpas()
var/list/L = usr.CraftPassives
var/i, len = (L ? L.len : 0)
for(i=1, i<=len, ++i)
usr << output(L[i],"paslist1:1,[i]")
winset(usr, "paslist1", "cells=[len]")

This is also a bad idea:

mob/var/list/CraftPassives = list()

Not all mobs will need this list; only players will. It doesn't have to be initialized until it's needed. Therefore, leave off the =list() and don't create the list till you need it.

// by making this a simple proc, you can call it for any passive type, simplifying your code
mob/proc/LearnPassive(passive_type)
if(locate(passive_type) in CraftPassives) return
// now initialize the list if it isn't there already
if(!CraftPassives) CraftPassives = list()
CraftPassives += new passive_type()
In response to Lummox JR
Lummox JR wrote:
// by making this a simple proc, you can call it for any passive type, simplifying your code
> mob/proc/LearnPassive(passive_type)
> if(locate(passive_type) in usr.CraftPassives) return
> // now initialize the list if it isn't there already
> if(!usr.CraftPassives) usr.CraftPassives = list()
> usr.CraftPassives += new passive_type()


Shouldn't he use src in his proc?
so is the passive_type() this part of the code?
var/obj/Passives/Crafting/Blacksmithing/Blacksmithing
or what do I do with it
In response to Mar big
Doh! Indeed he should; I changed it to a proc and forgot to remove usr.
In response to Mastergamerx
passive_type is not a proc; it's a type path, like /obj/Passives/Crafting/Blacksmithing. You would call LearnPassive() with that type path. This way you can reuse the proc for type like that.
So then it would be
+=new passive_type(Blacksmithing)
to call the specific passive right?
No. As I said, passive_type is not a proc. It's a type path, sent as an argument.

Like when you want to learn blacksmithing, you'd call mymob.LearnPassive(/obj/Passives/Crafting/Blacksmithing).

I don't know how you'd call the passive skill, because you didn't cover how you're using those. I just rewrote one specific verb into a general-use proc you can use for learning all your passive skills.
hmm i dont kno wuts wrong but before it was working but now it wont add the obj to the list

mob/var/list/CraftPassives


mob
verb
ShowPassives()
LearnPassive(/obj/passive_type/Blacksmithing)
var/list/L = usr.CraftPassives
var/i, len = (L ? L.len : 0)
for(i=1, i<=len, ++i)
usr << output(L[i],"paslist1:1,[L[i]]")
winset(src, "paslist1", "cells=[len]")

// by making this a simple proc, you can call it for any passive type, simplifying your code
mob/proc/LearnPassive(passive_type)
if(locate(passive_type) in CraftPassives) return
// now initialize the list if it isn't there already
if(!CraftPassives) CraftPassives = list()
CraftPassives += new passive_type()

obj
passive_type
icon='Passive.dmi'

Blacksmithing
icon_state="very common"
Won't add to the list or to the grid? That code looks like it should add it to the list just fine. I did simplify the output() calls by using just the grid name and not including the window name, so maybe your output isn't right. If that's the case though it would imply you have more than one control named paslist1.
to the grid i click the show pasive and it doesnt add it to the grid idk why before it was but nwt it doesnt
Ah, I did mess up one thing in my code example; I just edited in a fix. Change 1,[L[i]] to 1,[i].
welp i managed to alter the code in a way to get it to work it may not be as effective but it does cut out alot of work i was doing before