This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Cranium's profile picture

[Help] Adding colors together after a for loop

Started by Cranium, 26 September 2012 - 02:55 AM
Cranium #1
Posted 26 September 2012 - 04:55 AM
What I need to do, is iterate through a for loop, while collecting data from a save file. This save file has the values of my bundled cables, whether they are on or not. Currently, by function is failing, because it will iterate through the loop, collecting the correct data, and then all hell breaks loose….
I'll post my function here that's having the problem:
Spoiler


local function resume()
controlTable = loadState()
local colorTable = {}
for eNum, dInfo in ipairs(controlTable) do
for i = 1,#machineList do
if dInfo.type == machineList[i] then
if dInfo.type == "Reactor" or dInfo.type == "Coolant" then
if dInfo.status == "false" and dInfo.color ~= "false" then
print(dInfo.type..", "..dInfo.color..", "..dInfo.status)
sleep(1)
rs.setBundledOutput("back",colors.combine(tonumber(dInfo.color)))
elseif dInfo.status == "true" and dInfo.color ~= "false"  then
print(dInfo.type..", "..dInfo.color..", "..dInfo.status)
sleep(1)
rs.setBundledOutput("back",colors.subtract(tonumber(dInfo.color)))
end
else
if dInfo.status == "false" and dInfo.color ~= "false"  then
print(dInfo.type..", "..dInfo.color..", "..dInfo.status)
sleep(1)
rs.setBundledOutput("back",colors.subtract(tonumber(dInfo.color)))
elseif dInfo.status == "true" and dInfo.color ~= "false"  then
print(dInfo.type..", "..dInfo.color..", "..dInfo.status)
sleep(1)
rs.setBundledOutput("back",colors.combine(tonumber(dInfo.color)))
end
end
end
end 
end
end
Here is my loadState() function:
Spoiler


local function loadState()
    local loadTable = {}
    local file = fs.open(".MCS/status", "r")
    if file then
        readLine = file.readLine()
        while readLine do
            local lineTable = {}
            lineTable.type, lineTable.color, lineTable.status = string.match(readLine, "(%w+),(%w+),(%w+)")
            if lineTable.type then
                table.insert(loadTable, lineTable)
            end
            readLine = file.readLine()
        end
        file.close()
    end
    return loadTable
end
And here is my status file:
Spoiler


Admin,craniumkid22,xxxxx
User,miveck,xxxxx
Lights,1,true
IndustrialCraft2,true,true
Power,2,false
Reactor,16384,false
Coolant,2048,false
MFSU,redstone,None
Macerator,false,false
Furnace,false,false
Compressor,false,false
Extractor,false,false
RedPower2,true,true
RedPowerPump,false,false
Blockbreaker,false,false
Buildcraft,true,true
MiningWell,false,false
Quarry,false,false
BuildcraftPump,false,false
Forcefield,2,false
Firstrun,completed,true
As it is right now, it will iterate through, and for testing I put in the print and sleep functions. It will turn on the correct cables as it iterates, but it will then set to another cable the next color down. I thought that colors.combine() would fix that…obviously I'm wrong. I need the colors that it retrieves to be collected somehow, and then add the values AFTER the iteration, instead of during. How would I do that?
Doyle3694 #2
Posted 26 September 2012 - 07:15 AM
I don't really understand your code, but what if you just added a value for all colors(white:1orange:2magenta:4 etc.) and then just plus 'em up into a variable and do rs.setBundledOutpu("back", YourVariable)
Cranium #3
Posted 26 September 2012 - 12:13 PM
I solved it. It literally came to me while I was asleep(hence the reason I'm up so early):

local function resume()
controlTable = loadState()
local colorVar = 0
for eNum, dInfo in ipairs(controlTable) do
for i = 1,#machineList do
  if dInfo.type == machineList[i] then
   if dInfo.type == "Reactor" or dInfo.type == "Coolant" then
    if dInfo.status == "false" and dInfo.color ~= "false" and dInfo.color ~= "true" and dInfo.color ~= "redstone" then
	 print(dInfo.type..", "..dInfo.color..", "..dInfo.status)
	 sleep(1)
	 colorVar = colorVar + tonumber(dInfo.color)
    elseif dInfo.status == "true" and dInfo.color ~= "false" and dInfo.color ~= "true" and dInfo.color ~= "redstone"   then
	 print(dInfo.type..", "..dInfo.color..", "..dInfo.status)
	 sleep(1)
	 colorVar = colorVar - tonumber(dInfo.color)
    end
   else
    if dInfo.status == "false" and dInfo.color ~= "false" and dInfo.color ~= "true" and dInfo.color ~= "redstone"   then
	 print(dInfo.type..", "..dInfo.color..", "..dInfo.status)
	 sleep(1)
	 colorVar = colorVar - tonumber(dInfo.color)
    elseif dInfo.status == "true" and dInfo.color ~= "false" and dInfo.color ~= "true" and dInfo.color ~= "redstone"   then
	 print(dInfo.type..", "..dInfo.color..", "..dInfo.status)
	 sleep(1)
	 colorVar = colorVar + tonumber(dInfo.color)
    end
   end
  end
end
end
rs.setBundledOutput("back",colorVar)
end
I'll be removing the sleep and print functions before releasing the update to my program later. But this seems to fix my problem.
Cranium #4
Posted 26 September 2012 - 04:23 PM
Strike that, it did not work. But I realized my mistake:

local function resume()
controlTable = loadState()
local colorVar = 0
for eNum, dInfo in ipairs(controlTable) do
 for i = 1,#machineList do
  if dInfo.type == machineList[i] then
   if dInfo.type == "Reactor" or dInfo.type == "Coolant" then
	if dInfo.status == "false" and dInfo.color ~= "false" and dInfo.color ~= "true" and dInfo.color ~= "redstone" then
	 print(dInfo.type..", "..dInfo.color..", "..dInfo.status)
	 sleep(1)
	 colorVar = colorVar + tonumber(dInfo.color)
	elseif dInfo.status == "true" and dInfo.color ~= "false" and dInfo.color ~= "true" and dInfo.color ~= "redstone"   then
	 print(dInfo.type..", "..dInfo.color..", "..dInfo.status)
	 sleep(1)
	 colorVar = colorVar +0 --was subtracting when nothing should ahve been added.
	end
   else
	if dInfo.status == "false" and dInfo.color ~= "false" and dInfo.color ~= "true" and dInfo.color ~= "redstone"   then
	 print(dInfo.type..", "..dInfo.color..", "..dInfo.status)
	 sleep(1)
	 colorVar = colorVar + 0 --was subtracting when nothing should ahve been added.
	elseif dInfo.status == "true" and dInfo.color ~= "false" and dInfo.color ~= "true" and dInfo.color ~= "redstone"   then
	 print(dInfo.type..", "..dInfo.color..", "..dInfo.status)
	 sleep(1)
	 colorVar = colorVar + tonumber(dInfo.color)
	end
   end
  end
 end 
end
rs.setBundledOutput("back",colorVar)
end