|
| 06 Apr 2013 03:06 PM |
Say I have a function that needs to run every minute. I have two options.
One: function myFunc() -- code wait(30) myFunc() end
coroutine.resume(coroutine.create(myFunc))
Two: function myFunc() -- code end
coroutine.resume(coroutine.create(function repeat myFunc() until not wait(60) end))
The first one is my style, so naturally I want to use it. But I was thinking, if it runs fifty hundred times, it could end up using massive amounts of memory. Would that happen / is #2 better? |
|
|
| Report Abuse |
|
|
|
| 06 Apr 2013 03:06 PM |
| Messed up on the first one's code. You know what I mean. |
|
|
| Report Abuse |
|
|
|
| 06 Apr 2013 03:07 PM |
| Golly, that's a lot of views. |
|
|
| Report Abuse |
|
|
MrNicNac
|
  |
| Joined: 29 Aug 2008 |
| Total Posts: 26567 |
|
|
| 06 Apr 2013 03:08 PM |
The first one could be helped by using a tail call.
return myFunc() |
|
|
| Report Abuse |
|
|
crusada91
|
  |
| Joined: 04 Sep 2010 |
| Total Posts: 684 |
|
|
| 06 Apr 2013 03:09 PM |
| I don't think that would lag. |
|
|
| Report Abuse |
|
|
|
| 06 Apr 2013 03:11 PM |
@MNN;
Didn't think of that. It doesn't look very pretty/natural, but it works. Thanks. |
|
|
| Report Abuse |
|
|
|
| 06 Apr 2013 04:27 PM |
You should use the second way because if you ever want to call the function manually later in the script, you don't want it to start calling itself over and over.
If you really want to use the first way, though, then use a tail call.
_________________________________________________________________________ I possess 4 ROBUX and 73 tickets. |
|
|
| Report Abuse |
|
|
|
| 06 Apr 2013 04:30 PM |
| I have no desire to start the function's loop more than once. |
|
|
| Report Abuse |
|
|
|
| 06 Apr 2013 05:13 PM |
delay(0, function () --or spawn, or whatever while wait(60) do myFunc() end end) |
|
|
| Report Abuse |
|
|
8SunTzu8
|
  |
| Joined: 30 Sep 2011 |
| Total Posts: 8199 |
|
|
| 06 Apr 2013 05:25 PM |
Coroutines are ugly. All of these API methods to accomplish the same thing Preh did.
"Consider, friend, as you pass by, as you are now, so once was I. As I am now, you too shall be. Prepare, therefore, to follow me." -Scottish Tombstone Epitaph |
|
|
| Report Abuse |
|
|
|
| 06 Apr 2013 06:01 PM |
| IIRC, delay waits one frame before executing. Also, what pre said is effectively the same as #2. |
|
|
| Report Abuse |
|
|
|
| 06 Apr 2013 06:01 PM |
Whoops
*what pre meant is effectively the same as #2. |
|
|
| Report Abuse |
|
|
|
| 06 Apr 2013 06:02 PM |
| Yeah, if you need it to execute to-the-frame precision, go with coroutines. |
|
|
| Report Abuse |
|
|
8SunTzu8
|
  |
| Joined: 30 Sep 2011 |
| Total Posts: 8199 |
|
|
| 06 Apr 2013 08:22 PM |
lol
Coroutines just "look" ugly. I've only had to use them once before... I prefer to avoid them. Although, I seem to have issues getting parts of the API to work... Only one coroutine method worked for me. (WaitForChild() never works).
"Consider, friend, as you pass by, as you are now, so once was I. As I am now, you too shall be. Prepare, therefore, to follow me." -Scottish Tombstone Epitaph |
|
|
| Report Abuse |
|
|