Jump to content




Need help

help lua

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

#1 TheNoim

  • New Members
  • 2 posts

Posted 09 April 2015 - 06: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.

#2 Lupus590

  • Members
  • 2,028 posts
  • LocationUK

Posted 09 April 2015 - 06: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 by Lupus590, 09 April 2015 - 06:57 PM.


#3 Square789

  • Members
  • 39 posts
  • LocationUniverse:C:/MilkyWay/Sol/Earth/Europe/Germany

Posted 09 April 2015 - 06: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 by Square789, 15 April 2015 - 05:41 PM.


#4 HPWebcamAble

  • Members
  • 933 posts
  • LocationWeb Development

Posted 09 April 2015 - 06: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 by HPWebcamAble, 09 April 2015 - 06:59 PM.


#5 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 09 April 2015 - 07: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 by KingofGamesYami, 09 April 2015 - 07:02 PM.


#6 TheNoim

  • New Members
  • 2 posts

Posted 09 April 2015 - 07:55 PM

Thanke you,
that is my finished Program: http://pastebin.com/T0FGfjpq
@KingofGamesYami Yes that is German. ;)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users