Page 1 of 1

Why can not I get a random spawner

Posted: Sun Jan 21, 2018 8:41 pm
by WIZ4
I wrote a script that should generate random spawner:

Code: Select all

script.on_event(defines.events.on_tick, function(event)
  local interval = 1 * 1 * 60  
  if game.tick >= 1*1*60 then 
    if (game.tick % interval) == 0 then
	local X = math.random(-200,200)
	local Y = math.random(100,200)
	for y=-15,15 do   for x=-15,15 do
	 local pos1 = {X+math.random(-25,25),Y+math.random(-25,25)}
	 local enemy1 = "spitter-spawner"
	 local enemy2 = "biter-spawner"
	 local name = "enemy"..math.random(1,2)
	 
	try_place_spawner (enemy1,enemy2,name,pos1)
	end
	end
	end
	end
end)

function try_place_spawner (enemy1,enemy2,name,pos1)
  if  game.surfaces.nauvis.can_place_entity{name = name, position = pos1} == true then
game.surfaces.nauvis.create_entity({ name= name, position = pos1});
end
 
end
But I get this error:
Screenshot_1.png
Screenshot_1.png (101.55 KiB) Viewed 1900 times
Why do I get the name enemy1, not "spitter-spawner"?
Any suggestion how to implement it differently?

Re: Why can not I get a random spawner

Posted: Sun Jan 21, 2018 11:49 pm
by DaveMcW

Code: Select all

local enemy = {"biter-spawner", "spitter-spawner"}
local name = enemy[math.random(1,2)]

Re: Why can not I get a random spawner

Posted: Mon Jan 22, 2018 9:35 am
by bobingabout
Basically you're currently using a random to generate the name "enemy1" or "enemy2" which is the name of your variable, coding doesn't work that way.

The suggested fix in the previous post sets the variable name to be a copy of either element 1, or element 2 of an array named enemy.

Your function then doesn't need enemy1 or enemy2 passing to it, just name and pos1.

Re: Why can not I get a random spawner

Posted: Mon Jan 22, 2018 1:02 pm
by WIZ4
This solves the problem, thanks

Re: Why can not I get a random spawner

Posted: Mon Jan 22, 2018 2:58 pm
by WIZ4
I tried to make sure that each time the function is executed, to him "local count" was added 50

Code: Select all

script.on_event(defines.events.on_tick, function(event)
  local interval = 1 * 1 * 60  
  if game.tick >= 1*1*60 then 
    if (game.tick % interval) == 0 then
   local X = math.random(-200,200)
   local Y = math.random(100,200)
   for y=-15,15 do   for x=-15,15 do
    local count = 25
    local pos1 = {X+math.random(-count,count),Y+math.random(-count,count)}
    local enemy = {"spitter-spawner", "biter-spawner"}
    local name = "enemy"[math.random(1,2)]
    
   try_place_spawner (count,name,pos1)
   count = count + 50
   end
   end
   end
   end
end)

function try_place_spawner (count,name,pos1)
  if  game.surfaces.nauvis.can_place_entity{name = name, position = pos1} == true then
game.surfaces.nauvis.create_entity({ name= name, position = pos1});
end
 
end
But count it still remains the same, at the next activation function
Why this does not work?

Re: Why can not I get a random spawner

Posted: Mon Jan 22, 2018 6:38 pm
by orzelek
It's because local variable is short lived - it's reinitialized every run of method.
You'd need to store it in global table to have it stored between runs and saved in save file.

Re: Why can not I get a random spawner

Posted: Tue Jan 23, 2018 1:05 pm
by WIZ4
orzelek wrote:It's because local variable is short lived - it's reinitialized every run of method.
You'd need to store it in global table to have it stored between runs and saved in save file.
Thanks, it worked!