Why can not I get a random spawner

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:

Why can not I get a random spawner

Post 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 1312 times
Why do I get the name enemy1, not "spitter-spawner"?
Any suggestion how to implement it differently?
My native language is russian. Sorry if my messages are difficult to read.

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3700
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: Why can not I get a random spawner

Post by DaveMcW »

Code: Select all

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

User avatar
bobingabout
Smart Inserter
Smart Inserter
Posts: 7352
Joined: Fri May 09, 2014 1:01 pm
Contact:

Re: Why can not I get a random spawner

Post 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.
Creator of Bob's mods. Expanding your gameplay since version 0.9.8.
I also have a Patreon.

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

Re: Why can not I get a random spawner

Post by WIZ4 »

This solves the problem, thanks
My native language is russian. Sorry if my messages are difficult to read.

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

Re: Why can not I get a random spawner

Post 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?
My native language is russian. Sorry if my messages are difficult to read.

orzelek
Smart Inserter
Smart Inserter
Posts: 3911
Joined: Fri Apr 03, 2015 10:20 am
Contact:

Re: Why can not I get a random spawner

Post 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.

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

Re: Why can not I get a random spawner

Post 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!
My native language is russian. Sorry if my messages are difficult to read.

Post Reply

Return to “Modding help”