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: Stoppable Coroutines

Previous Thread :: Next Thread 
HotThoth is not online. HotThoth
Forum Moderator
Joined: 24 Aug 2010
Total Posts: 1176
06 Nov 2012 01:43 PM
I've seen quite a few questions on how to do something like this, and I have a way which works pretty well for me. So since sharing is caring, I figured I'd share. Basically, one way to do most simple things along this line of inquiry is (like everything else in Lua)... tables!

Here's a demo of a spawnRepeatingCoroutine and stopCoroutine function in action:

local coroutines = {}
function coroutineWrapper(myFunction, ...)
return function(myCoroutine, ...) while coroutines[myCoroutine] do myFunction(...) end end
end

function spawnRepeatingCoroutine(myFunction, ...)
local newCoroutine = coroutine.create(coroutineWrapper(myFunction, ...))
coroutines[newCoroutine] = true
coroutine.resume(newCoroutine, newCoroutine, ...)
return newCoroutine
end

function stopCoroutine(myCoroutine)
coroutines[myCoroutine] = nil
end

function printMessageAndWait(messageToPrint)
print(messageToPrint)
wait(1)
end

local coroutineA = spawnRepeatingCoroutine(printMessageAndWait, "Hello world")
local coroutineB = spawnRepeatingCoroutine(printMessageAndWait, "Pizza")
local success, coroutineC = coroutine.resume(coroutine.create(spawnRepeatingCoroutine), printMessageAndWait, "Yo dawg")

wait(3)
stopCoroutine(coroutineC)
wait(5)
stopCoroutine(coroutineA)





Hope that helps,


- HotThoth

~ I Thoth so ~
Report Abuse
BAUER102 is online. BAUER102
Joined: 03 Apr 2010
Total Posts: 5936
06 Nov 2012 01:51 PM
Mhm, I shall run some tests with it.

Thank you! :)
Report Abuse
thumper10 is not online. thumper10
Joined: 17 Apr 2009
Total Posts: 3304
06 Nov 2012 02:03 PM
Thank you very much! I've been looking for this!
It works great, no problems so far. I'm going to stress test it to see if it has a breaking point :P

- thumper10, C# Lover
Report Abuse
Vitouliss14 is not online. Vitouliss14
Joined: 18 Mar 2009
Total Posts: 7918
06 Nov 2012 03:12 PM
This is a rather nice thing to have.

Thanks.
Report Abuse
iMyFriendlyPony is not online. iMyFriendlyPony
Joined: 02 Nov 2012
Total Posts: 47
06 Nov 2012 04:29 PM
Tracked.
Good work, but this should be a implemented function.
Report Abuse
MrNicNac is not online. MrNicNac
Joined: 29 Aug 2008
Total Posts: 26567
06 Nov 2012 04:42 PM
Yes, it's very well and nice to see this small and simple method of performing a, sometimes, elusive task.

The concept will definitely be memorized for me, because sometimes it's not exactly knowing enough Lua to do something - but getting creative enough to make something with it.
Report Abuse
xSIXx is not online. xSIXx
Joined: 06 Aug 2010
Total Posts: 9202
06 Nov 2012 05:07 PM
instead of just a reply from this mod for my question, he gave everyone a whole thread.

fav mod k.
Report Abuse
HotThoth is not online. HotThoth
Forum Moderator
Joined: 24 Aug 2010
Total Posts: 1176
06 Nov 2012 05:10 PM
I think it'd be useful eventually to have a whole bunch of simple data structures too (sets/stacks/queues/ring buffers/trees), but until public libraries are implemented, I'll probably just make random helper/starter scripts to help people out with the details, since there's no good way to standardize a lot of this stuff otherwise (and most of it's not really stuff that we want to be putting into the engine).

- HotThoth
Report Abuse
Legend26 is not online. Legend26
Joined: 08 Sep 2008
Total Posts: 10586
06 Nov 2012 06:00 PM
Will scripters be able to make libraries too? I'm guessing no... :/ I also thought libraries were already implemented. How are they not?
Report Abuse
HotThoth is not online. HotThoth
Forum Moderator
Joined: 24 Aug 2010
Total Posts: 1176
06 Nov 2012 06:58 PM
One version of them was implemented, but not for public consumption. A public version was experimented with by one of the interns, but is not currently under development afaik.

- HotThoth
Report Abuse
HotThoth is not online. HotThoth
Forum Moderator
Joined: 24 Aug 2010
Total Posts: 1176
06 Nov 2012 07:13 PM
Also, fun little extension while I was thinking about it. Yes it may add wait(.01) loops or extra if-statements. But in the cases I want this, it's definitely worth the (almost 0) computation cost.

Specifically, I added a quick pause/resume to it:

local coroutines = {}
local yieldingCoroutines = {}

function coroutineWrapper(myFunction, ...)
return function(myCoroutine, ...) while coroutines[myCoroutine] do if yieldingCoroutines[myCoroutine] then wait(.01) else myFunction(...) end end end
end

function spawnRepeatingCoroutine(myFunction, ...)
local newCoroutine = coroutine.create(coroutineWrapper(myFunction, ...))
coroutines[newCoroutine] = true
coroutine.resume(newCoroutine, newCoroutine, ...)
return newCoroutine
end

function stopCoroutine(myCoroutine)
coroutines[myCoroutine] = nil
end

function pauseCoroutine(myCoroutine)
yieldingCoroutines[myCoroutine] = true
end

function resumeCoroutine(myCoroutine)
yieldingCoroutines[myCoroutine] = nil
end

function printMessageAndWait(messageToPrint)
print(messageToPrint)
wait(1)
end

local coroutineA = spawnRepeatingCoroutine(printMessageAndWait, "Hello world")
wait(3)
pauseCoroutine(coroutineA)
wait(3)
resumeCoroutine(coroutineA)


- HotThoth
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