|
| 31 Jan 2015 10:11 PM |
| I've heard of them a lot lately, what does spawn(function() end) and delay() do? |
|
|
| Report Abuse |
|
|
|
| 31 Jan 2015 10:13 PM |
| spawn creates a new function when the script isn't busy, and delay delays the given amount of time before executing the given function |
|
|
| Report Abuse |
|
|
128GB
|
  |
| Joined: 17 Apr 2014 |
| Total Posts: 8056 |
|
|
| 31 Jan 2015 10:16 PM |
spawn(function() wait(5) print("Hello") end)
print("Bye")
Output Bye 5 seconds later Hello
delay(5, function() print("Hello") end)
print("Bye")
Output Bye 5 seconds later Hello |
|
|
| Report Abuse |
|
|
|
| 01 Feb 2015 12:26 AM |
| Ok thanks! I understand delay but I still don't understand spawn :/ |
|
|
| Report Abuse |
|
|
Seranok
|
  |
| Joined: 12 Dec 2009 |
| Total Posts: 11083 |
|
|
| 01 Feb 2015 12:29 AM |
spawn(function_goes_here) is like you created a new script to run the given function
So let's say you do this:
function print123()
for i = 1, 3 do print(i) wait(1) end
end
spawn(print123) print123()
Then the results will be:
1 1 2 2 3 3
This is because the spawned thread and the script's main thread are being executed at the same time. |
|
|
| Report Abuse |
|
|
|
| 01 Feb 2015 12:31 AM |
Ah ok, now I think I understand better, so it's like delay() is the opposite of spawn(). Thanks. |
|
|
| Report Abuse |
|
|
|
| 01 Feb 2015 12:31 AM |
| they exist so you never have to use coroutines ever again |
|
|
| Report Abuse |
|
|
Seranok
|
  |
| Joined: 12 Dec 2009 |
| Total Posts: 11083 |
|
|
| 01 Feb 2015 12:32 AM |
Well delay is basically this:
function delay(t, f) wait(t) f() end |
|
|
| Report Abuse |
|
|
| |
|
| |
|
Seranok
|
  |
| Joined: 12 Dec 2009 |
| Total Posts: 11083 |
|
|
| 01 Feb 2015 12:33 AM |
Sorry I meant to post:
function delay(t, f) spawn(function() wait(t) f() end) end |
|
|
| Report Abuse |
|
|
|
| 01 Feb 2015 12:57 AM |
| Oh ok, thanks for the help! |
|
|
| Report Abuse |
|
|