ID:2876114
 
(See the best response by Ter13.)
Code:
obj
destructable

var
list
contents_list = list() //The contents of the destructable
health //The current health of the destructable, is equal to max_health when created
max_health //The maximal health of the destructable

proc
destroy() //contents are created, destructable is deleted
src.contents_list = new(src.loc)
src.loc = locate(0, 0, 0) //I think this is right, del() is slow

check_health() //checks health, calls src.destroy() when health <= 0
if(src.health <= 0)
src.destroy()


Click()
src.health--
view(5) << "[usr] hits the [src]!"
src.check_health()
..()

New()
src.health = src.max_health
..()

wooden_box
name = "Wooden Box"
icon = 'wooden_items.dmi'
icon_state = "wood_box"
density = 1
opacity = 1
max_health = 3

pear_box

New()
contents_list += /obj/food/pear //adds a pear to the contents_list list
..()



Problem description:
I want to create a destructable pear_box object, which, when destroyed,
creates an instance of '/obj/food/pear' at its location. Instead of BYONDs
'contents' variable, I create a list at 'obj/destructable'. The code compiles and runs without errors, but when a pear_box is destroyed, it doesn't drop a pear. (Also if there's anything else wrong with this code please tell me)


Best response
couple tips:

src.loc = locate(0, 0, 0)

src.loc = null

locate(0,0,0) will always return null, so it is better to use null instead. It's less work and more clear.


this line is your main problem:

src.contents_list = new(src.loc)


this should work instead:
var/droptype
for(droptype in contents_list)
new droptype(src.loc)


You can't really create drops on the fly how you are trying to do it. You are actually creating a new list, instead of a new list of drops.

Now, I will add that it's probably better to not use a drop list like this. Since you are already doing a new override to add the pear to the drop list, let's just hijack your destroy hook:

obj/destructable
proc
destroy()
loc = null

obj/destructable/pear_box
destroy()
new/obj/food/pear(loc)
..()


Also, I notice you setting health to max health on creation. Instead of doing that, you can track health and damage. if(damage>health), destroy. I find that tracking damage instead of current health is less bug prone and requires less work for you.
In response to Ter13
It works now, thank you for your help!
I tried it, it resulted in a runtime error when the box broke:

runtime error: Cannot modify /obj/food/pear.loc.
proc name: destroy (/obj/destructable/proc/destroy)
usr: the o (/mob/player)
src: Wooden Box (/obj/destructable/wooden_box/pear_box)
usr.loc: the grass (11,20,1) (/turf/grass)
src.loc: the grass (10,19,1) (/turf/grass)
call stack:
Wooden Box (/obj/destructable/wooden_box/pear_box): destroy()
Wooden Box (/obj/destructable/wooden_box/pear_box): check health()
Wooden Box (/obj/destructable/wooden_box/pear_box): Click(the grass (10,19,1) (/turf/grass), "mapwindow.map", "icon-x=23;icon-y=23;left=1;but...")

I'll just stick to the version that works