Adjusting One Table Seems to Affect a Different Table
Posted: Sat May 12, 2018 1:57 am
So in my mod I add inputs to one table and outputs to another. I also have a third table which is a total of the two. So when I add something to the inputs table I also add it to the total table. When I add something to the outputs table I also add it (subtract) to the total table. I've noticed a weird issue with this. If I don't add anything from the outputs to the total table everything is fine, but if I add the outputs to the total table, the input table also gets changed.
Here is some relevant code for adding the inputs and outputs:
And here is the code that actually calls those functions
If you look at the code, what happens is we loop over all the input production statistics and output production statistics, get the difference over time (i.e. since the last update how much has changed) and then call either addInput or addOutput. Each of these first checks if the item already exists in the table, if it does then it simply adds the count to the existing amount, otherwise it inserts the count into the table. As you can see the addOutput function never touches the global.PSCInputSignals table, but still it messes up. If I comment out the lines in addOutput that related to global.PSCTotalSignals and global.PSCPercentageSignals it works fine. So there's some weird issue with the code that I don't understand.
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.
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.