ID:576071
 
Keywords: randomdrops
(See the best response by A.T.H.K.)
Code:
mob/Creature/Enemies/Elf/DeathCheck()
if(prob(20))
new/obj/WoodSword(src.loc)
..()
if(prob(20))
new/obj/WoodShield(src.loc)
..()


How would i go about making monsters randomly drop things? The code I use above doesnt seem to be doing anything, and i'm new to coding so I have no clue what i'm doing wrong. Would somebody help point out my derp?

Best response
I think this is one of the best examples as it allows for a list of items with easy programming.

http://www.byond.com/forum/?post=173044#comment839477
In response to A.T.H.K
Thanks, this looks just like what i was looking for.
I am getting errors with that code, and i don't know how to fix them, sorry I just started coding.

Drops.dm:1:error: list: undefined var
Drops.dm:1:error: =: expected a constant expression
Drops.dm:9:warning: O: variable defined but not used
Yes well I said it was a good example :)

Copy and pasting things never work..

var/list/loot = list(
/obj/Weapon/Sword = 65, // 65% chance
/obj/Weapon/BiggerSword = 30, // 30% chance
)

mob/proc/Die()
for(var/a in loot) // For each loot item...
if(prob(loot[a])) // If the player gets lucky...
var/obj/O = new a // Make the item and drop it at the mob's location
O.loc = src.loc
del(src) // Kill the mob, or whatever custom procedure you have
I'm still having problems with them not dropping anything, it says no errors though. Am i supposed to add something else to make it work?
Sorry I made a mistake I missed the a after new, I have fixed the original code.
Still not dropping anything, My code looks exactly like that.
You do realise it won't drop an item all the time right?

Have you set up the objects in the list correctly? have you set icons for the objects?

How are you calling Die()
I'm calling Die() in my DeathCheck
    DeathCheck(var/mob/Killer)
if(src.HP<=0)
if(src.client)
world<<"[Killer] Killed [src]!"
src.HP=src.MaxHP
src.loc=locate(23,86,1)
else
Killer<<"<b>You Killed [src] for [src.Exp] Exp!"
Killer.Exp+=src.Exp
Killer.LevelCheck()
del src
Die()
RepopWorld()

I've set up icons and everything for the items i can use them, just not get them to drop.
this is my drop code
var/list/loot = list(
/obj/WoodSword = 50,
/obj/WoodShield = 50,
)

mob/proc/Die()
for(var/a in loot)
if(prob(loot[a]))
var/obj/O = new a
O.loc = src.loc
del(src)
I just looked at my code and realised i put Die() in after deleting the mob.... it drops now, thanks for the help :) How would i go about making each mob drop different things though?
mob/var/list/loot = list()

mob/Monster
loot = list(
/obj/Weapon/Sword = 65, // 65% chance
/obj/Weapon/BiggerSword = 30, // 30% chance
)

mob/proc/Die()
for(var/a in src.loot) // For each loot item...
if(prob(loot[a])) // If the player gets lucky...
var/obj/O = new a // Make the item and drop it at the mob's location
O.loc = src.loc
del(src) // Kill the mob, or whatever custom procedure you have
In response to A.T.H.K
Thanks :) this helped alot.