#Delay Discussion
1 messages ยท Page 1 of 1 (latest)
Lets continue here
So we don't flood the chat
@near palm ๐
Ima gather all the examples kek
grabs popcorn
I really don't care about proving if async-await is better
Well you keep responding and invalidating my points so you seem to care ๐
If you want to have several await in a sequence, waiting for other task and other complicated stuff like that within one function, it's gonna be difficult to implement with just Invoke.
Yeah, obviously.
The context here is just delaying a single method/action
Sometimes Invoke isn't enough
In this case it might be fine to use invoke, but you probably wouldn't have just one await if you were using async anyway.
Also, you might want to adhere to the same style of code. And mixing invoke/async/coroutines can arguably break that.
@sour current so let's say you want to call MyMethod from start, with a 2 second delay. Here's a template:
void Start()
{
}
void MyMethod()
{
}
With invoke:
void Start()
{
Invoke(nameof(MyMethod), 2f);
}
void MyMethod()
{
}
With coroutine:
void Start()
{
StartCoroutine(MyMethodCoroutine(2f));
}
IEnumerator MyMethodCoroutine(float delay)
{
yield return new WaitForSeconds(delay);
MyMethod();
}
void MyMethod()
{
}
From Osmal ^
Tried to get @sour current example, but he removed it
Why? idk
Yes, if you want parameters, Invoke won't exactly be your go-to function
WaitForSeconds shortWait = new WaitForSeconds(2f);
Cache the WaitForSeconds so you don't have to do the new constantly, that will get garbage collected again afterwards. That would be a fair comparison.
Did not know you could do that
void Start()
{
MyMethod();
}
async void MyMethod()
{
await Task.Delay(time);
DoSomething ();
}
Would it be possible to cache it but also keep the parameter interchangeable?
Thank you
Damn, it's hell to code on the phone.๐ฌ
Wow I'm impressed you pulled it off
That's pretty much as complicated as I though it would be. (Not complicated at all)
Referring to the async, not typing on phone ๐
AFAIK you can not.
I've a shortcute mono that if I type it, it'll give be the three backticks
(on phone)
What if you make the delay a variable?
Maybe the time is public so you can change it
Still don't think you can
Or has a property
Or does it capture it when it's like at 2f and when you change it to 3f (the variable) it'd not update
it "captures" it the moment you say = new WaitForSeconds(x)
https://github.com/Unity-Technologies/UnityCsReference/blob/master/Runtime/Export/Scripting/WaitForSeconds.cs
Doesn't look like you can change it.
private int wait = 2;
WaitForSeconds shortWait;
void Start()
{
shortWait= new WaitForSeconds(wait);
wait = 3;
StartCoroutine(MyMethodCoroutine());
}
IEnumerator MyMethodCoroutine()
{
yield return shortWait;
MyMethod();
}
void MyMethod()
{
}
Would this wait 3 seconds or 2 seconds
That's what I mean
You already passed 2f to the shortWait so it will stay tthat way
int is a value type so 2f
Changing wait afterwards will not change the shortWait's seconds
Sooo it's not possible
And even then yeah, it wouldn't change it.
So invoke would win on this front then right
(Unless you don't cache the WaitFor ofc)
I think where async really shines is when you have a complicated chain of calls and tasks. Want to await some of them, keep it in one place and not freeze the main thread while doing it. Also have the option to cancel all the tasks or some of the at once.
Probably, yes
That's not how @sour current sees it tho :p
(Also no I won't stop pinging him into this conversation, I'm evil >;P)
I mean, it's a matter of preference. Some people prefer to put braces on the same line even if you don't need to do it!
Heh
If you want to change all the times you want to wait for something yeah. I never had that to be honest.
It's still funny how all of them fixated on that instead of answering the question kek
There are definitely use cases though
Pretty sure I use it
Yeah I do actually
Net zoals alle Nederlanders
zeer interessant
Anyway, we all use whatever tool we want and see fit. I'm just trying to make people understand that Invoke is not the "absolute trash" that some people make it seem to be ๐
Aaaas long as you use nameof
I guess it's part of the "unity culture". Kinda like the bias that you can't make a good game with unity.
Yeah 100%. I'm kind of loud against that kind of thinking
Cool now burn this thread and stop pinging me
Man's never explain await propely :(
(For documenting purposes :P https://strawpoll.com/polls/XmZRjOPo6Zd)
Honestly it's kind of like GetComponent<>() in a way
Some people are like completely allergic to it
And that it'll just destroy your game if you do it like once in a Start(){}
My teachers actually said at first you should never use it kek
But that might also have been to prevent the newbies from throwing GetComponents<>() in the Update(){}