2

Is there any way to run a function and get the return value as a parameter for a function? Let me explain: I have a ton of functions lying around in my code which are just there for waiting a certain amount of seconds before something takes place. Since I only use them in one circumstance, I wanted to put them inside of my method that uses them somehow. (I'm using Unity3D.) Example:

public void SayHiAfterSeconds(float seconds) {
    StartCoroutine(sayHiAfterSeconds(seconds));
}
IEnumerator sayHiCoroutine(float seconds) {
    yield return new WaitForSeconds(seconds);
    print("Hi.");
}

I want to put the sayHiCoroutine function inside of SayHiAfterSeconds. Something like:

public void SayHiAfterSeconds(float seconds) {
    StartCoroutine(IEnumerator() {
        yield return new WaitForSeconds(seconds);
        print("Hi.");
    });
}

Is that possible? Thanks.

283
  • 141
  • 1
  • 9
  • @Valentin just for the record, that is not a duplicate. This is about **coroutines** – Fattie Jun 12 '16 at 14:42
  • Hi @283. You're quite right, you've stumbled on to a funny thing about Unity. You will often see functions, which do, nothing other than start a coroutine. **It is very common on many teams that you do this:** the main call is called **SayHiAfter**. the matching coroutine **is the same with an underscore and a small letter, so, **"_sayHiAfter"**. I would almost say to you you "should" do that, **it's the usual idiom amongst Unity engineers**. – Fattie Jun 12 '16 at 14:44
  • Note very clearly that: **NORMALLY, you never, ever, ever, ever, EVER use underscores to begin names - it is a really incredibly out of date, stupid, bad idea from when things were different in the past.** So NORMALLY never ever use the "underscore begins name" idea in Unity - never ever. This however is a very exceptional situation. All I can say is I feel that it is appropriate in this case (it indicates "local" "incidental" "associated" really well) and that it is very common. So, I suggest you do that. – Fattie Jun 12 '16 at 14:48
  • Note that, simply, whatever team you're working on will have a style rule about that. (So, "always name matching coroutines like this .... blah") If you work for me or any of the studios I set up it will be like that, if you go work at rovio they will have their own thing in the Unity team, or whatever. If it's you and some college friends you'll decide on something. That's the deal on that. (BTW - incidentally you'll almost certainly mark it "private", don't forget.) – Fattie Jun 12 '16 at 14:49
  • BTW this is one of the very best Unity questions on SO. It is actually about engineering and something that matters. 99.9% of the unity questions on here are complete trash. It's ridiculous you got two close votes, but not untypical of SO unfortunately. – Fattie Jun 12 '16 at 14:51

1 Answers1

1

Short Answer: NO.

You can't put couroutine function in another function but you can call it from another function. This has been asked before.

Community
  • 1
  • 1
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Alright, thanks. Are there any good ways to name your coroutine functions? – 283 Jun 12 '16 at 13:59
  • @283 For me I don't like calling it directly. I call it from another function. For example, your `sayHiCoroutine` coroutine function should be named `SayHiAfterSecondsCRT` then the function you call it from should be `SayHiAfterSeconds`. That way `CRT` is used to determine that it is a coroutine and `SayHiAfterSeconds` without the `CRT` will let me know that this is the function to call not the one that ends with `CRT`. The names should always be the-same except for the CRT at the end of the coroutine function. That way there is less confusion on which function is calling which function. – Programmer Jun 12 '16 at 14:09
  • Thanks. I felt that my code was getting very messy since I had no idea how to name my coroutine functions! – 283 Jun 12 '16 at 14:15
  • @283 your code looks fine except for sayHiCoroutine which is longer. Naming it somethingCoroutine is unnecessary. – Programmer Jun 12 '16 at 14:32
  • "I felt that my code was getting very messy since I had no idea how to name my coroutine functions". You now know. In the Unity scene a common idiom for naming "associated" coroutines, is, "underscore then change the first letter to lower case". – Fattie Jun 12 '16 at 14:53
  • @JoeBlow I used to use underscore for coroutines too but then then it began to look weird to me so I switched to adding CRT at the end. I guess it's a matter of what suits you. – Programmer Jun 12 '16 at 19:43
  • yes I think adding CRT at the end is also a common thing. i think the underscore convention (which is an old convention meaning "local" or "MIND YOUR OWN BUSINESS, DON'T TOUCH THIS!" :) ) works well since that's what it is. – Fattie Jun 12 '16 at 20:29