Jump to content




[Help] Adding colors together after a for loop


  • You cannot reply to this topic
3 replies to this topic

#1 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 26 September 2012 - 03: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
Here is my loadState() function:
Spoiler
And here is my status file:
Spoiler
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?

#2 Doyle3694

  • Members
  • 815 posts

Posted 26 September 2012 - 06: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)

#3 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 26 September 2012 - 11:13 AM

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.

#4 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 26 September 2012 - 03: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






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users