1

This is my code https://pastebin.com/fnZreFKA I have tried all the coroutine options, I have print statements at the start of each of the two functions, it prints, but it doesn't do anything in the loop

coroutine.wrap(constantWork)()
coroutine.wrap(lookForKeys)()

The loops start after line 170

Epuuc
  • 53
  • 5

2 Answers2

3

Because they are not detached threads, they are green synchronous threads, only one of them will run the code at the time.

To simulate multitasking you forgot to use yield. coroutine.yield pauses the thread and runs the next code after you called the coroutine. You can resume the coroutine later on by calling wrapped coroutine again or using coroutine.resume if you created it using coroutine.create.

Read the documentation here: https://www.lua.org/pil/9.html

Spar
  • 1,582
  • 9
  • 16
  • Where could I put coroutine.yield and coroutine.create in my code to make it work? – Epuuc Nov 28 '20 at 23:09
  • @Epuuc By the freedom of choice: anywhere you want in the coroutine function. In real usage of coroutine you usually yield after doing some part of code to give some air for other code to run. External C modules and async lua implementations (like Luvit) use coroutines to support async felling. – Spar Nov 28 '20 at 23:13
  • My event waits until there's an event, what if no events happen, and the code is running, would it stop the mining? – Epuuc Nov 28 '20 at 23:17
  • I have a coroutine.yield() in both the while loops at the end, and I have two coroutine.resume()'s at the bottom, and it just prints then ends the script. – Epuuc Nov 28 '20 at 23:20
  • You now need to resume each coroutine again and again. I'm not familiar if lua scripts in computercraft are freezing the game or not. – Spar Nov 28 '20 at 23:24
  • So for the variables there's coroutine.create(func..etc) So I just need to while loop coroutine.resume(var)? – Epuuc Nov 28 '20 at 23:25
  • I tried to while loop the coroutine.resume() but I'm getting the same issue I had with parallel.waitForAll(func1,func2) – Epuuc Nov 28 '20 at 23:29
  • Don't know then, this would be possible only if the script is not blocking or they support async – Spar Nov 28 '20 at 23:33
  • Do you know any other methods that would let me run two loops at the same time? Because in your answer you were saying something about detached threads, and green synchronous threads. – Epuuc Nov 28 '20 at 23:34
  • Lua by itself doesn't provide anything for simultaneous execution. You can simulate it by yielding in coroutines to give other function to do their stuff. – Spar Nov 28 '20 at 23:43
  • Try to find something in computercraft documentation if they have something for it – Spar Nov 28 '20 at 23:44
1

coroutine.wrap creates a new coroutine based on the function you passed it, and then creates a new function based on the coroutine. The first time you call it, it calls the original function until it yields. The next time, it returns from the yield and runs until the next yield. And so on.

In ComputerCraft, yielding is the same as waiting for an event.

ComputerCraft comes with the parallel library which runs two or more functions as coroutines in parallel. It does all the work for you.

You can use parallel.waitForAll or parallel.waitForAny, depending on when you want it to return.

Usage: parallel.waitForAll(constantWork, lookForKeys)

user253751
  • 57,427
  • 7
  • 48
  • 90