#Delay Discussion

1 messages ยท Page 1 of 1 (latest)

vale pagoda
#

Lets continue here

#

So we don't flood the chat

#

@near palm ๐Ÿ‘€

#

Ima gather all the examples kek

proper flower
#

grabs popcorn

sour current
#

I really don't care about proving if async-await is better

near palm
deft bough
#

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.

near palm
#

Sometimes Invoke isn't enough

deft bough
#

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.

vale pagoda
#

@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

vale pagoda
tranquil depot
#

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.

deft bough
#
void Start()
{
    MyMethod();
}

async void MyMethod()
{
   await Task.Delay(time);
   DoSomething ();
}
vale pagoda
deft bough
#

Damn, it's hell to code on the phone.๐Ÿ˜ฌ

vale pagoda
near palm
#

That's pretty much as complicated as I though it would be. (Not complicated at all)

#

Referring to the async, not typing on phone ๐Ÿ˜…

vale pagoda
#

(on phone)

vale pagoda
tranquil depot
#

Maybe the time is public so you can change it

near palm
tranquil depot
#

Or has a property

vale pagoda
#

Or does it capture it when it's like at 2f and when you change it to 3f (the variable) it'd not update

near palm
#

it "captures" it the moment you say = new WaitForSeconds(x)

tranquil depot
vale pagoda
#
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

near palm
tranquil depot
near palm
#

Changing wait afterwards will not change the shortWait's seconds

vale pagoda
#

Sooo it's not possible

tranquil depot
#

And even then yeah, it wouldn't change it.

vale pagoda
#

So invoke would win on this front then right

#

(Unless you don't cache the WaitFor ofc)

deft bough
#

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.

vale pagoda
#

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)

deft bough
#

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!

vale pagoda
#

Heh

tranquil depot
vale pagoda
# vale pagoda Heh

It's still funny how all of them fixated on that instead of answering the question kek

vale pagoda
#

Pretty sure I use it

vale pagoda
sour current
vale pagoda
#

au

#

mijn gevoelens

tranquil depot
#

More dutchies here?

#

Interesting

vale pagoda
#

zeer interessant

near palm
#

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

deft bough
near palm
sour current
#

Cool now burn this thread and stop pinging me

vale pagoda
#

Man's never explain await propely :(

vale pagoda
#

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(){}

near palm
#

People reading articles from 2008 (when Unity was way different) and basing their opinions on those

#

Like in this case, I think GetComponent used to be way less performant

vale pagoda
#

They actually recently improved the performance of GetComponent<>() somewhat recently I think

#

I read it somewhere

#

Not sure where though, but I think I'm correct on it

#

Regardless, it's not a big deal to use it, unless you throw it in an Update ofc