ID:168382
 
Basically, my equip VERB is this:

obj
//blah blah icons and the like
verb
equip()
if(src in usr.contents)
if(usr.wearing)
return
else
usr.red = src.red
usr.green = src.green
usr.blue = src.blue
var/obj/clothes/robestop/T = new()
T.icon += rgb(usr.red,usr.green,usr.blue)
var/obj/robesbottom/B = new()
//B.icon += rgb(usr.red,usr.green,usr.blue)
usr.overlays += T //adding the overlay for the top icon
usr.overs.Add(T) //add it to the list for removal when saving
usr.overlays += B
usr.overs.Add(B)
usr.clothest = T //for removing the overlay
usr.clothesb = B


now, this works fine, however, I'd like to turn this into a proc so i can re-use the code, the only thing that would need changing is the "var/obj/clothes/robestop/T".

How can I turn it from a verb used on every item into a proc that i can re-use?

Thanks,

Farkas
REMOVED
In response to Zmadpeter
Yeah thanks, but I already have a working equip code. I'm just wondering how to turn it into a proc so I don't have to keep copy n pasting the same equip verb and changing one word.
In response to Zmadpeter
The equiped var here is wrong wrong wrong. First of all, it's misspelled. Second, it's redundant and can only lead to potential bugs. Just use ismob(loc), then use the equipsto var (equivalent to "slot" in some systems) to determine if it's already equipped. Items should never ever have a boolean var saying whether they're equipped, because 1) this is easy to find out in more reliable ways, and 2) any discrepancy could lead to some truly weird bugs. And it's one thing to use a suffix to show equipped status, another to rely on its value.

Also, why bother with a switch() when you can give equipsto the same name as the actual var? Then all you need is mob.vars[equipsto] to access the var. It's vastly simpler and makes the code easier to maintain.

Lummox JR
In response to Farkas
Farkas wrote:
Yeah thanks, but I already have a working equip code. I'm just wondering how to turn it into a proc so I don't have to keep copy n pasting the same equip verb and changing one word.

Aye, this is easy enough to do with procs. Let's say you're using a fairly simple equipment system with one var per item slot, like mob.weapon and mob.armor, etc.
mob
var/obj/item/weapon
var/obj/item/armor
// etc.

obj/item
var/slot

proc/_Equip(mob/M)
if(!M || !ismob(M) || !slot) return
if(M.vars[slot])
var/obj/item/I = M.vars[slot]
if(I==src || !I._Unequip(M)) return
M.vars[slot] = src
M << "You equip [src]."
suffix = "(equipped)"
return 1 // success

proc/_Unequip(mob/M)
if(!M || !ismob(M) || !slot) return
if(M.vars[slot]!=src) return
M.vars[slot] = null
M << "You unequip [src]."
suffix = null
return 1 // success

Now for a weapon, you'd use slot="weapon" and then a mob's weapon var would be set to whatever weapon was in use. Likewise if slot="armor", mob.armor will be set to the armor in use.

If you want a more complex equipment system that just uses a list and is more easily expandable, that's not hard at all:
mob
var/list/equipment

// returns null or an item
proc/ItemEquipped(slot)
if(equipment) return equipment[slot]

obj/item
var/slot

proc/_Equip(mob/M)
if(!M || !ismob(M) || !slot) return
// initialize the equipment list as needed
// otherwise make it null to conserve lists
if(!M.equipment) M.equipment = new
else if(M.equipment[slot])
var/obj/item/I = M.equipment[slot]
if(I==src || !I._Unequip(M)) return
M.equipment[slot] = src
M << "You equip [src]."
suffix = "(equipped)"
return 1 // success

proc/_Unequip(mob/M)
if(!M || !ismob(M) || !slot) return
if(!M.equipment || M.equipment[slot]!=src) return
M.equipment[slot] = null
// reset an empty list to null, to conserve lists
if(!M.equipment.len) M.equipment = null
M << "You unequip [src]."
suffix = null
return 1 // success

Lummox JR