Davidii
|
  |
| Joined: 17 Jul 2008 |
| Total Posts: 1282 |
|
|
| 18 Feb 2012 08:07 AM |
| No, I do not know what they are. I have an idea, but I would much prefer someone explain them to me. What are they, what are their uses, and please provide some example code. |
|
|
| Report Abuse |
|
|
Legend26
|
  |
| Joined: 08 Sep 2008 |
| Total Posts: 10586 |
|
|
| 18 Feb 2012 08:11 AM |
http://wiki.roblox.com/index.php/Beginners_Guide_to_Coroutines
^^ |
|
|
| Report Abuse |
|
|
|
| 18 Feb 2012 10:44 AM |
OR do
print("lol") Spawn(function() wait(1) print("hai") end) print("wuzzup")
> lol > wuzzup > hai
way easier than hacky coroutines. |
|
|
| Report Abuse |
|
|
nomatters
|
  |
| Joined: 09 Feb 2012 |
| Total Posts: 10 |
|
|
| 18 Feb 2012 12:11 PM |
print("lol") Spawn(function() wait(1) print("hai") end) print("wuzzup")
you never closed Spawn? i never knew what did so correct me if im wrong q.q |
|
|
| Report Abuse |
|
|
stravant
|
  |
 |
| Joined: 22 Oct 2007 |
| Total Posts: 2893 |
|
|
| 18 Feb 2012 03:22 PM |
Or a more high level description:
They are alternate threads of execution in Lua. In Lua every piece of executing code has to have a stack of registers to store which functions called which, and to store the values of local variables. Every single execution state (such as a script, or a function called by an event handler) has to have a coroutine backing it.
All of the threading constructs in Lua are really backed by coroutines, although the mechanisms are a bit more hidden for the top-level thread Roblox creates, and they are managed by the Roblox engine to execute once every frame, or when the current wait is done. |
|
|
| Report Abuse |
|
|
stravant
|
  |
 |
| Joined: 22 Oct 2007 |
| Total Posts: 2893 |
|
|
| 18 Feb 2012 03:25 PM |
"way easier than hacky coroutines."
Also, what should be used in most of the places where people use coroutines. coroutines shouldn't be used other than for implementing more low level constructs that require fine control. If you just need to spawn a new thread of execution then using Spawn() to tap into Roblox's built in thread scheduler is the way to go.
See the Lua-side thread scheduler example I posted here for an example of sort of how Roblox could do thread scheduling internally: http://www.roblox.com/Forum/ShowPost.aspx?PostID=62694413 |
|
|
| Report Abuse |
|
|
pighead10
|
  |
| Joined: 03 May 2009 |
| Total Posts: 10341 |
|
|
| 18 Feb 2012 04:09 PM |
| I didn't even know there was a Spawn() function. I thought my coroutines were too hacky =/ |
|
|
| Report Abuse |
|
|
|
| 18 Feb 2012 04:40 PM |
Say you want to run two loops at a time...
while true do print("lol") wait() end while true do print("lol2") wait(1) end
-- that will only execute one loop
local lol = function() while true do print("lol") wait() end end
local lol2 = function() while true do print("lol2") wait(1) end end
local lolcoroutine = coroutine.create(lol) local lolcoroutine2 = coroutine.create(lol2)
coroutine.resume(lolcoroutine) coroutine.resume(lolcoroutine2)
-- I know there is obviously less steps to do this, but I wanted to break it down for you. That script above will run the loops at the same time without using more than one script. |
|
|
| Report Abuse |
|
|
smurf279
|
  |
| Joined: 15 Mar 2010 |
| Total Posts: 6871 |
|
|
| 18 Feb 2012 04:57 PM |
| Coroutines also act as error-handlers ^_^ |
|
|
| Report Abuse |
|
|
redditor
|
  |
| Joined: 26 Jul 2011 |
| Total Posts: 182 |
|
|
| 18 Feb 2012 07:56 PM |
These are some of the most useful things I've learned from this forum. This is awesome.
Time to use and abuse Spawn(). |
|
|
| Report Abuse |
|
|