ID:141244
 
Code:
        medical_kit
icon = 'obj.dmi'
icon_state = "medical"
verb/use()
if (usr.HP > 149)
usr.HP = 150
usr<<"You are fully healed."
else
usr.HP +=50
del src
verb/get()
set src in oview(1)
src.loc = usr
if (usr.weight == 8)
usr<<"Cant hold anymore items"
src.loc = usr.loc
usr.weight = 8
else
usr.weight += 1
verb/drop()
src.loc = usr.loc
usr.weight -= 1


Problem description:

Okay assuming I already set variables for all of these I just want to know if theres a easier way I can do this. for the ability to only be able to carry 8 items. the code works perfectly but I'm a little confused on if i should have done it a less harder way. Any thoughts ^^ I'm kind of a newbie so any helpful advice would help out a lot.

You could check usr.contents.len , this returns the amount of items in the user's inventory. so if(usr.contents.len>=8) and stuff.
If weight is going to be 1 for every object and you're just keeping track of how many items the player has, just use contents.len as Mysame said, to keep track automatically and check to see if the player has room.

If weight is going to differ between objects, then I prefer to use Entered()/Exited() to adjust the weight, and Enter() to make sure that they can carry more.

obj/var
weight = 1

obj/Entered(mob/M)
if(ismob(M))
M.weight += src.weight

obj/Exited(mob/M)
if(ismob(M))
M.weight -= src.weight

obj/Enter(mob/M)
if(ismob(M))
if((M.weight + src.weight) > M.max_weight)
return 0
..()


mob/var
weight = 0
max_weight = 40

mob/verb/Get(obj/O)
if(O.Move(src))
src << "You get [O]."
else
src << "You can't carry [O]."
Thanks this shines some light on easier methods I'll try these out.
You should always use an object-oriented method like what Foomer posted (which is a nice approach making use of the movement system I also recommend), even if you do the item-limit processing only in the Get(),Drop() verbs, though this is less recommended as it applies less globally and is less robust. But those verbs should make use of OOP anyway - declaring a new get() and drop() for every item like you seem to do is very wasteful and tedious unneeded duplicate work. You just need to declare these once on a parent-type, and they will be shared by all subtypes.
For example:
obj
apple
verb/Get()
set src in oview(1)
if(src.Move(usr)) usr << "You pick up [src]."
verb/Eat()
usr << "The apple is yummy."
del src
orange
verb/Get()
set src in oview(1)
if(src.Move(usr)) usr << "You pick up [src]."
verb/Eat()
usr << "The orange is yummy."
del src
rock
verb/Get()
set src in oview(1)
if(src.Move(usr)) usr << "You pick up [src]."

This is very, very bad. There's lots of duplicated code, and say you wanted to change how a verb works (f.ex. change the Get() message to "You get [src]"), you'd need to change it 3+ times. Because it's repeated, the code is also more prone to errors.
Instead, if you make use of object inheritance, you only need to write each verb a single time on a parent type, and it will be inherited by all child types of that parent:
obj/item
//all /obj/item descendants have the Get verb
verb/Get()
set src in oview(1)
if(src.Move(usr)) usr << "You pick up [src]."
edible
//all /obj/item/edible descendants have the Eat verb
verb/Eat()
usr << "[src] is yummy."
del src
apple
orange
rock

In the above code, all 3 items are defined under a common ancestor and share the same Get() verb, and 'apple' and 'orange' share the same Eat() verb of the /obj/item/edible parent type. Note this concept can apply to anything and not only verbs, but also vars and procs, and to any concept, such as an item-number limit or an item-weight limit. Do it once on an ancestor, define whatever needs to also have it under the ancestor.
In response to Foomer
You have some issues in the code - first, these series of procs (Enter(),Entered()...) are called on the actual location object that something e.g. entered, not the object that did the movement. Meaning they need to be defined on the actual item container (=mob in this case), not the other way around; so mob/Entered(obj/O) f.ex.
An in the end of both your Enter() and Exit() overrides, you forgot to return ..() (once you're only calling it) to make it use the result of the default or parent behavior.

In addition, I'll include an extra hint about this to make sure it's not overlooked by annyone (although the Reference mentions it, it's not often discussed); when an item is created inside a location, it does not actually Move() into it (or call Entered()), as it didn't move from anywhere - it started its existence inside it. Similarly, when an object is deleted, it doesn't actually exit the location where it was. So this means if an obj item was created inside a player, it will not affect his weight - neither will an obj that was deleted when it was in the player. So, using the Eat() example above, an eaten obj's weight will not be removed from the mob's weight, and he'll be stuck with it forever!
We can and should, of course, account for this by overriding the respective New() and Del() hooks. One method is to simply include the weight processing there:
mob/person/var/total_weight
obj/item
var/weight
New(mob/person/Loc)
//if the obj was created inside a [person] mob...
if(istype(Loc,/mob/person))
Loc.total_weight += src.weight
..() //<-- not _required_
Del()
//if the current loc of the obj, right before it's \
deleted, is a mob...

if(ismob(src.loc))
/*typecasting is needed as DM only knows 'loc'
is an atom, not some mob, so it doesn't know it has
a 'weight' var */

var/mob/person/M = src.loc
M.total_weight -= src.weight
..() //required to make the obj actually delete

Note something like the above doesn't actually prevent a mob from carrying beyond its limit because an obj was created inside it - but in such a case there's a number of choices you could make, (including do nothing special) so that is left up to the developer to decide. One approach could be to, whenever an item is created inside a mob which exceeds his capacity, have the item drop to the mob's turf/loc:
   New(mob/person/Loc)
//if the obj was created inside a [person] mob...
if(istype(Loc,/mob/person))
if(Loc.total_weight + src.weight > MAX_WEIGHT)
//(should use && here)
if(src.Move(Loc.loc))
src << "Whoops! You can't carry [src], so it dropped to [Loc.loc]!"
else Loc.total_weight += src.weight
..()

Note this already safeguards against the mob being off-map - in such a case Move(null) doesn't do anything; handy here to avoid losing the item.