Adding an object scattered over a terrain plane

Hi all,

I’m working on my first blender game project, a survival game. I’ve progressed quite a bit and am kind of stuck on a problem.

I’m trying to get 1 tree object on a different layer to be duplicated and scattered over a terrain mesh an X amount of times.

The furthest I got was adding the tree object once to a world position with the following python code:

import bge

``````scene = bge.logic.getCurrentScene()

object.worldPosition = [1,5,7]
``````

Can anyone help me out to figure this out?

thanks!

Anyone got any ideas? Thread kinda got flooded away

Ok, you will want to do a random position inside a grid

lets say a grid that is spanning from -50 to 50 on x and y.

position = [-50+bge.logic.getRandomFloat()*100, -50+bge.logic.getRandomFloat()*100, 30]

this point is inside that grid, 30 units in the air

next I would cast a ray down from the position, if nothing is in the way, add you item, at the ray hit point, (you can also do more ray checks)

I would try to place 5 per frame or so until I placed them all,
(while loops are hard to debug)

for I in range(5):

is easiest and wont make a big performance hit.

Could you perhaps provide me with an example, or example code? I’m pretty new to coding and have no idea yet how to code it in python. I’m still reading lot’s of tutorials

He gave you the code. it selects a random X,Y position. He cant set a random z position because he is assuming your terrain is not flat. Random Z would have objects in the air, and under the terrain. He sets them 30 units high, you must then cast a ray down and move it where it connects with the ground…I find the constraint actuator good for doing this in one tick. But assuming his code usage is correct. I always import random but.

``````

scene = bge.logic.getCurrentScene()

object.worldPosition =  [-50+bge.logic.getRandomFloat()*100, -50+bge.logic.getRandomFloat()*100, 30]

``````

Then put this on your boompie object. And a “ground” property on your terrain.

Hopefully you have a loop or someway of stopping this from running a zillion times

Edit: I didnt see the loop in his example but yeah do that.

@BPR I think the best way to use while loops is to just dive into while[“this”]==True. They are buggy in BGE but fun to play with. A neat little program that we used in our “Introduction to Logic and Programming Class” is VisualLogic. It teaches you loops and stuff in a node/flowchart/visual style. Very basic. Its free but you just cant save the files unless you purchase it. Can help you layout how you want something to work though