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

Need help with, what should be, a simple program

Started by Phenox124, 02 September 2014 - 01:22 AM
Phenox124 #1
Posted 02 September 2014 - 03:22 AM
I'm trying to get the monitor, single monitor, to change various aspects and trigger a redstone signal out the right side, I keep getting a expected string, boolean on line 27

mon = peripheral.wrap("top")
if rednet.isOpen() == false then
  rednet.open("left")
  active()
else
  active()
end
function active()
rednet.send(202, "I'm Online!")
load()
end
function load()
sleep(5)
mon.clear()
mon.setBackgroundColor(colors.green)
mon.clear()
mon.setTextScale(1)
mon.setCursorPos(2, 3)
mon.write("Ready")
touch()
end
function touch()
if os.pullEvent("monitor_touch")then
  redstone.setOutput("top", true)
  os.sleep(3)
  mon.clear()
  mon.setBackgroundColor(colors.red)
  mon.clear()
  mon.setCursorPos(1, 3)
  mon.write("Working")
end
if os.pullEvent("monitor_touch") then
  redstone.setOutput("top", false)
  os.sleep(3)
  load()
end
end

Here's the code, any help woul be thankful :)/>

P.S. here is the rest of my stuff, only this and another file atm https://github.com/P...24/Singularitys
I'm trying to make a program to switch between quantum singularitys in my base, multiple computers.
Bomb Bloke #2
Posted 02 September 2014 - 10:04 AM
Line 27 reads:

  mon.setBackgroundColor(colors.red)

That function only accepts a number, not a string and boolean, hence the error isn't being triggered by that line. Make sure the content of the script file you're running matches that which you've posted here.

rednet.setOutput() is an example of a function which does expect you to pass it a string and boolean.

This code:

if os.pullEvent("monitor_touch") then

… will always resolve as true. This is because os.pullEvent() returns a series of values, the first of which is the string naming the sort of event that was pulled; strings always count as true in "if" statements (as does most anything that isn't false or nil).

It's usually a bad idea to have a function call itself (or call another function that calls itself). When a function call is made, the "old" function doesn't end - it sits in the function stack, waiting for the "new" function to finish, at which point it resumes from where it left off.

If "touch()" calls "load()", then "load()" calls "touch()", and so on for eternity with none of the new function calls ever ending, eventually the RAM reserved for the function stack runs out and your script will outright crash.

Here's your code refactored to use a while loop instead:

local mon = peripheral.wrap("top")
mon.setTextScale(1)
redstone.setOutput("top", false)

if not rednet.isOpen() then rednet.open("left") end

rednet.send(202, "I'm Online!")

while true do
	mon.setBackgroundColor(colors.green)
	mon.clear()
	mon.setCursorPos(2, 3)
	mon.write("Ready")
	
	os.pullEvent("monitor_touch")
	
	redstone.setOutput("top", true)
	
	mon.setBackgroundColor(colors.red)
	mon.clear()
	mon.setCursorPos(1, 3)
	mon.write("Working")
	
	os.sleep(3)
	redstone.setOutput("top", false)
end