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

Why isn't this working?

Started by X3ME, 20 November 2014 - 08:37 PM
X3ME #1
Posted 20 November 2014 - 09:37 PM
Hi, i have made a door bell program with a override function, but it's not working..
It's suppost to open the door when it receives an input from the server (top computer) or when it has override enabled..

But it doesn´t wokring! Why is lua so frustating??


os.pullEvent = os.pullEventRaw
local accept = true
local denied = false
local opentime = 5
local password = "changeme"
while true do
term.clear()
term.setCursorPos(1,1)
write ("Enter Override:")
local input = read("*")
function rsevent()
if rs.getInput ("top") then
os.pullEvent( "redstone" )
rs.setOutput ("left",accept)
os.pullEvent = pullEvent
sleep(opentime)
rs.setOutput ("left",denied)
end

elseif input == password then
rs.setOutput ("left",accept)
term.clear()
term.setCursorPos(1,1)
print ("Password Correct")
sleep(opentime)
rs.setOutput ("left",denied)

else
rsevent()

end

Thanks,
valithor #2
Posted 20 November 2014 - 10:35 PM
You may want to do a few basic lua tutorials before attempting to write a script such as this.

1.  os.pullEvent = os.pullEventRaw
2.  local accept = true
3.  local denied = false
4.  local opentime = 5
5.  local password = "changeme"
6.  while true do
7.	term.clear()
8.	term.setCursorPos(1,1)
9.	write ("Enter Override:")
10.  local input = read("*")
11.  function rsevent()
12.	if rs.getInput ("top") then
13.	  os.pullEvent( "redstone" )
14.	  rs.setOutput ("left",accept)
15.	  os.pullEvent = pullEvent
16.	  sleep(opentime)
17.	  rs.setOutput ("left",denied)
18.	end
19.	elseif input == password then
20.	  rs.setOutput ("left",accept)
21.	  term.clear()
22.	  term.setCursorPos(1,1)
23.	  print ("Password Correct")
24.	  sleep(opentime)
25.	  rs.setOutput ("left",denied)
26.	else
27.	  rsevent()
28.	end

The first thing that stands out to me is where you define the rsevent() function on line 11. This function is defined in the middle of a while loop which is something you should not/ can not do. This function should be defined above the while true do loop.

The next thing is lines 12, 13, and 15. First you check if the computer is receiving input from the top (line 11), then you tell the computer to wait until the redstone state changes on any side of the computer(12), then on line 15 you set os.pullEvent to a nil value. Lines 13 and 15 should be removed.

On line 18 you end the if statement, which makes it to where the elseif statement on line 19 will error. This end statement should be removed.

The code i posted is not fixed. I posted it simply so I would have line numbers to easily reference.
Edited on 20 November 2014 - 09:36 PM
Dog #3
Posted 20 November 2014 - 11:01 PM
To add to valithor's comments, a couple things I noticed:

1. The first thing you do is redefine os.pullEvent as os.pullEventRaw, then on line 15 you redefine os.pullEvent as pullEvent which isn't defined anywhere.

2. With your code properly indented, it also appears that you are missing at least 2 ends at the end of your code.

3. I also noticed that you are calling rsevent() from within rsevent() - this can cause the program to keep spawning more and more instances of rsevent() until the computer crashes (I don't know if there's a recursive or memory limit in CC).


I would second valithor's recommendation and suggest you start with the Ask a Pro Renewal Project. The OP has a spoiler with a lot of great tutorials just waiting to be discovered :)/>
Edited on 20 November 2014 - 10:02 PM
HDeffo #4
Posted 25 November 2014 - 08:44 PM
3. I also noticed that you are calling rsevent() from within rsevent() - this can cause the program to keep spawning more and more instances of rsevent() until the computer crashes (I don't know if there's a recursive or memory limit in CC)
The recursive limit won't apply if the loop sleeps at all or has a long enough nested loop(which it reads as "yielding" for some reason) which at a quick glance his would probably still hit that continued too long without yielding error.

The memory limit he would only hit if it kept adding to a variable or adding more variables simply calling a function over and over on top of itself won't really affect the memory limit or at least not enough to crash the program. Last time I checked to hit the limit it would require a large enough variable that a string for instance returns string.len() as less than 0.
KingofGamesYami #5
Posted 25 November 2014 - 09:08 PM
@HDeffo

Run this, and see what happens…

function foo()
  os.queueEvent( 'foo' )
  os.pullEvent( 'foo' )
  foo()
end
foo()
Edited on 25 November 2014 - 08:38 PM
KingofGamesYami #6
Posted 25 November 2014 - 11:55 PM
… that doesn't bypass the no yield. os.pullEvent yeilds. I was demonstrating that more than 255 (I think its 255) function calls stacked on top of each other will error, since you didn't seem to have a handle on how that works.
Edited on 25 November 2014 - 10:56 PM
valithor #7
Posted 26 November 2014 - 12:05 AM
… that doesn't bypass the no yield. os.pullEvent yeilds. I was demonstrating that more than 255 (I think its 255) function calls stacked on top of each other will error, since you didn't seem to have a handle on how that works.

He has confused/related two errors. The 10 second yield error, and the 255 stack error (the one you are talking about).

-snip-

The error that dog originally was talking about is in no way related to the 10 seconds/to long without yielding error that you are talking about. The code which KingofGamesYami posted demostrates the original error, which Dog was talking about. I would suggest running it and seeing what error code it says, seeing as you are thinking of a different error.


edit:
10 second yield error: A program in CC must yield at least once every 10 seconds, or will throw this error. This is to prevent one computer from preventing other CC computers from running.

255 stack error: Don't know if this one is only in CC, but when a function is called it is added to a table. If the original function that is called calls another function it is added to the table. (This is the easiest way i could think of to explain it) This table will be added to until the first function, which was called, ends. If this table reaches a length of 256 (1 over 255) the program will crash. In King's example the function calls itself over and over, so another function is always called before the previous one ends. Because of this the program will crash from the 255 stack error.
Edited by
theoriginalbit #8
Posted 26 November 2014 - 12:24 AM
-snip-
So far everyone has just been speculating as to what the problem could be, please tell us exactly what is 'not working' so that we can further assist you. Is there an error message, or does it just not behave as expected? if it's an error, what is the full error message? if it's not running as expected, what do you expect it to do, and what does it actually do? we need more information to assist you better.

The first thing that stands out to me is where you define the rsevent() function on line 11. This function is defined in the middle of a while loop which is something you should not/ can not do. This function should be defined above the while true do loop.
this is possible to do within Lua, can you assign variables inside a while loop? yes you can. So why would assigning a function to a variable suddenly be something you cannot do. The following code works perfectly fine!

while true do
  function a()
    print("hi")
  end
  a()
  sleep(0)
end
yes it's not exactly efficient, you're recreating the function each time the loop runs, but it is still perfectly valid code.

3. I also noticed that you are calling rsevent() from within rsevent() - this can cause the program to keep spawning more and more instances of rsevent() until the computer crashes (I don't know if there's a recursive or memory limit in CC).
Yes there is a recursive limit in ComputerCraft, that's the cause of the stack overflow error.

Don't know if this one is only in CC
No, a stack overflow isn't exclusive to ComputerCraft.
valithor #9
Posted 26 November 2014 - 12:53 AM
-snip

Said the thing about defining functions since it is extremely inefficent, and bad practice.

Some reason thought that CC lowered the stack overflow, so that is why i said that.

A error message honestly wouldn't help with this program. Without running it, I would assume that the program would say something about it missing 2 ends at the end, or it having a else statement with no if to continue from. We have pretty much addressed everything that is wrong with it, the only problem is that he has not responded. This topic was for the most part dead until it was bumped by hdeffo.
Bomb Bloke #10
Posted 26 November 2014 - 01:06 AM
Said the thing about defining functions since it is extremely inefficent, and bad practice.

In most of the cases you see on these forums (like in the OP's post), sure, in-function function definitions are used poorly. That doesn't mean that they're a bad thing in themselves, or that they aren't sometimes the best tool for the job. It's a case of "inefficient compared to what?".
valithor #11
Posted 26 November 2014 - 01:34 AM
Said the thing about defining functions since it is extremely inefficent, and bad practice.

In most of the cases you see on these forums (like in the OP's post), sure, in-function function definitions are used poorly. That doesn't mean that they're a bad thing in themselves, or that they aren't sometimes the best tool for the job. It's a case of "inefficient compared to what?".

Well he declared it in a while loop not a function. I personally can not think of a single instance in which declaring a function in a while loop would be better than declaring it above the loop. It is better to say that you shouldn't do something, than just ignore that it is extremely inefficent to redeclare a function everytime a while loop runs (without changing anything).