Trying to use a loop to generate trees but I'm getting weird results

I have a blueprint that is supposed to be spawning a predetermined amount of trees in random locations but not overlapping one another. It spawns trees but it usually spawns the wrong amount, and, using debug sphere trace it is doing more work than it should be.
I’ll try to explain each screen shot as good as I can.

This event is supposed to get a random location and run a sphere trace to make sure the space isn’t occupied. If the area is occupied then it just tries again (that part is unfinished). If the area is clear then it places a tree.

This function should spawn a tree at the same location that passed my sphere trace.

I set the number of trees to 1 so the main loop should run until it spawns a single tree and then stops. I ran it three times in a row and these were the results.



As you can see it places between 1-4 trees AND you can see that there were sphere traces that came back empty which SHOULD mean it places a tree there.
What the fudge is going on here?!?

your loop condition is currentItteration < numberOfTrees

you set currentItteration to random int between 0 and lengthOfSomeArray

pretty self evident that that is going to produce some random results!

2 Likes

WOW.
Didn’t even realize I was using that same variable, I had made a second int variable specifically for that, and then I didn’t use it…

I’m glad you were able to solve that last problem so quickly but I still am getting one minor issue.
It still runs multiple sphere traces that do not spawn trees. Every sphere trace that comes back negative should spawn a tree but some do not, any idea why?

i don’t know what your tree parts A is about

i have never used instance static mesh component but from a quick look at it it seems you would have 1 instanced static mesh component which you keep calling “add instance” on to add more instances of the mesh so i don’t really understand why you have an array and are getting the Nth element of it

in short, i don’t know, but that is where i suspect the problem is.

I have multiple different tree meshes in an array, and also different sets of branches that go to each tree so it makes my trees much less repetitive.

As an experiment I got rid of the arrays and just plugged a single HISM into the ‘add instance’ but I get the same problem.

hmm maybe…

"Add an instance to this component. Transform is given in local space of this component. "

local space of this component.

your sphere trace is in world space and then you’re plugging that same location straight in to the instanced static mesh component which does things in local space. perhaps subtract the location of the actor from “current location” before plugging it in to add instance?

altho if this is the issue idk how it ever worked at all. probably your actor is at 0,0,0 so it makes no difference in this case

chuck a bunch of print strings everywhere ie. after the branch conditions after the sphere trace so you can get a clearer picture of what is happening. aside from that i’m out of ideas.