generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Coroutines...

Previous Thread :: Next Thread 
Davidii is not online. 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 is not online. 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
Radioaktiivinen is not online. Radioaktiivinen
Joined: 25 Apr 2009
Total Posts: 18629
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 is not online. 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 is not online. stravant
Forum Moderator
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 is not online. stravant
Forum Moderator
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 is not online. 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
xSIXxStorage is not online. xSIXxStorage
Joined: 22 Aug 2010
Total Posts: 1811
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 is not online. smurf279
Joined: 15 Mar 2010
Total Posts: 6871
18 Feb 2012 04:57 PM
Coroutines also act as error-handlers ^_^
Report Abuse
redditor is not online. 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
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image