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: What is spawn() and delay()?

Previous Thread :: Next Thread 
Holy_Doge is not online. Holy_Doge
Joined: 08 Feb 2012
Total Posts: 1968
07 Jul 2016 12:46 PM
I keep seeing this, but I don't understand it


Report Abuse
JarodOfOrbiter is not online. JarodOfOrbiter
Joined: 17 Feb 2011
Total Posts: 20029
07 Jul 2016 12:48 PM
spawn() runs a function asynchronously, like coroutines.

spawn(function()
for N = 1, 10 do
wait(1)
print(N)
end
end)
for N = 11, 20 do
print(N)
wait(1)
end

That results in
1
11
2
12
3
13
4
14
etc
because the loops both run at the same time.



for N = 1, 10 do
wait(1)
print(N)
end
for N = 11, 20 do
wait(1)
print(N)
end

That results in
1
2
3
...
9
10
11
12

because the loops don't run at the same time.


Report Abuse
JarodOfOrbiter is not online. JarodOfOrbiter
Joined: 17 Feb 2011
Total Posts: 20029
07 Jul 2016 12:49 PM
Oh, and delay is like spawn, except it waits for the specified amount of seconds before running the function asynchronously (At the same time as the other code)


Report Abuse
Holy_Doge is not online. Holy_Doge
Joined: 08 Feb 2012
Total Posts: 1968
07 Jul 2016 12:53 PM
Quick question:

Async is a one way communication (like a BindableEvent)

And sync is a two way communication (like a BindableFunction)

Right?

Otherwise I'm being taught the wrong stuff


Report Abuse
JarodOfOrbiter is not online. JarodOfOrbiter
Joined: 17 Feb 2011
Total Posts: 20029
07 Jul 2016 01:07 PM
From my understanding, pretty much.
But the meaning goes deeper.
Asynchronous means that they don't sync up. Neither of them wait for the other one. That's what I'm meaning here. You run it, and don't wait for a response. Move on with your code.
And so async functions work that way too. They don't return anything, because then it couldn't be async. The other place would need to wait for a response.

I don't know why DataStores and HttpService use GetAsync and stuff like that, but I think it means more of on the C++ side of things. I don't really understand why or how. But Lua ALWAYS waits for a response with these functions, if I understand correctly.


Report Abuse
KapKing47 is not online. KapKing47
Joined: 09 Sep 2012
Total Posts: 5522
07 Jul 2016 01:14 PM
^ yes, any function that has Async in it means it can delay the script, meaning it's waiting for a response.
Report Abuse
JarodOfOrbiter is not online. JarodOfOrbiter
Joined: 17 Feb 2011
Total Posts: 20029
07 Jul 2016 01:20 PM
Other way around, Kap.
Any function that has Async is not supposed to delay the script.
In Roblox, that's not the case. You go into languages like C# and the likes, and you'll find that an async function does NOT yield, even if it has something that yields inside of it.
Roblox Lua uses that for function names that aren't async though. Weird, right?


Report Abuse
KapKing47 is not online. KapKing47
Joined: 09 Sep 2012
Total Posts: 5522
07 Jul 2016 01:47 PM
Yes, that's what I meant, GetAsync yields. And yes, it does seem kinda weird how Rbx.Lua is different from some other languages in VARIES ways.
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
07 Jul 2016 01:52 PM
Async means asynchronous, "not going at the same rate and exactly together with something else, in particular." In other words, the function will not run at the same time as the script, therefore the script has to wait for it to finish. Or at least that's my understanding of it.


As for spawn and delay, they just queue up tasks in the task scheduler, sort of like creating a new script. The only difference between spawn and delay is delay waits a specified time before respawning the task.

It's usually better to use coroutines however, because of the power and flexibility they provide.
Report Abuse
cntkillme1 is not online. cntkillme1
Joined: 16 Feb 2012
Total Posts: 592
07 Jul 2016 01:52 PM
I think when they use the term Async in that case, they don't mean it in terms of the thread but actually the service. As in you can send multiple GetAsync requests at the same time and they're not queued (hence each request doesn't know or care about the existence of the other, they do their own thing).
Report Abuse
cntkillme1 is not online. cntkillme1
Joined: 16 Feb 2012
Total Posts: 592
07 Jul 2016 01:55 PM
war you got your terms mixed up, what you described about having a script wait for the return would make it synchronous.
"Asynchrony, in computer programming, refers to the occurrence of events independently of the main program flow and ways to deal with such events."
_independently_
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
07 Jul 2016 01:58 PM
Oh. Weird why doesn't it refer to the actual real-world definitions lol.

Maybe they refer to the real definitions in their functions, kinda like how Lua indices starts at 1 like a real person would count?
Report Abuse
cntkillme1 is not online. cntkillme1
Joined: 16 Feb 2012
Total Posts: 592
07 Jul 2016 02:00 PM
It does, it's just geared more towards how it's used in CS.

"not going at the same rate and exactly together with something else, in particular"
That's virtually the same thing as
"refers to the occurrence of events independently"
Report Abuse
warspyking is not online. warspyking
Joined: 15 Nov 2011
Total Posts: 13947
07 Jul 2016 02:01 PM
Well I guess. I always interpreted it more like the other thread had to finish executing because it couldn't run while the caller thread was running.

Okay then I learned something new today :D
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