ID:157022
 
The topic pretty much says it all, i want to know how to add an item to the players inventory, for example, rat meat, that when you kill a rat would give you meat (i just need to know the command to put a item into 'usr.contents' got all the other stuff as far as i know...

Examples that i have tried.... (newbish mistakes most likely...)
mob
proc()
get_meat()
usr.contents << "Meat"
obj
loot
meat
icon = 'loot_meat.dmi'
name = "Meat"


I put it in a proc() so i wouldn't have to spend alot of time to copy and paste (At the library...) but in essence, that is the situation, hopefully someone can help, or point me in the right direction (i looked at the guide, mulitple times, no where does it say, how to send a obj, to a the 'usr.contents' or 'contents' list...

Thanks in advance.
1) Do not use usr in proc.

2) That is not how you define a procedure, there's no parenthesis after the proc. You would want
mob
proc
get_meat()


3) This would be much better as a verb:
obj
food
verb/Pickup() // All /obj/food types would have the pickup verb
src.Move(usr) // usr is acceptable here. Moves the food in to the usr's content

Chicken
Pie
// Both chicken and pie have the pickup verb


4) In your death procedure, you should set up a procedure that is called when the creature dies:
NPC/proc
Dead(Player/killer) // Lets say we call it after the NPC dies. If you plan to delete the killed NPC, maybe Del() would be more appropriate?
if(istype(killer)) // if the argument is the right type and exists:
killer.GainExp(max(1,src.level-killer.level)) // gives the exp as a difference between the levels, with 1 as the lowest
del src

NPC
Chicken
Dead(Player/Killer)
if(prob(10)) // 10% chance
new/obj/food/chicken(Killer?Killer : src.loc)
..() // Calls the parent procedure NPC/Dead() which deals with the EXP and deletes it afterwards
/*
X?Y:Z = A mini-if statement:
if(X)
Y
else
Z

So in the example above, if Killer exists, a food item is created in its content - if it doesn't exist, it'll be located on the ground
*/


Pie/Dead()
// No argument defined here because we don't want to give the EXP + food to the killer directly
// No ..() here = no exp
new/obj/food/pie(src.loc)
del src


As you can see, there's two ways to place an item to someone's content:

1) X.Move(Y)
This moves X in to Y's contents

2) new/path/X(Y)
This creates X (ex: /obj/food/pie) in Y's contents

3) (as a note, #2 is much useful and faster than this anyways - has the same effect)
Y.contents += X (or new/path/X)
This is if you wish to duplicate item X instead of moving it (if you want to keep the original in the same location).
** I am not too sure if += X works but the new/path/X should! Again, this is similar to #2
In response to GhostAnime
GhostAnime wrote:
1) Do not use usr in proc.

2) That is not how you define a procedure, there's no parenthesis after the proc. You would want
mob
> proc
> get_meat()

3) This would be much better as a verb:
obj
> food
> verb/Pickup() // All /obj/food types would have the pickup verb
> src.Move(usr) // usr is acceptable here. Moves the food in to the usr's content
>
> Chicken
> Pie
> // Both chicken and pie have the pickup verb

4) In your death procedure, you should set up a procedure that is called when the creature dies:
NPC/proc
> Dead(Player/killer) // Lets say we call it after the NPC dies. If you plan to delete the killed NPC, maybe Del() would be more appropriate?
> if(istype(killer)) // if the argument is the right type and exists:
> killer.GainExp(max(1,src.level-killer.level)) // gives the exp as a difference between the levels, with 1 as the lowest
> del src
>
> NPC
> Chicken
> Dead(Player/Killer)
> if(prob(10)) // 10% chance
> new/obj/food/chicken(Killer?Killer : src.loc)
> ..() // Calls the parent procedure NPC/Dead() which deals with the EXP and deletes it afterwards
> /*
> X?Y:Z = A mini-if statement:
> if(X)
> Y
> else
> Z
>
> So in the example above, if Killer exists, a food item is created in its content - if it doesn't exist, it'll be located on the ground
> */

>
> Pie/Dead()
> // No argument defined here because we don't want to give the EXP + food to the killer directly
> // No ..() here = no exp
> new/obj/food/pie(src.loc)
> del src

As you can see, there's two ways to place an item to someone's content:

1) X.Move(Y)
This moves X in to Y's contents

2) new/path/X(Y)
This creates X (ex: /obj/food/pie) in Y's contents

3) (as a note, #2 is much useful and faster than this anyways - has the same effect)
Y.contents += X (or new/path/X)
This is if you wish to duplicate item X instead of moving it (if you want to keep the original in the same location).
** I am not too sure if += X works but the new/path/X should! Again, this is similar to #2

Not trying to quibble over small details of a very good and helpful post, but personally, I would avoid using the :? operator when giving example code to (what appears to be) a newer coder.
I love :? and everything, but it's really just a shortut, and isn't the clearest code to read, and many newer coders aren't aware of it, or familiar with it, and teaching them nifty shortcuts takes second priority to actually answering their question in a clear way. (Maybe others will disagree. I certainly don't mean it as a harsh criticism, but I just felt I'd point it out)
In response to Chessmaster_19
I agree, I was thinking of doing an if...else for that part but I got lazy. But here's the expanded form of it, for anyone confused by the ? operator
NPC
Chicken
Dead(Player/Killer)
if(prob(10)) // 10% chance
if(istype(Killer))
new/obj/food/chicken(Killer)
else
new/obj/food/chicken(src.loc)
..() // Calls the parent procedure NPC/Dead() which deals with the EXP and deletes it afterwards


I noticed I forgot istype() in the ? snippet earlier... oh well.
In response to GhostAnime
Thank you very much Ghost anime, i was also, if directly to usr.contents didn't work, is to put iton the tile, the monster was at, so that makes it a bit easier, thanks.
In response to GhostAnime
By the Way, Ghost anime, this code won't work with something that drops multiple items, but incase someone has this problem, and sees this, iwant to post, how i found, to get multi items drops...

This code, is for a rat npc, to drop tail, meat, and bones (Example...)

mob
npc
proc
drop_loot_meat()
new/obj/loot/meat(src.loc)
proc
drop_loot_bones()
new/obj/loot/bones(src.loc)
proc
drop_loot_tail()
new/obj/loot/tail(src.loc)
rat
icon = 'npc_rat.dmi'
verb
attack()
set src in view(1)
health -= 10
view() << "You hit the Rat"
if(health <= 0)
drop_loot_meat()
drop_loot_tail()
drop_loot_bones()
del src
obj
loot
verb
Get()
set src in view(1)
src.Move(usr)
/* Basically the Pickup verb, that Ghostanime shown, but you need to make available to the player */
meat
name = "Meat"
icon = 'loot_meat.dmi'
bones
icon = 'loot_bones.dmi'
tail
icon = 'loot_tail.dmi'


Notice, no proc's for the Player mob, although, this is for a 100% chance, to drop all three items, because this is only an example, of how i did it.
In response to Wolfnova
If you noticed, the procedure I have shown in my sample has redefined from the default definition for NPC/Chicken and NPC/Pie, so that the loot dropped from the mentioned ones are not alike. Did you make sure to read the comments in it?

Using the method in my snippet, this is for multiple items:
NPC
Cyclops
Dead(mob/Killer) // we don't care if Killer exists here, it's for the default NPC/Dead() to worry about.
// Creating items where the Cyclops is
new/food/Cyclops_Eyes(src.loc)
new/potion/low_health(src.loc)
new/obj/food/Pie(src.loop) // Because creatures like this Cyclops also enjoy pie! LONG LIVE PI(E)!

..(Killer) // Calls the default procedure (NPC/Dead()) and sends Killer as the argument

In your method (which is not very efficient nor modular), you might as well replace the procedure with the create lines.