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)
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:
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:
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.