Factorio wait,sleep,etc?

Place to get help with not working mods / modding interface.
Post Reply
Dominic92x
Burner Inserter
Burner Inserter
Posts: 12
Joined: Wed Mar 02, 2016 11:48 pm
Contact:

Factorio wait,sleep,etc?

Post by Dominic92x »

Hello! For learning Factorio/lua I am working on a personal mod. One part of this mod is having the enemy bases slowly spread creep (Like Zerg from SC2).

I have the creep working with the line below:

Code: Select all

function check_spawners()
   if global.biterbase ~= nil then
				for k,gen in pairs(global.biterbase) do
					local tiles={}
					local surface = gen.surface
					for y=-(global.CreepSpread),(global.CreepSpread) do
					for x=-(global.CreepSpread),(global.CreepSpread) do
            if not string.match(surface.get_tile(gen.position.x+x,gen.position.y+y).name, "water") then						
                table.insert(tiles, {name="Creep", position={gen.position.x+x,gen.position.y+y}})
					end
					end
					end	
					surface.set_tiles(tiles)	
end
end
end
However, this makes all the bases update their "creep" at once. How do you make each entity in the table execute this command at a random interval? Like in other games I have modded I would simply put a sleep 2 + ((random 2)) after the "do" command, to have the commands staggered. How would you do this in Factorio? Thank you! I can't seem to find anything on the forums/web.

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

Re: Factorio wait,sleep,etc?

Post by DaveMcW »

Code: Select all

script.on_tick(function()
  local creep_delay = 3 * 60   -- 3 seconds
  
  local i = game.tick % creep_delay

  if (global.biterbase and global.biterbase[i]) then
    local gen = global.biterbase[i]
  
    ... spread creep around this base ...

  end
end
This will work for up to 180 bases. If you have more than that, you need to update multiple bases per tick.

Dominic92x
Burner Inserter
Burner Inserter
Posts: 12
Joined: Wed Mar 02, 2016 11:48 pm
Contact:

Re: Factorio wait,sleep,etc?

Post by Dominic92x »

Wow! Thank you,
I'm having a hard time wrapping my head around that - but I will keep working with it until I get it.

Would you have time to explain this line?

Code: Select all

   if (global.biterbase and global.biterbase[i]) then
    local gen = global.biterbase[i]
  
    ... spread creep around this base ...

  end
I can see how it is interacting (Guesssing that "local gen" is pulling the entity from the table) but I am having a hard time understanding what that line actually is checking for and how.

Thank you for your time and the quick response!
I appreciate it!


edit: It is working in-game...Just don't fully understand WHY lol

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

Re: Factorio wait,sleep,etc?

Post by DaveMcW »

i counts from 0 to 179 over three seconds.

global.biterbase returns the base at the i'th position in the table.

Actually I guess that is a bug... it should be i+1 because lua tables start at index 1.

Dominic92x
Burner Inserter
Burner Inserter
Posts: 12
Joined: Wed Mar 02, 2016 11:48 pm
Contact:

Re: Factorio wait,sleep,etc?

Post by Dominic92x »

Ahh, thank you so much!

I understand better now - it works very well.

This is what I ended up with,

Code: Select all

function check_spawners()
   if global.biterbase ~= nil then
					local tiles={}
					local creep_delay = 4 * 60 -- 4 seconds
					local i = game.tick % creep_delay
					--game.players[1].print(tostring(global.biterbase))
					if (global.biterbase and global.biterbase[i+1]) then
					--game.players[1].print(tostring(global.CreepSpread))
					local gen = global.biterbase[i+1]
					if global.biterbase[i+1].valid then
					for y=-(global.CreepSpread),(global.CreepSpread) do
					for x=-(global.CreepSpread),(global.CreepSpread) do
            if not string.match(gen.surface.get_tile(gen.position.x+x,gen.position.y+y).name, "water") and not string.match(gen.surface.get_tile(gen.position.x+x,gen.position.y+y).name, "Creep") then						
                table.insert(tiles, {name="Creep", position={gen.position.x+x,gen.position.y+y}})
					end
					end
					end
					gen.surface.set_tiles(tiles)
					end		
					end
end
end

Unfortunately, depending on how big the spawned tile is, it causes some pretty bad stutters (Still good to learn though).

I wonder how to create a system to perpetually spread a tile across a base without simply increasing the tile creation size.

Post Reply

Return to “Modding help”