Delete prop when hit with other?

I’m new on this forum so sorry if i make mistakes.

I’m just playing around with lua and trying to make a gamemode for fun.
But i got a automatic prop spawner that spawns trees at a random place but i dont want them to collide with another prop so you won’t see two trees in eachother.

i’m usin this spawn code:

function SpawnProp(model, times)
		for i=1, times do
		local ent = ents.Create("prop_physics")
			local rndX = math.random(4000)
			local rndY = math.random(4000)
			ent:SetModel( model )
			ent:SetPos( Vector(rndX, rndY, 19) )
			Msg( "spawning".. model .. " " .. times .. " times" .. " X:" .. rndX .. " Y: " .. rndY .. " now at: " .. i .. "

So, ofcourse, how do i do that?

Rather than deleting props if they collide, I would rather make sure the space isn’t occupied. Try something like this.

[lua]function SpawnProp(model,times,radius,overflow) – You don’t use overflow. You just call SpawnProp(model,times,radius).
if not overflow then overflow = 0 end

if overflow>5 then return end -- We've tried 5 times; we just can't find an open spot! Give up. :(

local fails = 0
for i=1,times do
	local p = Vector(math.random(4000),math.random(4000),19)

	if #ents.FindInSphere(p,radius)<1 then
		local e = ents.Create("prop_physics")
		fails = fails + 1 -- We keep track of all the times we picked an occupied spot, so we can try again when we're done with this spawn iteration.

if fails>0 then
	SpawnProp(model,fails,radius,overflow+1) -- We failed at least once, so we'll try to spawn those ones that failed again.


Where “radius” is the area around the prop you don’t want anything else to be. I suggest something slightly larger than the prop itself.

It’s a bit more complex than the code you’re trying to achieve, but that’s because I put in an overflow prevention. Look over the code, see if it all makes sense. Any questions, don’t hesitate to ask. I put comments on the overflow logic to help you see what I’m doing.

Also, just a quick note. When you do math.random(int), the generated number will only be between 0 and 4000. This is fine and all, but assuming the origin of the map (Vector(0,0,0)) is in the center of your map, you’ll only be spawning props in the upper-right region of the map doing this.

I suggest doing math.random(-int,int), which in this case is math.random(-4000,4000), for both x and y. That way you get all four quadrants of space.

Close, but not quite
You missed a variable on SpawnProp, so SpawnProp(model,fails,overflow+1) should be SpawnProp(model, fails, radius, overflow+1)

It was psuedocode. Wrote it up on the spot. I was kinda distracted with doing calculus.

thanks! it works.
But you removed the e:enableMotion(false)
is it because it doesn’t do anything cause i can’t get my prop to spawn freezed :confused:

Greets, Tim.

Sorry, I didn’t really look at your existing code when I wrote that.

Yeah, you’ll want to include

local phys = e:GetPhysicsObject()
if phys and ValidEntity(phys) then

Right after you spawn the tree.

Can you just use ENT:Touch() and see if the ent touching it is another tree and remove it if it is?

I’m also curious as to if lua allows ents to be spawned outside the map. If it does you might want to check it and remove it. I assume you’ll also want to drop the ents to the ground when you spawn it.