SolotaireJoin Date: 2009-07-30 Post Count: 30356 |
Which one do you guys find yourself using most often? What are the core differences between them, or are they all pretty similar--do any of them represent a separate thread better than the other? Are the run-times comparable?
Making this thread in part because I'd like to get your opinions and in part because there seems to be even less scripting discussion than usual. |
|
Waffle3zJoin Date: 2011-04-15 Post Count: 266 |
Delay has a minimum 1-frame wait-time
Spawn is easier to type
coroutines have useful methods. |
|
dekkonotJoin Date: 2010-12-22 Post Count: 6685 |
I honestly have no idea. I never use any of them, as I'm never in any large projects.
~Not a Moderator, The Makeshift Moderator Team, Dekkonot |
|
zinsJoin Date: 2011-01-14 Post Count: 3394 |
[ Content Deleted ] |
|
SolotaireJoin Date: 2009-07-30 Post Count: 30356 |
"I honestly have no idea. I never use any of them, as I'm never in any large projects."
I find myself using them when trying to reduce the total number of scripts in my game, but mostly have used coroutines. |
|
coploxJoin Date: 2008-06-07 Post Count: 3252 |
I tend to use coroutines. |
|
|
i used a coroutine for having multiple loops instead of multiple scripts
is that the correct way? |
|
iStone4SJoin Date: 2012-05-07 Post Count: 416 |
What does this do, please? |
|
|
Coroutines don't create real threads, while delay and spawn do.
Delay should only be used when you actually don't want to run it immediately. Otherwise, use spawn. |
|
SolotaireJoin Date: 2009-07-30 Post Count: 30356 |
"i used a coroutine for having multiple loops instead of multiple scripts
is that the correct way?"
If "multiple loops" means at the same time, then yes. |
|
|
@Solotaire
Use delay when you want to run it in a certain time. Use Spawn when you want to run something in another, real thread. Use coroutines when you just want multithreading. |
|
SharksieJoin Date: 2009-10-10 Post Count: 30576 |
Doesn't spawn wait a frame before running its code? |
|
SolotaireJoin Date: 2009-07-30 Post Count: 30356 |
@iStone4S,
Here's an example of coroutines to help you understand
function printNumbers()
for i = 1,3 do
print(i)
end
end
routine = coroutine.wrap(printNumbers)
routine()
printNumbers()
that should give you something that looks about like this:
>1
>1
>2
>2
>3
>3
If you didn't use a coroutine and just had the following:
printNumbers()
printNumbers()
Your output would be:
>1
>2
>3
>4
>5
>6 |
|
SnoxicleJoin Date: 2012-11-17 Post Count: 187 |
I use spawn except for some cases where coroutines are needed. |
|
HEAT507Join Date: 2012-08-31 Post Count: 429 |
text=[[
print(1/1);
]]
pcall(function()
coroutine.resume(coroutine.create(loadstring(text));
end); |
|
08CJoin Date: 2013-01-26 Post Count: 847 |
[ Content Deleted ] |
|
OysiJoin Date: 2009-07-06 Post Count: 9058 |
[ Content Deleted ] |
|
nate890Join Date: 2008-11-22 Post Count: 21686 |
Coroutines have their own uses. It's best to use Spawn if you want to sneak a thread in there. Delay should only be used if you are trying to delay the thread from starting, otherwise it's best to use the Spawn function. |
|
zars15Join Date: 2008-11-10 Post Count: 9999 |
"because there seems to be even less scripting discussion than usual."
No, usually there are 0-3 discussions per day. Yesterday everything suddenly changed, and scripters is filled with discussions.
And back on topic...
I use new threads just hacky stuff, so I could live without them, since they aren't useful for me at all. If you do everything clean and efficient, you shouldn't need new threads.
|
|
|
|
I use delay. I could probably use spawn in most cases though. |
|
|
Multithreading with .create vs new thread entirely - same difference?
I use coroutine.resume(coroutine.create(function ...()) end)) because it is cool looking. |
|
OysiJoin Date: 2009-07-06 Post Count: 9058 |
[ Content Deleted ] |
|
abaw7Join Date: 2009-10-23 Post Count: 745 |
I used to use coroutine.wrap(function() end)() because I understood Coroutines, and couldn't figure our how the heck Spawn worked. Then, I found out how Spawn worked, went Yay!, and that's my main choice now. |
|
|
I generally use spawn as its simple and does exactly what i need it to do. For example, having and entire model fade:
function fadeModel(model)
for i,v in pairs(model:GetChildren()) do
if v:IsA("BasePart") then
Spawn(function()
for i = v.Transparency*10,1,0.1 do
wait()
v.Transpareny= i/10
end
end)
end
end
end
~ I choice end)() coroutine.wrap(function() I and used Spawn Yay!, Spawn |
|