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

How to not run a loop contained in a function unless the function is called?

Started by Fashter, 28 October 2012 - 08:05 AM
Fashter #1
Posted 28 October 2012 - 09:05 AM
Read down below for more

I got a question about a program i'm trying to make. I put all my functions before the main program to make sure that they load first. But, as the program moves down lines, it comes across a while loop in the function and seems to get stuck in that loop before the main program starts and calls that function. How do I make it so the while loop will not start unless the function (in this case readASCII) is called? Hopefully this makes enough sense for someone to answer my question.

Here is my code:

function readASCII()
	num = redstone.getBundledInput( bUSide )
if num == 4 then
  return end
end
	num = tostring( num )
	append = readVal( num )
	--msg = msg..append
	rs.setOutput( cSide, true )
	while true do
  os.pullEvent( "redstone" )
		if rs.getInput( aSide ) == true then
			rs.setOutput( cSide, false )
			return true
		else
			sleep( 1 )
			aCount = aCount+1
			if aCount == 10 then
				return false
  end
end
end
ChunLing #2
Posted 28 October 2012 - 09:38 AM
The function doesn't execute without being called. It doesn't matter how inane a function is, it can contain a hundred nested infinite loops where the function calls itself at every level, but if nothing else calls the function then it never gets called.

Put some debugging prints in your program to track your program flow, particularly just before this program gets called and inside of the part that you think is causing the problem.
Fashter #3
Posted 28 October 2012 - 09:49 AM
I did before I posted this, and it printed right up to the function. I'm not calling the function at all, but it's still running that loop (I know it is because it's executing everything I told that loop to-do). It's an obscure bug but here is my entire code if you're interested.

EDIT: It's also executing the other code in that function… If you don't believe that I'm not calling that function, feel free to comment out any lines that isn't nested in a function.

EDIT 2: Try this code too, it has everything that's either unnecessary or part of the main program commented out, so you know that the function (or any function) is not ever being called, but still running.
huettner94 #4
Posted 28 October 2012 - 10:18 AM
Hi Fashter,

the Problem should be pretty ease to solve: (Line Numbers are the ones from the second code)

in line 124 you close the if statement, so your end in line 125 closes the function. Because of that the while part will get executed as normal code.

Solution:
1. Delete the "end" in Line 124
2. Add "end" to Line 147 to close readASCII

I hope that will solve you problem.
Fashter #5
Posted 28 October 2012 - 10:33 AM
Wow, thanks guy! This forum is just so lovely :D/>/>
huettner94 #6
Posted 28 October 2012 - 10:47 AM
No Problem

Glad that i could help you :D/>/>