ID:266385
 
I use the following code to generate some trees in my program:
turf/Tree
icon = 'Trees.dmi'
icon_state = "Default"
proc/GenerateTrees(var/density = 1 as num)
var/placeX
var/placeY
var/placeZ
while(density)
density -= 1
placeX = rand(1,world.maxx)
placeY = rand(1,world.maxy)
placeZ = rand(1,world.maxz)
var/list/Trees = list()
var/Tree = new /turf/Tree (placeX,placeY,placeZ) // <-- Problem Line
Trees.Add(Tree)
var/Generate = (density+1)*10
while(Generate)
Generate -= 1
var/placer = rand(-6,6)
var/Xrand = rand(1,placer)
placeX += Xrand
if(placeX > world.maxx) placeX = world.maxx
var/Yrand = rand(1,placer)
placeY += Yrand
if(placeY > world.maxy) placeY = world.maxy
Tree = new /turf/Tree (placeX,placeY,placeZ)
placeX -= Xrand
placeY -= Yrand
Trees.Add(Tree)
for(var/turf/Tree/T in Trees)
var/chooser = rand(1,2)
T.icon_state = "[chooser]tree"


This spits a confusing bad loc error back at me:

runtime error: bad loc
proc name: GenerateTrees (/proc/GenerateTrees)
usr: null
src: null
call stack:
GenerateTrees(0)
: New()


Where did it get GenerateTrees(0)? It is called on world.New() as follows:

world/New()
..()
GenerateTrees(1)

Also, what does : New() mean?

-Lord of Water
What line is the error from??

And as a side note, unless you're using that proc as a verb, proc/GenerateTrees(density=1) should work just as well.
Hmmm this doesnt help much... but... have you tried watering your trees? This ancient technique has helped millions of people generate trees. You should try that.

-Raven-
In response to Foomer
I don't know what line the error is on. What I posted above is all it gave me. :-\

[Edit]

I defined DEBUG, and it gave me the line. I'll comment the problem line in the post above.
This is just a shot in the dark, but maybe it wants (locate(placeX, placeY, placeZ)).
In response to Cinnom
Hey, it worked! Thanks.
Just to explain the really unusual change in arguments, you're seeing GenerateTrees(0) because you decrement density before the bug happens. Since density is the argument to the proc, DS believes that 0 was the original argument.

Lummox JR
Also I'd place the New() under obj/
and not world.

LJR
In response to LordJR
LordJR wrote:
Also I'd place the New() under obj/
and not world.

I think the reason he's using world.New() is to set up the game map at startup. I do something similar myself, though I spawn it out.

Lummox JR
block() question.

I am using this as part of a generation code:
for(var/obj/Tree/T in block(locate(seed-2,up,1),locate(seed+2,up,1)))
del T

The problem is that the trees are not being deleted! Am I using block() wrong?
In response to Lord of Water
block() only returns turfs within that region. You'll have to loop through the turfs and filter their contents for obj/trees.