generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: A very interesting problem regarding coroutines

Previous Thread :: Next Thread 
Mangeia is not online. Mangeia
Joined: 13 Feb 2016
Total Posts: 3
08 Mar 2016 06:23 PM
I've explained on skype the entirety of the problem and I thought you guys would be interested in it. I already "fixed" it somehow (I resorted to writing my own sleep function rather than using wait.), but it raises an interesting point regarding roblox's task scheduler and coroutines. I copy pasted my chats into this thread because I was too lazy to rewrite it properly.

This is the script in question:

-------------------------------------------------------------
-------------------------------------------------------------

local E = 0

function Coroutine(t)
return coroutine.create(function()
if t then
wait()
end
E = E + 1
end)
end

local a,b = Coroutine(false), Coroutine(true)

do
coroutine.resume(a)
print(E) --> 1
end

do
coroutine.resume(b)
print(E) --> 1, but you would expect 2
end

-------------------------------------------------------------
-------------------------------------------------------------

7:12:45 PM] louka: (deleted link)
[7:12:59 PM] louka: this is not to explain my issue but to explain what i realized
[7:13:19 PM] louka: wait schedules the current coroutine for execution after a certain amount of time has passed which is what should be expected
[7:13:31 PM] louka: wait will basically yield the current coroutine then restart it later
[7:13:51 PM] louka: now the interesting thing is that since wait yields the coroutine
[7:14:03 PM] louka: "normal" code execution gets restarted
[7:14:15 PM] louka: but if you look at my example
[7:14:20 PM] louka: if your game depends on a variable
[7:14:25 PM] louka: and you must add some delay in the coroutine
[7:14:28 PM] louka: and you use wait
[7:14:45 PM] louka: then 2 bad for you because the coroutine will be yielded before the local E is incremented
[7:15:15 PM] louka: so basically what roblox should do is do some environment magic and only allow wait() to be performed in threads started with spawn/delay
[7:15:22 PM] louka: but not coroutines
[7:15:33 PM] louka: because if you look at my script it becomes problematic if your game/script depends on variables
[7:15:42 PM] louka: and you must add a delay inside of the coroutine
[7:15:45 PM] louka: and you use wait()
[7:16:01 PM] louka: because the main coroutine will be yielded before E is incremented
[7:16:16 PM] louka: which makes the main code continue
[7:16:49 PM] louka: and since the main code doesnt have any wait then that code will eventually terminate/end before the "increment E" coroutine is resumed





Report Abuse
Flux_Capacitor is not online. Flux_Capacitor
Joined: 07 Apr 2008
Total Posts: 45720
08 Mar 2016 06:38 PM
lmao misinformation is great
Report Abuse
chimmihc is not online. chimmihc
Joined: 01 Sep 2014
Total Posts: 17143
08 Mar 2016 06:38 PM
Only one "thread" is running at a time, when you use the wait function it yields the current one and tells the thread scheduler to resume another one.




Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image