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

Need help

Started by TheNoim, 09 April 2015 - 05:27 PM
TheNoim #1
Posted 09 April 2015 - 07:27 PM
Hello,

i want to program an light off and light on Programm, but I do not get ahead.
This is what i already have did: http://pastebin.com/Pmpje9ay

I hope that you can helpe me.

With kind regards, Nils.
Lupus590 #2
Posted 09 April 2015 - 07:56 PM

--# add to the top of your main function
function main() --#you have this line, it's just here for reference
local input = read()

--#change last line to
while true do
   main()
   sleep(0.1)
end
Edited on 09 April 2015 - 05:57 PM
Square789 #3
Posted 09 April 2015 - 07:57 PM
I'd do it like this:

function setOn()
  rs.setOutput("back", true)
end
function setOff()
  rs.setOutput("back", false)
end
function clear()
  term.clear()
  term.setCursorPos(1,1)
end
function printOn()
  print("Licht Status: An")
end
function printOff()
  print("Licht Status: Aus")
end
function main()
clear()
input = read()
  if input == "An" or input == "an" then
	clear()
	setOn()
	printOn()
	main()
  elseif input == "Aus" or input == "aus" then
	clear()
	setOff()
	printOff()
	main()
  else
	clear()
	print("Gib entweder ,,An'' oder ,,Aus'' ein!")
	os.sleep(3)
	main()
  end
end
There are two three main errors in your program:
  • You only set the input variable once wit read() and then never change it again, so the user can't change it without restarting the program.
  • You didn't define "An" or "Aus" as a string so the program looks up for the variables An and Aus which doesn't exist.
  • You defined the functions as ,,setoff" and ,,seton" but then called the functions ,,setOff" and ,,setOn"
Also, the pos() function is never called.
I also fixed some ,,stylish" errors.
Edited on 15 April 2015 - 04:41 PM
HPWebcamAble #4
Posted 09 April 2015 - 07:58 PM
Your 'main' function is recursive, which means it calls itself. Its best to avoid that if you can.

Instead use a while-loop:

while <condition> do --# Replace <condition> with your conditional statement

end

--# Here's a while loop that runs forever:
while true do

end

Here is your code, but touched up a bit:

function seton()
  rs.setOutput("back", true)
end

function setoff()
  rs.setOutput("back", false)
end

function clear()
  term.clear()
  term.setCursorPos(1,1)
end

function pos(x , y)
  term.setCursorPos(x,y)
end

function printOn()
  print("Licht Startus: An")
end

function printOff()
  print("Licht Startus: Aus")
end

while true do --# Runs forever
  clear()
  local input = read() --# Gets user input (local means only this program can access the variable)

  if input == "An" then --# Quotes ("") makes it a string, not quotes makes it a variable
	clear()
	setOn()
	printOn()
  elseif input == "Aus" then
	clear()
	setOff()
	printOff()
  end

end

Edit: DOUBLE ninjad
Edited on 09 April 2015 - 05:59 PM
KingofGamesYami #5
Posted 09 April 2015 - 08:01 PM
read() returns a string, which must be compared to another string. To solve this, you must put quotes around the text ("").

Additionally, localizing your variables is a good idea, as I've done on the first line. You can also localize your functions

local input = read() --#here
function seton()
  rs.setOutput("back", true)
end
function setoff()
  rs.setOutput("back", false)
end
function clear()
  term.clear()
  term.setCursorPos(1,1)
end
function pos(x , y)
  term.setCursorPos(x,y)
end
function printOn()
  print("Licht Startus: An")
end
function printOff()
  print("Licht Startus: Aus")
end
function main()
  if input=="An" then --#here
    clear()
    setOn()
    printOn()
    main()
  elseif input=="Aus" then --#here
    clear()
    setOff()
    printOff()
    main()
  end
end
clear()
printOff()
main()

PS: Is that German (Deutsch)? Ich spreche ein bisschen Deutsch.

Edit: triple ninja…
Edited on 09 April 2015 - 06:02 PM
TheNoim #6
Posted 09 April 2015 - 08:55 PM
Thanke you,
that is my finished Program: http://pastebin.com/T0FGfjpq
@KingofGamesYami Yes that is German. ;)/>