71 posts
Location
Latvia
Posted 20 October 2012 - 12:04 AM
Hello.
My lights system is failing to launch properly.I am getting error every time I try to launch it and I can't seem to find the problem, but I guess it got to do something with functions.
Any help would be appreciated.
Here's to code:
on = "1"
off = "2"
menu()
function menu()
term.clear()
term.setCursorPos(1,1)
print(" +--------------+")
print(" :Lights Control:")
print(" +--------------+")
print(" :1.To turn ON. :")
print(" :2.To turn OFF.:")
print(" +--------------+")
write("Your Choice:")
input = read()
if input == on then on() end
elseif input == off then off()
else menu()
end
function on()
term.clearLine()
print("Lights are turning on")
rs.setOutput("left",true)
sleep(1)
menu()
end
function off()
term.clearLine()
print("Lights are turning on")
rs.setOutput("left",false)
sleep(1)
menu()
end
And here is the picture of the error:
Spoiler
I couldn't add png extension to this forum, so you'll have to open this link.
http://imageshack.us/photo/my-images/708/20121020015613.png/Thank you.
715 posts
Posted 20 October 2012 - 12:17 AM
Remove the 'end' of the following line (to make the 'elseif' of the line after that part of it again):
if input == on then on() end
It's always
If … elseif … end and not
If … end … elseif … end.
Or in other words: An
If only ends once, and any
elseif's are between the
If and the
end.
2217 posts
Location
3232235883
Posted 20 October 2012 - 12:40 AM
also on sould be "on" when comparing it to input
71 posts
Location
Latvia
Posted 20 October 2012 - 08:11 AM
Remove the 'end' of the following line (to make the 'elseif' of the line after that part of it again):
if input == on then on() end
It's always
If … elseif … end and not
If … end … elseif … end.
Or in other words: An
If only ends once, and any
elseif's are between the
If and the
end.
also on sould be "on" when comparing it to input
Thank you for helping, but now when I fixed that misplaced end I get an error:
startup:3: attempt to call nil.
I tried to edit the code, but with no luck so far.So where is the problem now?
Thank you.
2005 posts
Posted 20 October 2012 - 08:31 AM
You didn't have an extra end, you had an end in the wrong place. By removing the end, instead of moving it where it belonged, you made it so that the function definition of menu() doesn't close and is thus undefined.
Of course I'm just guessing, because you didn't post the modified code.
715 posts
Posted 20 October 2012 - 12:04 PM
[…]
Thank you for helping, but now when I fixed that misplaced end I get an error: startup:3: attempt to call nil.
I tried to edit the code, but with no luck so far.So where is the problem now?
Thank you.
It tells you that on line 3 it attempted to call nil.
"Attempt to call nil" is an indicator for trying to call a function that doesn't exist.
In your case on line 3 there is "menu()".
But you haven't written the function up to that point. Only
after your call to menu() did you write the actual method.
The problem is that in Lua you have to define a function before you call it.
So in this case, just move the call to menu()
below the actual implementation of the function.
Then that error shouldn't appear anymore.
Cheers :)/>/>
71 posts
Location
Latvia
Posted 20 October 2012 - 01:43 PM
[…]
Thank you for helping, but now when I fixed that misplaced end I get an error: startup:3: attempt to call nil.
I tried to edit the code, but with no luck so far.So where is the problem now?
Thank you.
It tells you that on line 3 it attempted to call nil.
"Attempt to call nil" is an indicator for trying to call a function that doesn't exist.
In your case on line 3 there is "menu()".
But you haven't written the function up to that point. Only
after your call to menu() did you write the actual method.
The problem is that in Lua you have to define a function before you call it.
So in this case, just move the call to menu()
below the actual implementation of the function.
Then that error shouldn't appear anymore.
Cheers :)/>/>
Thank you, this worked.
1054 posts
Posted 20 October 2012 - 02:22 PM
also on sould be "on" when comparing it to input
No it shouldn't, on is a variable defined as the string "1". And that's what he's polling for. (So you did that right exploder :)/>/> )