|
coroutine.resume(coroutine.create(function()
CODE
end))
delay(0,function()
CODE
end)
Are those two basically the same thing? |
|
cntkillmeJoin Date: 2008-04-07 Post Count: 49450 |
Basically, but I believe delay actually runs the function in an actual separate while coroutine simulates that. |
|
|
However, if all you're really trying to do is create a separate thread, I recommend Spawn |
|
|
Thanks! Which of the two do you prefer? |
|
|
Also, I just need to run some while loops without interrupting anything. |
|
|
cntkillmeJoin Date: 2008-04-07 Post Count: 49450 |
I usually prefer coroutine since spawn and delay don't actually exist in Lua, but it was added by Roblox. So (depending on what I'm doing) if I ever want to work with something I made in RBX.Lua in Lua, I can be sure that coroutine will exist and is an actual data type as opposed to whatever roblox does. |
|
cntkillmeJoin Date: 2008-04-07 Post Count: 49450 |
Why not run the code in the while loops together? |
|
Bobobob12Join Date: 2008-01-23 Post Count: 5350 |
"However, if all you're really trying to do is create a separate thread, I recommend Spawn"
i don't, since spawn passes arguments to the inner function; i say use coroutine.wrap(function())() for threads you don't need to stop manually |
|
|
@cntkillme
I didn't know Spawn and Delay were Roblox-specific, haha. I only script inside of Roblox so far, so I'm not going to worry about that yet. Also, what do you mean run the code with the while loops?
|
|
cntkillmeJoin Date: 2008-04-07 Post Count: 49450 |
Well it depends what you are doing, see if you can combine the two into 1 loop. It would be more efficient that way (if you can, of course) |
|
|
func = function(args)
local nW = coroutine.wrap(function(args)
local Del = math.random(3)
print("Waiting",Del,"Seconds to print",args)
wait(Del)
print(args)
end)
nW()
end
for I=1,5 do
func(math.random(10))
end
--XD?
--ik cnt will show me up tho.. |
|
|
I don't think I can do that. The main thread is based off of a while loop itself, and the other mini while loops don't run the entire time. Like this:
local game = false
local function dogame()
delay(0,function()
while (game) do
GAME
wait()
end
end)
end
local function game()
game = true
dogame()
end
local function endgame()
game = false
end
while true do
game()
endgame()
wait(5)
end
That wasn't as quick an example as I was hoping |
|