Jump to content




Why Does The Red Cable Go On?!


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

#1 campicus

  • Members
  • 164 posts

Posted 08 November 2013 - 08:48 AM

When this code boots up after a restart, all the cables should be ON, only the red goes on though. After I touch something on the screen, they all turn on. I have no idea what is happening here

Spoiler


#2 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 08 November 2013 - 04:15 PM

Your current toggle-spawn function:

local function toggleSpawn() --"turns spawners on/off depending on their status"
  for mob,data in pairs(spawn) do
    if (data.status == "on ") and colors.test(rs.getBundledOutput(rsSide), data.color) then             -- If this colour should be on, and IS on, then...
      rs.setBundledOutput(rsSide, rs.getBundledOutput(rsSide)-data.color)                               -- remove it from the cable, turning it off.
    elseif (data.status == "off") and (not colors.test(rs.getBundledOutput(rsSide), data.color)) then   -- otherwise, if it should be off, and IS off...
      rs.setBundledOutput(rsSide, rs.getBundledOutput(rsSide)+data.color)                               -- then turn it on.
    end
  end
end

See the problem? If a colour is enabled in the cable and is supposed to be, or isn't and shouldn't, then you don't want to perform any action at all.

If you want all the cables to be on when the program starts, then your starting table shouldn't be filled with "off"s.

Edited by Bomb Bloke, 08 November 2013 - 04:15 PM.


#3 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 08 November 2013 - 09:41 PM

Also you should be avoiding using + and - with ComputerCraft colours. You can get unexpected results, instead you should use colors.combine and colors.subtract...

Here is an example output/reason why... Lets assume we want to add white to a cable, that already has white
local current = colors.white
print(current + colors.white)
the output of this will be 2, which is colours.orange, meaning that this now has an unexpected result, however, now if we use combine
local current = colors.white
print(colors.combine(current, colors.white))
the output for this will be 1, which is white, meaning no unexpected result.

Knowing this knowledge we can actually refine your toggle spawn function down
local function toggleSpawn()
  for mob,data in pairs(spawn) do
	local func = data.status == "on " and colors.subtract or colors.combine --# make a variable for which function we want to call based on the status   [condition] and [if-true] or [if-false]
	rs.setBundledOutput(rsSide, func(rs.getBundledOutput(rsSide), data.color) --# set the output to what the new output should be
  end
end

Edited by theoriginalbit, 08 November 2013 - 09:42 PM.


#4 campicus

  • Members
  • 164 posts

Posted 08 November 2013 - 09:44 PM

If you supply spawners with a redstone signal they will turn off, so "off" actually means that the redpower should be on, so that the spawners are off. I will try colors.combine :)

EDIT:
Tried combine and subtract and it made no difference
local function toggleSpawn() --"turns spawners on/off depending on their status"
  for mob,data in pairs(spawn) do
	if active and (data.status == "on ") and colors.test(rs.getBundledOutput(rsSide), data.color) then
	  rs.setBundledOutput(rsSide, colors.subtract(rs.getBundledOutput(rsSide),data.color))
	elseif (data.status == "off") and (not colors.test(rs.getBundledOutput(rsSide), data.color)) then
	  rs.setBundledOutput(rsSide, colors.combine(rs.getBundledOutput(rsSide),data.color))
	end
  end
end

EDIT:
I changed the cable from red to gray and it still turned on... weird
Also, changed the status in the table to "on " for all of them, this resulted in all the wires having no rs signal (as it should)

LAST EDIT:
I fixed the problem by manually setting all the colors on when the program launches and not calling toggleSpawn() until the prgram gets a touch_screen event

Edited by campicus, 08 November 2013 - 10:07 PM.






2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users