Merge signals read from get_circuit_network

Place to get help with not working mods / modding interface.
Post Reply
User avatar
Optera
Smart Inserter
Smart Inserter
Posts: 2919
Joined: Sat Jun 11, 2016 6:41 am
Contact:

Merge signals read from get_circuit_network

Post by Optera »

I'm looking for a fast way to merge signals read with get_circuit_network(defines.wire_type.green) and get_circuit_network(defines.wire_type.red).
Running recursive for loops is not what i want to do in on_tick handling just to get a complete list of signals.

It's a shame we don't have defines.wire_type.all :(

User avatar
aubergine18
Smart Inserter
Smart Inserter
Posts: 1264
Joined: Fri Jul 22, 2016 8:51 pm
Contact:

Re: Merge signals read from get_circuit_network

Post by aubergine18 »

It would help to know what you intend to do with the merged results.

Merging in any manner would be relatively slow compared to just iterating the results of both wires.
Better forum search for modders: Enclose your search term in quotes, eg. "font_color" or "custom-input" - it prevents the forum search from splitting on hypens and underscores, resulting in much more accurate results.

User avatar
Optera
Smart Inserter
Smart Inserter
Posts: 2919
Joined: Sat Jun 11, 2016 6:41 am
Contact:

Re: Merge signals read from get_circuit_network

Post by Optera »

What I want is having all signals merged into one array.
Currently I'm doing it like this.

Code: Select all

function GetCircuitValues(entity) 
  local greenWire = entity.get_circuit_network(defines.wire_type.green)
  local redWire =  entity.get_circuit_network(defines.wire_type.red)
  local items = {} 
  if greenWire then
    for _, v in pairs (greenWire.signals) do
      if v.signal.type == "item" then
        items[v.signal.name] = v.count
      end
    end
  end
  if redWire then
    for _, v in pairs (redWire.signals) do 
      if v.signal.type == "item" then
        if items[v.signal.name] ~= nil then
          items[v.signal.name] = items[v.signal.name] + v.count
        else
          items[v.signal.name] = v.count
        end
      end
    end
  end
  return items
end
If we want to mod something that can handle inputs similar to combinators we'll need either an extension to the api, like get_circuit_network(defines.wire_type.all), or some really fast method of merging these two tables, as I can't imagine every combinator doing these two for each loops every tick.

Rseding91
Factorio Staff
Factorio Staff
Posts: 13219
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: Merge signals read from get_circuit_network

Post by Rseding91 »

Optera wrote:... as I can't imagine every combinator doing these two for each loops every tick.
That's what the game does :P
If you want to get ahold of me I'm almost always on Discord.

User avatar
Optera
Smart Inserter
Smart Inserter
Posts: 2919
Joined: Sat Jun 11, 2016 6:41 am
Contact:

Re: Merge signals read from get_circuit_network

Post by Optera »

Rseding91 wrote:
Optera wrote:... as I can't imagine every combinator doing these two for each loops every tick.
That's what the game does :P
Is that why feeding the whole content of the logistic network from roboports into several combinators causes network update to take noticeable more time?

User avatar
aubergine18
Smart Inserter
Smart Inserter
Posts: 1264
Joined: Fri Jul 22, 2016 8:51 pm
Contact:

Re: Merge signals read from get_circuit_network

Post by aubergine18 »

Can't see many ways to make it faster via Lua, other than reducing number of table lookups and variables:

Code: Select all

local red                  , green
    = defines.wire_type.red, defines.wire_type.green

function GetCircuitValues( entity )

  local items, wire
      = {}   , entity.get_circuit_network( green )

  if wire then
    for _, data in pairs( wire.signals ) do
      if data.signal.type == "item" then
        items[data.signal.name] = data.count
      end
    end
  end

  wire = entity.get_circuit_network( red )

  if wire then
    local value
    for _, data in pairs (wire.signals) do 
      if data.signal.type == "item" then
        value = items[data.signal.name]
        items[data.signal.name] = value and (value + data.count) or data.count
      end
    end
  end

  return items
end
Better forum search for modders: Enclose your search term in quotes, eg. "font_color" or "custom-input" - it prevents the forum search from splitting on hypens and underscores, resulting in much more accurate results.

Post Reply

Return to “Modding help”