Fedorakid
|
  |
| Joined: 17 Jul 2010 |
| Total Posts: 7079 |
|
|
| 10 Jan 2013 03:31 PM |
| Im gona learn coroutine and if u come here to post "It makes it possible to run 2 functions at once" explain better, show me the most simplest example explain how it works, and dont give me a wiki, and no complains about not using wiki. |
|
|
| Report Abuse |
|
|
|
| 10 Jan 2013 03:33 PM |
| If you go to a sb with me, explanation of them would be easier? |
|
|
| Report Abuse |
|
|
Fedorakid
|
  |
| Joined: 17 Jul 2010 |
| Total Posts: 7079 |
|
|
| 10 Jan 2013 03:38 PM |
| Srry gotta go to bed soon and trying to make it much time as possible, tommorow if ur on? |
|
|
| Report Abuse |
|
|
| |
|
|
| 10 Jan 2013 04:29 PM |
It allows you to run two thingys at once like this:
For example, if this is at the start of your script, it will not work, because it is constantly repeating this loop. while wait() do end
However, if you were to put this in a courotine, it would, as the loop would run on a different 'thread'. |
|
|
| Report Abuse |
|
|
Fedorakid
|
  |
| Joined: 17 Jul 2010 |
| Total Posts: 7079 |
|
|
| 12 Jan 2013 11:22 AM |
| Can someone show me an example using coroutines?and what it does and how it works? |
|
|
| Report Abuse |
|
|
|
| 12 Jan 2013 11:24 AM |
while true do print("2") wait(1) end
coroutine.resume(coroutine.create(function() wait(.5) while true do print("1') end))
That would print:
2, 1, 2, 1, 2, 1, 2, 1.
Because, the loop runs normally, and the loop in the coroutine runs by itself as well. There is not much to learn, really, just remember the syntax of it. |
|
|
| Report Abuse |
|
|
|
| 12 Jan 2013 11:25 AM |
Sorry forgot an end:
while true do print("2") wait(1) end
coroutine.resume(coroutine.create(function() wait(.5) while true do print("1') end end))
And it would print the 2 and 1 on seperate lines, not all on the same line. I just wrote it that way because it is faster than making them all on separate lines. |
|
|
| Report Abuse |
|
|
Fedorakid
|
  |
| Joined: 17 Jul 2010 |
| Total Posts: 7079 |
|
|
| 12 Jan 2013 11:30 AM |
| But , to make 2 functions run at once, cant we just put 2 of them after each other, or wont it work? |
|
|
| Report Abuse |
|
|
|
| 12 Jan 2013 11:42 AM |
There is usually never a reason to ever use a coroutine since we can just add multiple scripts. Even then, multiple scripts typically aren't needed; you can usually write the core mechanics of your game in a single script.
You can use coroutines to get runtime errors before any yields, but ypcall is both easier and better to use over coroutines.
All in all, I don't see anything tragically useful with coroutines on Roblox. However, in something like Love2D, where there is only a single "thread" running, coroutines may be useful. |
|
|
| Report Abuse |
|
|
|
| 12 Jan 2013 11:58 AM |
| When animating, I usually use while loops, so I use coroutines to hold the while loops, so that the script dose not end right there. |
|
|
| Report Abuse |
|
|
|
| 12 Jan 2013 12:00 PM |
| Using a single while loop would work just fine for that. Take a look at the Roblox Animate script in your chareacter. |
|
|
| Report Abuse |
|
|
|
| 12 Jan 2013 12:01 PM |
I am not sure why it does this then? :o
_G["AttackSelect"] = (function() repeat wait() until joints[2].CurrentAngle == -4 TorsoHandle.Part1 = limbs[2] TorsoHandle.C0 = CFrame.Angles(-1.5,-0.7,2.5) TorsoHandle.C1 = CFrame.new(1.5,0,-0.9) nub = coroutine.create(function() herp = 0 while herp ~= 15 do wait(0.03) joints[2].C0 = joints[2].C0*CFrame.Angles(0.05,0,0) herp = herp + 1 end end) repeat wait() until TorsoHandle.C0 == CFrame.Angles(-1.5,-0.7,2.5) and TorsoHandle.C1 == CFrame.new(1.5,0,-0.9) coroutine.resume(nub) repeat wait(0.03) until herp == 15 nublet = coroutine.create(function() der = 0 while der ~= 4 do wait(0.03) joints[2].C0 = joints[2].C0*CFrame.Angles(0,0,0.6) der = der + 1 end end) coroutine.resume(nublet) end)
This is how I have been having to do animation lately. |
|
|
| Report Abuse |
|
|
|
| 12 Jan 2013 12:07 PM |
This is my Animate script for my Archmage:
local Figure = script.Parent local Torso = Figure.Torso local Head = Figure.Head local Joint = Figure.Part
Torso.Gyro.cframe = Torso.CFrame
local RHZ = Joint:Clone() RHZ.Parent = Figure RHZ.Name = "Right Hip Joint Z" RHZ.Transparency = 1 local rHipZ = Instance.new("Motor", Torso) rHipZ.Part0 = Torso rHipZ.Part1 = RHZ rHipZ.C0 = CFrame.new(0.5,-1,0) rHipZ.C1 = CFrame.new(0,0.5,0) rHipZ.Name = "Right Hip Z" rHipZ.MaxVelocity = 0.15 local rHip = Instance.new("Motor", RHZ) rHip.Part0 = RHZ rHip.Part1 = Figure["Right Leg"] rHip.C0 = CFrame.new(0,0.5,0)*CFrame.Angles(0,math.pi/2,0) rHip.C1 = CFrame.new(0,1,0)*CFrame.Angles(0,math.pi/2,0) rHip.Name = "Right Hip" rHip.MaxVelocity = 0.15
local LHZ = Joint:Clone() LHZ.Parent = Figure LHZ.Name = "Left Hip Joint Z" LHZ.Transparency = 1 local lHipZ = Instance.new("Motor", Torso) lHipZ.Part0 = Torso lHipZ.Part1 = LHZ lHipZ.C0 = CFrame.new(-0.5,-1,0) lHipZ.C1 = CFrame.new(0,0.5,0) lHipZ.Name = "Left Hip Z" lHipZ.MaxVelocity = 0.15 local lHip = Instance.new("Motor", LHZ) lHip.Part0 = lHip.Parent lHip.Part1 = Figure["Left Leg"] lHip.C0 = CFrame.new(0,0.5,0)*CFrame.Angles(0,math.pi/2,0) lHip.C1 = CFrame.new(0,1,0)*CFrame.Angles(0,math.pi/2,0) lHip.Name = "Left Hip" lHip.MaxVelocity = 0.15
local RSZ = Joint:Clone() RSZ.Parent = Figure RSZ.Name = "Right Shoulder Joint Z" RSZ.Transparency = 1 local rShoulderZ = Instance.new("Motor", Torso) rShoulderZ.Part0 = Torso rShoulderZ.Part1 = RSZ rShoulderZ.C0 = CFrame.new(0.75,0.75,0) rShoulderZ.C1 = CFrame.new(-0.75,0.25,0) rShoulderZ.Name = "Right Shoulder Z" rShoulderZ.MaxVelocity = 0.15 local rShoulder = Instance.new("Motor", RSZ) rShoulder.Part0 = RSZ rShoulder.Part1 = Figure["Right Arm"] rShoulder.C0 = CFrame.Angles(0,math.pi/2,0) rShoulder.C1 = CFrame.new(0,0.5,0)*CFrame.Angles(0,math.pi/2,0) rShoulder.Name = "Right Shoulder" rShoulder.MaxVelocity = 0.15
local LSZ = Joint:Clone() LSZ.Parent = Figure LSZ.Name = "Left Shoulder Joint Z" LSZ.Transparency = 1 local lShoulderZ = Instance.new("Motor", Torso) lShoulderZ.Part0 = Torso lShoulderZ.Part1 = LSZ lShoulderZ.C0 = CFrame.new(-0.75,0.75,0) lShoulderZ.C1 = CFrame.new(0.75,0.25,0) lShoulderZ.Name = "Left Shoulder Z" lShoulderZ.MaxVelocity = 0.15 local lShoulder = Instance.new("Motor", LSZ) lShoulder.Part0 = LSZ lShoulder.Part1 = Figure["Left Arm"] lShoulder.C0 = CFrame.Angles(0,math.pi/2,0) lShoulder.C1 = CFrame.new(0,0.5,0)*CFrame.Angles(0,math.pi/2,0) lShoulder.Name = "Left Shoulder" lShoulder.MaxVelocity = 0.15
local MaskWeld = Instance.new("Motor", Head) MaskWeld.Part0 = Head MaskWeld.Part1 = Figure.Mask MaskWeld.C0 = CFrame.new(0,1,-0.6) MaskWeld.Name = "Mask Weld"
local HelmWeld = Instance.new("Motor", Head) HelmWeld.Part0 = Head HelmWeld.Part1 = Figure.Helmet HelmWeld.C0 = CFrame.new(0,0.7,0) HelmWeld.Name = "Helmet Weld"
local NeckWeld = Instance.new("Motor", Head) NeckWeld.Part0 = Torso NeckWeld.Part1 = Head NeckWeld.C0 = CFrame.new(0,1.5,0)*CFrame.Angles(math.pi/2,0,0) NeckWeld.C1 = CFrame.Angles(math.pi/2,0,0) NeckWeld.Name = "Neck" NeckWeld.MaxVelocity = 0.15
--print "Weld complete"
--[[ while wait(0.5) do lShoulder.DesiredAngle = math.random()*math.pi - math.pi/2 rShoulder.DesiredAngle = math.random()*math.pi - math.pi/2 lHip.DesiredAngle = math.random()*math.pi - math.pi/2 rHip.DesiredAngle = math.random()*math.pi - math.pi/2 lShoulderZ.DesiredAngle = -math.random() rShoulderZ.DesiredAngle = math.random() lHipZ.DesiredAngle = -math.random() rHipZ.DesiredAngle = math.random() NeckWeld.DesiredAngle = math.random()*math.random(-1,1)*math.pi/2 end ]] local Fireball = Figure.Ball local FlyVal = Figure.Flying local Target = Figure.Target local AttackVal = Figure.AttackMode local AttackRec = Figure.AttackReceeding
function Animate(time) local Attack = AttackVal.Value local weld = Figure.ToolWeld.Value if Target.Value and Target.Value:FindFirstChild("Torso") then if FlyVal.Value then Torso.Gyro.cframe = CFrame.new(Torso.Position, Target.Value.Torso.Position) end end if Attack == "None" then if FlyVal.Value then rShoulderZ.MaxVelocity = 0.02 lShoulderZ.MaxVelocity = 0.02 rShoulderZ.DesiredAngle = (math.sin(time+math.pi)+1)/2 lShoulderZ.DesiredAngle = (-math.sin(time+math.pi)-1)/2 rShoulder.DesiredAngle = 0.5 lShoulder.DesiredAngle = 0.5 else rShoulder.DesiredAngle = 0 lShoulder.DesiredAngle = 0 lShoulderZ.DesiredAngle = 0 end if weld then weld.DesiredAngle = 0 end elseif Attack == "Fire Storm" then if weld and weld.Part0 == Figure["Right Arm"] then if AttackRec.Value then rShoulder.DesiredAngle = math.pi/2 weld.DesiredAngle = -math.pi/2 lShoulderZ.DesiredAngle = -0.25 rShoulderZ.DesiredAngle = 0 lShoulder.DesiredAngle = 0 else rShoulder.DesiredAngle = 3*math.pi/4 weld.DesiredAngle = -math.pi/4 lShoulderZ.DesiredAngle = -0.25 rShoulderZ.DesiredAngle = 0 lShoulder.DesiredAngle = 0 end else if AttackRec.Value then rShoulder.DesiredAngle = math.pi/4 lShoulder.DesiredAngle = math.pi/4 lShoulderZ.DesiredAngle = 0 rShoulderZ.DesiredAngle = 0 else rShoulder.DesiredAngle = 5*math.pi/6 lShoulder.DesiredAngle = 5*math.pi/6 lShoulderZ.DesiredAngle = 0 rShoulderZ.DesiredAngle = 0 end end elseif Attack == "Fire Blast" then elseif Attack == "Ice Spike" then if AttackRec.Value then rShoulder.DesiredAngle = math.pi/4 lShoulder.DesiredAngle = math.pi/4 lShoulderZ.DesiredAngle = 0 rShoulderZ.DesiredAngle = 0 else rShoulder.DesiredAngle = 5*math.pi/6 lShoulder.DesiredAngle = 5*math.pi/6 lShoulderZ.DesiredAngle = 0 rShoulderZ.DesiredAngle = 0 end elseif Attack == "Ice Blast" then elseif Attack == "Earth Spike" then end end
local runService = game:GetService("RunService") runService.Stepped:connect(function(time, step) Animate(time) end)
As you can see, no coroutines. I don't even use a while loop; I use RunService.Stepped. |
|
|
| Report Abuse |
|
|
|
| 12 Jan 2013 12:09 PM |
:o
Hmm, you win this one, but ill get you next time, and your little fox to! |
|
|
| Report Abuse |
|
|
1smurf1
|
  |
| Joined: 07 Aug 2010 |
| Total Posts: 50 |
|
|
| 12 Jan 2013 01:24 PM |
@AFF
Well it all depends on the situation? I don't think you'd need coroutines much for animations but they're invaluable in my game Eg I wanted to create a custom event and since I'm binding multiple listeners I need a coroutine so all the listeners fire at once or I had to make multiple groups of parts move at once for a special effects All possible through coroutines ^^ |
|
|
| Report Abuse |
|
|
|
| 12 Jan 2013 01:54 PM |
I agree with AFF, pretty much the only time I use a coroutine is when deleting an object if I want some kind of delay
Delay(.5,function()whatever:Destroy()end)
rather than game:GetService("Debris"):AddItem(whatever,5)
'cause AddItem is the equivalent to removed, which is depreciated :( |
|
|
| Report Abuse |
|
|