ID:2071916
 
Code:
/obj/inventory
StarterShirt
name = "Blue Shirt"
icon = 'icons/player/blueshirt.dmi'
var/worn = 0
density = 0
Click()
if(src in usr.contents)
if(src.worn)
src.worn = 0
usr << "You remove the [src.name]."
usr.overlays -= 'icons/player/blueshirt.dmi'
src.suffix = ""
else
src.worn = 1
usr.overlays += 'icons/player/blueshirt.dmi'
usr << "You wear the [src.name]."
src.suffix += "{-Equipped-}"
verb/Drop()
set category=null
if(src:worn == 1)
usr << "Not while its being worn."
else
del(src)
verb/Get()
set src in oview(1)
Move(usr)
view(3)<<"[usr] picks up [src.name]."

/obj/inventory
StarterPants
name = "Black Pants"
icon = 'icons/player/pants.dmi'
var/worn = 0
density = 0
Click()
if(src in usr.contents)
if(src.worn)
src.worn = 0
var/custom = 'icons/player/pants.dmi'
custom += rgb(src.customred,src.customgreen,src.customblue)
usr.overlays -= custom
usr.overlays -= 'icons/player/pants.dmi'
usr << "You remove the [src.name]."
src.suffix = ""
else
src.worn = 1
usr.overlays += 'icons/player/pants.dmi'
usr << "You wear the [src.name]."
src.suffix += "{-Equipped-}"
verb/Drop()
set category=null
if(src:worn == 1)
usr << "Not while its being worn."
else
del(src)
verb/Get()
set src in oview(1)
Move(usr)
view(3)<<"[usr] picks up [src.name]."


Problem description:
how would i make i t so i can shorten these into a simple tree? and so i can add on items without having huge unnecessary lines of code like this. thank you for your time :D. i havent coded in a long time therefore im rusty.

POLYMORPHISM! The basic concept of polymorphism is that behavior and structure are inherited from the parent type to child types.

Embedding common behavior in root types is what you want to do. If you find yourself repeating code over and over again, it's probably because you missed the opportunity to create a common type somewhere along the way.

obj
item
verb //new class behavior
Drop()
set category=null
del src
return 1
Get()
set src in oview(1)
Move(usr)
view(3) << "[usr] picks up [name]."
return 1
equipment
var //new class structure
worn = 0
tmp/equip_appearance
equip_icon
equip_layer = FLOAT_LAYER
custom_color = "#FFFFFF"
proc //new class behavior
Equip(mob/m,force=0)
if(force||(!worn&&loc==m))
if(!equip_appearance) Recolor() //make sure appearance is cached
m.overlays += equip_appearance
suffix = "{-Equipped-}"
return 1
return 0

Unequip(mob/m,force=0)
if(force||(worn&&loc==m))
if(!equip_appearance) Recolor() //make sure appearance is cached
m.overlays -= equip_appearance
suffix = null
return 1
return 0

Recolor()
var/obj/o = new() //discard object
o.icon = equip_icon
o.layer = equip_layer
o.color = custom_color||"#FFFFFF"
if(worn&&equip_appearance)
var/mob/m = loc //unsafe cast
m.overlays -= equip_appearance
m.overlays += o.appearance
equip_appearance = o.appearance

Drop() //polymorphic override
if(worn)
usr << "Not while it is being worn!"
return 0
else
return ..() //supercall to parent verb

Click() //this is also a polymorphic override!
if(loc==usr)
if(worn)
if(Unequip(usr))
m << "You remove [src]."
else if(Equip(usr))
m << "You wear [src]."


With all that done, you can just derive from the equipment type by variation:

obj/item/equipment
StarterShirt
equip_icon = 'startershirt.dmi'
StarterPants
equip_icon = 'starterpants.dmi'


Polymorphic overrides can change as little or as much structure as you want, and can introduce all kinds of new behavior or structure if you want --or not.

Cheers!
thanks a bunch man! id buy you a beer if i could lol
what if i dont want specific items to have the verb, but i still want them to be under the tree of inventory?
i say this because some of the items are rare and i wont allow for a drop feature on them
could i use a call object? if so how? in the get and drop verbs
i say this because some of the items are rare and i wont allow for a drop feature on them

You can remove verbs from the verb list of an item's instance. The behavior still exists on the object, but the verb can be removed for the specific item instance.

To do this, our root prototype would need a few changes:

obj
item
var
rarity = 0
verb
Drop() //let's make this double-safe by changing the drop logic
set category=null
if(!rarity)
del src //you should probably never do this
return 1
return 0

New()
if(rarity) //this might need adjustment
verbs -= /obj/item/verb/Drop
//..() //you might need a supercall here


Then when defining an object that you don't want to be droppable down the line, you can just set the rarity to 1.
One thing I recommend strongly is not to have a worn var. Most often, you'll want equipment to have a specific slot it goes with. All you need to do to determine if the equipement is worn is to check its loc and, if it's a mob, look to see if it's in the equipment list.

mob
var/list/equipment

obj/item
var/slot // anything that can be equipped has a slot

proc/Equip()
var/mob/M = loc
var/obj/item/old
if(!slot || !istype(M)) return
if(!M.equipment) M.equipment = new // create list as needed
old = M.equipment[slot]
if(old)
if(old == src) return 1
if(!old.Unequip(1)) return 0
M.equipment[slot] = src
// set overlays here
// ...
return 1

proc/Unequip(auto) // the auto var might control whether you show any output
var/mob/M = loc
if(!slot || !istype(M) || !M.equipment || M.equipment[slot] != src) return
// return 0 here if the item can't be removed, e.g. cursed
M.equipment -= slot
if(!M.equipment.len) M.equipment = null // recycle list
// set overlays here
// ...
return 1

The main reason a worn var should be avoided is that it's subject to weird behavior if for any reason the object gets separated from its owner. It's also a low-information-density var, because it's telling you whether an item is equipped rather than how. Looking at loc and checking its equipment list will tell you how, and if you have that, then the worn var is totally redundant.
^strong agreement.

I was more focused on getting rid of the icon operation, but yeah, there are a good number of ways that the approach could be improved in general.