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

Help with paralell ( and possibly events)

Started by Apfeldstrudel, 26 May 2013 - 03:01 AM
Apfeldstrudel #1
Posted 26 May 2013 - 05:01 AM
Hello, i am testing the paralell api and os.pullEvent,
The (test) program is supposed to print "I am RUNNING! (1)" "I am RUNNING (2)" etc until you terminate it
code:

function t1()
  while true do
	print("I am RUNNING! ("..i..")")
	i = i+1
	sleep(0.5)
  end
end

function t2()
  run = true
  while run == true do
	event = os.pullEvent("terminate")
	if event == "terminate" then
	  run = false
	  print("Wee, it WORKS!")
	end
  end
end

parallel.waitForAny(t1,t2)
I get the error paralell:22: test:3: attempt to concatenate nil and string



Thank you
Shnupbups #2
Posted 26 May 2013 - 05:16 AM

function t1()
  local i = 1 --#Added this line
  while true do
	print("I am RUNNING! ("..i..")")
	i = i+1
	sleep(0.5)
  end
end
function t2()
  run = true
  while run == true do
	event = os.pullEvent("terminate")
	if event == "terminate" then
		  run = false
		  print("Wee, it WORKS!")
	end
  end
end
parallel.waitForAny(t1,t2)
That should fix it. 'i' didn't exist the first time you tried to print it, resulting in it being nil. You were trying to concatenate nil and a string, just like the error says.
Apfeldstrudel #3
Posted 26 May 2013 - 05:24 AM
Okay, cool- i have a new question. can you call a function from inside it? ex:

function name()
name()
end
If it doesn't work, are there any other ways to do it?
Edit: Wont get anser here- new topicifying
Bomb Bloke #4
Posted 26 May 2013 - 05:42 AM
Yes, you can do that, but in your particular example it'll quickly (as in, near instantly) overflow and crash. The Lua interpreter needs to keep track of each function call (including each call to the same function), and as written, your function calls never end - they just open up more instances; so Lua's tracking name() calling name() calling name() calling name() etc until it gives up and throws its hands in the air.

So, if you must call functions recursively, make sure that they will eventually stop doing so before things get out of hand. As an example, you might use recursion to help a turtle path find (have a function check what blocks a given block can lead to, then have it call itself a few times to see where those NEXT blocks can lead to, and so on), but if you hit an overflow before it determines its path the program will crash.

The actual upper limit seems to be 256 function calls open at a time, so you've got a lot of room to play with so long as your calls are eventually ending.
Bubba #5
Posted 27 May 2013 - 12:14 AM
-snip-


To expand upon this, in order to "end" the instance that is opened when using recursion, you can use return.
For example:

function b()
  print("Function b")
  return a()
end

function a()
  print("Function a")
  return b()
end

print("Never ending recursion.")
a()

The above code works, whereas the following will not:

function b()
  print("Function b")
  a()
end

function a()
  print("Function a")
  b()
end

print("This will stop after 256 iterations")
a()