Random Timer?

Okay forgive me if I’m being majorly retarded, I’m trying to create a function just for the purpose of learning that will spawn 5 chairs in random positions after a timer has expired after a random value.

It sort of works, when I say sort of 4 chairs spawn then another one randomly spawns according to the math.random values.

If you don’t get what I mean just ask me :v:

Here’s the code:

[lua]test = 0

function SpawnTimer()

timer.Simple( (math.random(5,10)), ChairFunc)

end

function ChairFunc()

local x = math.random(1,256) //Generates the X variable
local y = math.random(1,256) //Generates the Y variable
local z = -130 //math.random(1,256) Generates the Z variable but is subject to change based on map

	local chairposition = Vector( x, y, z ) //Should generate a vector based on the random variables
	
Msg(chairposition)
	
local ent = ents.Create ("prop_physics") //Create chairs which are immovable 
ent:SetModel ("models/props_c17/FurnitureChair001a.mdl") //sets the chairs model
ent:SetPos (chairposition) //sets chair position from the Vectors
ent:Spawn() //Spawns the ent

test = test + 1 //Adds 1 onto a variable for the repeat call

end 

repeat //repeats the defined function, in this case ChairFunc

	ChairFunc()
	
until test == 4 //Will repeat until the test variable equals 4

SpawnTimer()[/lua]

Get any errors? If so put them here?

No, no errors at all, it just spawns 4 chairs then the fifth one after a random time :confused:

Want like around 5 seconds apart randomly or just all randomly in around 5 seconds. Like shold it take 25 avg for all or 5 for all?

Would like them all to spawn at the same time, because the x and y is random they tend not to collide.

Well for one, why bother using “repeat until” ? It’s completely unnecessary, just use a for loop.

This is how I would write it.
[lua]
function SpawnTimer()
timer.Simple(math.random(5,10), function()
for i = 1, 5 do
local chairPos = Vector(math.random(1,256), math.random(1,256), -130)
local ent = ents.Create(“prop_physics”)
ent:SetModel(“models/props_c17/FurnitureChair001a.mdl”)
ent:SetPos(chairPos)
ent:Spawn()
end
end)
end
[/lua]

Sometimes Lua for loops are strange and don’t work unless you use the loop variable in them (at least thats what it seems like for me, I could be wrong) if that is the case just add i to one of the position vectors.

[editline]24th April 2011[/editline]

Also I think your code will perform an infinite loop, you’re recursively calling the functions within each other. Bad news bears right there.

Try this out. Im doing this on my phone so no promices.

Wow, I feel like a major retard for over complicating something, I think I may love you Feihc…

[editline]24th April 2011[/editline]

So how would I go about making so all the chair spawn individually at random intervals?

[lua]test = 0

function SpawnTimer()
timer.Simple(math.random(5,10), function()
ChairFunc()

	test = test + 1
	
	if test < 4 then
		SpawnTimer() -- Create another timer with a random interval
	end
end)

end

function ChairFunc()
local chairPos = Vector(math.random(1,256), math.random(1,256), -130)
local ent = ents.Create(“prop_physics”)
ent:SetModel(“models/props_c17/FurnitureChair001a.mdl”)
ent:SetPos(chairPos)
ent:Spawn()
end

SpawnTimer()[/lua]

Not sure why this has to be so complicated.

[lua]
– This version of this function will spawn all the chairs within maxInterval
function spawnChairs( num, minInterval, maxInterval)
for i = 1, num do
timer.Simple(math.random(minInterval, maxInterval), spawnChair)
end
end

function spawnChair()
local chairPos = Vector(math.random(1,256), math.random(1,256), -130)
local ent = ents.Create(“prop_physics”)
ent:SetModel(“models/props_c17/FurnitureChair001a.mdl”)
ent:SetPos(chairPos)
ent:Spawn()
end

– Then just call spawnChairs whenever you want chairs
spawnChairs(5, 5,10) – so 5 chairs, with min 5 seconds, max 10 seconds on the random interval

[/lua]

And this version:
[lua]
– Will spawn the chairs after the last chair, so if chair one was 3 seconds chair two would be 3 seconds + chair two’s time
– So it will finish between minInterval * num seconds and maxInterval * num seconds
function spawnChairs( num, minInterval, maxInterval)
local totalInterval = 0
for i = 1, num do
local rndInterval = math.random(minInterval, maxInterval)
timer.Simple(totalInterval + rndInterval, spawnChair)
totalInterval = totalInterval + rndInterval
end
end
[/lua]