How to playing the script again if the condition is not done

Place to get help with not working mods / modding interface.
Post Reply
User avatar
WIZ4
Fast Inserter
Fast Inserter
Posts: 209
Joined: Thu Apr 07, 2016 1:36 pm
Contact:

How to playing the script again if the condition is not done

Post by WIZ4 »

I wrote a script, which generates several spitter spawner after a time interval.

Code: Select all

script.on_event(defines.events.on_tick, function(event)
  local interval = 10 * 60 * 1
    if game.tick >= interval then
    if (game.tick % interval) == 0 then
	
	local X = math.random(-1000,1000)
	local Y = math.random(100,1000)
	local radius = 32
	
	for y=-10,10 do   for x=-10,10 do 
	 local pos1 = {X+math.random(1,50),Y+math.random(1,50)}
          
	      if  game.surfaces.nauvis.can_place_entity{name = "spitter-spawner", position = pos1} == true then
	       game.surfaces.nauvis.create_entity({ name="spitter-spawner", position=pos1});
		   game.forces.player.chart(game.surfaces.nauvis, {{X - radius, Y - radius}, {X + radius, Y + radius}})
		   end
	   end	
	end
	end
	end
end)
But I have a problems that I can not solve:
How can I do this if random choose in not generate chunk, then reroll the script again ignore interval?
I I used "is_chunk_generated", but it not works:

Code: Select all

script.on_event(defines.events.on_tick, function(event)
  local interval = 10 * 60 * 60 
    if game.tick >= interval then
    if (game.tick % interval) == 0 then
	
	local X = math.random(-200,200)
	local Y = math.random(100,200)
	local radius = 32
	
	for y=-10,10 do   for x=-10,10 do 
	 local pos1 = {X+math.random(1,50),Y+math.random(1,50)}
          
	      if  game.surfaces.nauvis.can_place_entity{name = "spitter-spawner", position = pos1} == true then
	       game.surfaces.nauvis.create_entity({ name="spitter-spawner", position=pos1});
		   game.forces.player.chart(game.surfaces.nauvis, {{X - radius, Y - radius}, {X + radius, Y + radius}})
		   elseif game.surfaces.nauvis.is_chunk_generated({X, Y}) == false then -- How to reroll the script again ignore interval?
		   game.print("chunk not generated")
		   end
	   end	
	end
	end
	end
	end
end)
Help me please solve this problem and sorry for my google translate
My native language is russian. Sorry if my messages are difficult to read.

User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2903
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: How to playing the script again if the condition is not done

Post by darkfrei »

Move this code to the function.

Code: Select all

function try_place (pos1, radius)
  if  game.surfaces.nauvis.can_place_entity{name = "spitter-spawner", position = pos1} == true then
    game.surfaces.nauvis.create_entity({ name="spitter-spawner", position=pos1});
    game.forces.player.chart(game.surfaces.nauvis, {{X - radius, Y - radius}, {X + radius, Y + radius}})
    return true
  elseif game.surfaces.nauvis.is_chunk_generated({X, Y}) == false then
    game.print("chunk not generated")
    return false
  else
    game.print("second exception")
    return false
  end
end
Then you can try this function again if this function wasn't successful.

See also code of tree brush: Here is trying to place new tree again if it wasn't placed. But not too much, it's takes only one tick.
https://mods.factorio.com/mod/TreeBrush

User avatar
WIZ4
Fast Inserter
Fast Inserter
Posts: 209
Joined: Thu Apr 07, 2016 1:36 pm
Contact:

Re: How to playing the script again if the condition is not done

Post by WIZ4 »

darkfrei wrote:Move this code to the function.

Code: Select all

function try_place (pos1, radius)
  if  game.surfaces.nauvis.can_place_entity{name = "spitter-spawner", position = pos1} == true then
    game.surfaces.nauvis.create_entity({ name="spitter-spawner", position=pos1});
    game.forces.player.chart(game.surfaces.nauvis, {{X - radius, Y - radius}, {X + radius, Y + radius}})
    return true
  elseif game.surfaces.nauvis.is_chunk_generated({X, Y}) == false then
    game.print("chunk not generated")
    return false
  else
    game.print("second exception")
    return false
  end
end
Then you can try this function again if this function wasn't successful.

See also code of tree brush: Here is trying to place new tree again if it wasn't placed. But not too much, it's takes only one tick.
https://mods.factorio.com/mod/TreeBrush
The script does not work until 600 ticks in case the spawner can not appear.

Code: Select all

function try_place (pos1, radius, X, Y)
  if  game.surfaces.nauvis.can_place_entity{name = "spitter-spawner", position = pos1} == true then
    game.surfaces.nauvis.create_entity({ name="spitter-spawner", position=pos1});
    game.forces.player.chart(game.surfaces.nauvis, {{X - radius, Y - radius}, {X + radius, Y + radius}})
	game.print("luck")
	game.print(game.tick) 
    return true
  elseif game.surfaces.nauvis.is_chunk_generated({X, Y}) == false then
    game.print("chunk not generated")
    return false
  else
    game.print("second exception")
    return false
  end
end

Code: Select all

script.on_event(defines.events.on_tick, function(event)
  local interval = 10 * 60 * 1
    if game.tick >= interval then
    if (game.tick % interval) == 0 then
   
   local X = math.random(-1000,1000)
   local Y = math.random(800,1000)
   local radius = 32
   
   for y=-10,10 do   for x=-10,10 do 
    local pos1 = {X+math.random(1,50),Y+math.random(1,50)}
         game.print(game.tick) 
         try_place (pos1, radius, X, Y) 
		  
	 end
	 end
	 end
	 end
end)
What have I done wrong?
here's another: I do not understand how the operator "return" works. Can you briefly explain how it works in this function?
thank you for your help
My native language is russian. Sorry if my messages are difficult to read.

User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2903
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: How to playing the script again if the condition is not done

Post by darkfrei »

Code: Select all

function try_place_100_times (pos1, radius, X, Y)
  for i = 1, 100 do
    if try_place (pos1, radius, X, Y) then
      return
    end
  end
end
If your function try_place returns true, then this cycle will be completed. After 100 trying it will be also completed.

Post Reply

Return to “Modding help”