Here is some relevant code for adding the inputs and outputs:
Code: Select all
local function addInput(itemSignal)
-- Add to inputs
itemSignal.index = global.PSCInputIndex
local signal_found = false
for _, p in pairs( global.PSCInputSignals ) do
if p.signal.name == itemSignal.signal.name then
p.count = p.count + itemSignal.count
signal_found = true
break
end
end
if not signal_found then
table.insert( global.PSCInputSignals, itemSignal )
global.PSCInputIndex = global.PSCInputIndex + 1
end
-- Add to total
local itemSignalT = itemSignal
itemSignalT.index = global.PSCTotalIndex
local signal_found = false
for _, p in pairs( global.PSCTotalSignals ) do
if p.signal.name == itemSignalT.signal.name then
p.count = p.count + itemSignalT.count
signal_found = true
break
end
end
if not signal_found then
table.insert( global.PSCTotalSignals, itemSignalT )
global.PSCTotalIndex = global.PSCTotalIndex + 1
end
-- Add to percentage
local itemSignalP = itemSignal
itemSignalP.index = global.PSCPercentageIndex
local signal_found = false
for _, p in pairs( global.PSCPercentageSignals ) do
if p.signal.name == itemSignalP.signal.name then
p.count = p.count + itemSignalP.count
signal_found = true
break
end
end
if not signal_found then
table.insert( global.PSCPercentageSignals, itemSignalP )
global.PSCPercentageIndex = global.PSCPercentageIndex + 1
end
end
local function addOutput(itemSignal)
-- Add to outputs
itemSignal.index = global.PSCOutputIndex
local signal_found = false
for _, p in pairs( global.PSCOutputSignals ) do
if p.signal.name == itemSignal.signal.name then
p.count = p.count + itemSignal.count
signal_found = true
break
end
end
if not signal_found then
table.insert( global.PSCOutputSignals, itemSignal )
global.PSCOutputIndex = global.PSCOutputIndex + 1
end
-- Add to total
local itemSignalT = itemSignal
itemSignalT.index = global.PSCTotalIndex
local signal_found = false
for _, p in pairs( global.PSCTotalSignals ) do
if p.signal.name == itemSignalT.signal.name then
p.count = p.count - itemSignalT.count
signal_found = true
break
end
end
if not signal_found then
table.insert( global.PSCTotalSignals, itemSignalT )
global.PSCTotalIndex = global.PSCTotalIndex + 1
end
-- Add to percentage
local itemSignalP = itemSignal
itemSignalP.index = global.PSCPercentageIndex
itemSignalP.count = -itemSignalP.count
local signal_found = false
for _, p in pairs( global.PSCPercentageSignals ) do
if p.signal.name == itemSignalP.signal.name then
if itemSignalP.count > 0 then
p.count = ((p.count / itemSignalP.count) - 1) * 100
elseif p.count > 0 and itemSignalP.count == 0 then
p.count = 100
end
signal_found = true
break
end
end
if not signal_found then
itemSignalP.count = -100
table.insert( global.PSCPercentageSignals, itemSignalP )
global.PSCPercentageIndex = global.PSCPercentageIndex + 1
end
end
Code: Select all
-- Item Production Input
for itemName, itemCount in pairs (force.item_production_statistics.input_counts) do
local itemSignal = { signal = { type = "item", name = itemName }, count = itemCount, index = global.PSCInputIndex }
local temp = itemCount
-- Init array value
if global.PSCInputPrevious[itemName] == nil then
global.PSCInputPrevious[itemName] = itemCount
end
-- Modify count to represent change over time
itemSignal.count = (itemSignal.count - global.PSCInputPrevious[itemName]) * multiplier
global.PSCInputPrevious[itemName] = temp
-- Add Input
addInput(itemSignal)
end
-- Item Production Output
for itemName, itemCount in pairs (force.item_production_statistics.output_counts) do
local itemSignal = { signal = { type = "item", name = itemName }, count = itemCount, index = global.PSCOutputIndex }
local temp = itemCount
-- Init array value
if global.PSCOutputPrevious[itemName] == nil then
global.PSCOutputPrevious[itemName] = itemCount
end
-- Modify count to represent change over time
itemSignal.count = (itemSignal.count - global.PSCOutputPrevious[itemName]) * multiplier
global.PSCOutputPrevious[itemName] = temp
itemSignal.count = -itemSignal.count
-- Output
addOutput(itemSignal)
end
-- More code
-- Apply Signals
for i, c in pairs (global.PSCEntitiesInput) do
if c then
local behavior = c.get_or_create_control_behavior()
if #global.PSCInputSignals > 0 then
behavior.parameters = { parameters = global.PSCInputSignals }
end
end
end
Does anyone see the issue?
Side-question: is there anything special I need to do to make my GUI closable when pressing esc? Because right now esc does nothing and I can only close the GUI my opening another GUI.