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: I still haven't mastered coroutines.

Previous Thread :: Next Thread 
JimmyChance is not online. JimmyChance
Joined: 01 Nov 2009
Total Posts: 3681
08 Mar 2015 01:24 PM
I tried (for probably months checking on and off the wiki generally) to grasp coroutines, but it is one of those subjects I can't really understand. Can someone tell me if there are any better sources than the documentation or script book chapter on them, or explain it themselves? This is all I can understand:

Coroutines allow you to execute multiple functions and loops at the same time.

And three functions:

coroutine.create(f) -- creates a new thread (don't know if correct)
coroutine.resume(f) -- starts the thread created ^
coroutine.wrap(f) -- works like the above two combined ^

But that is about all I can see into it. And what I said probably is misguided or incorrect.
Report Abuse
CrescentJade is not online. CrescentJade
Joined: 07 Jul 2010
Total Posts: 5494
08 Mar 2015 01:27 PM
just think of them as functions that run while everything else runs, but can still change the variables within the script.

Like..

coroutine = coroutine.wrap(function() print'hi' end)

coroutine()

coroutine = coroutine.create(function() print'hi' end)

coroutine.resume(coroutine)



Report Abuse
nicemike40 is not online. nicemike40
Joined: 19 Dec 2008
Total Posts: 1814
08 Mar 2015 01:31 PM
A pretty basic explanation is that a coroutine creates a new section of code that ROBLOX will run simultaneously with all the other sections of code (i.e. other coroutines).

Flip through this, it contains some good info: http://www.lua.org/pil/9.html

~Upload code to codepad-dot-org with Lua syntax highlighting to preserve indentation and make it easier to read!~
Report Abuse
kingkiller1000 is not online. kingkiller1000
Joined: 12 Dec 2008
Total Posts: 26415
08 Mar 2015 01:32 PM
Your understanding of coroutine.wrap is incorrect. All it does is return a coroutine that resumes each time it is called. It usually has a yield in it.

For example:

local f = coroutine.wrap(function()
local i = 1
while true do
print(i)
i = i + 1
coroutine.yield()
end
end)

f()
f()
f()
f()

> 1
> 2
> 3
> 4
Report Abuse
kingkiller1000 is not online. kingkiller1000
Joined: 12 Dec 2008
Total Posts: 26415
08 Mar 2015 01:33 PM
> return a coroutine that resumes each time it is called.

*return a function that resumes the coroutine each time it is called.
Report Abuse
JimmyChance is not online. JimmyChance
Joined: 01 Nov 2009
Total Posts: 3681
08 Mar 2015 01:51 PM
Thank you for the explanations.
Report Abuse
eLunate is not online. eLunate
Joined: 29 Jul 2014
Total Posts: 13268
08 Mar 2015 02:13 PM
If you're using coroutines then you're still not using them right :P
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