Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Captain_Dando · Feb 14, 2013 at 12:36 PM · guiyield

How can I run functions in succession, each waiting for the last to complete?

Hi everyone, is it possible to run functions in succession without them being inside of one another?

for example, let's say I have four functions;

FadeIn(); ContinueButton(); FadeOut();

In this example, I'd like to have a GUI button in ContinueButton that waits for the player to press it before the camera fades out in the previous function. However, I'd like to do this without putting FadeOut inside of ContinueButton's GUI.Button event, rather having it just allow the next function to run when the button is pressed. So far, when I run the program FadeOut will obviously just run at the same time.

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Benproductions1 · Feb 14, 2013 at 10:35 PM 0
Share

What your trying to do is run functions Asyncronously. I suggest com$$anonymous$$g up with another method of accomplishing your goals ins$$anonymous$$d of asyncronous functions.

avatar image dubbreak · Feb 15, 2013 at 12:36 AM 0
Share

Please describe what you are trying to do step by step. $$anonymous$$g. player does x, the y shows up, then they do z but that gets y to fade.

It's kind of ambiguous as is. Sounds like using events or some kind of publisher subscriber pattern might help, but then again it could be that you just need to be doing everything synchronously (e.g. doing a yield return on a coroutine rather than just starting it).

Have any code for what you are doing now?

avatar image Captain_Dando · Feb 15, 2013 at 07:35 AM 0
Share

Sorry about the late reply, I'm in a different time zone so I only woke up recently. I don't have any code yet, as I'm trying to understand what I'll need to do before I begin. Basically, I'm extending the editor to create an "Event editor" which will allow me to assign a series of events to a list gameobject with colliders. you'll be able to select a gameobject from a list and then add or remove events in a gui, which will save to a script. The way I want to do it is that for each event listed in the GUI, a function will be called with parameters set in the gui. so for example, if I wanted to have a bit of dialogue (in text in a box like in the old final fantasies) followed by a change in scene, I would run Dialogue("somedialogue"), FadeOut(3 seconds), ChangeScene(), FadeIn(3 seconds). However, I wouldn't want FadeIn to run before the player has read all the dialogue in the dialogue box and clicked to continue, and considering the functions will be added through the gui I can't have them run inside of eachother (I think)

avatar image Jeremy Hindle · Feb 15, 2013 at 07:49 AM 0
Share

If you are using C# take a look at coroutines. They allow you to run events in order. I use them for all my ordered processes and cut scenes.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Jeremy Hindle · Feb 15, 2013 at 07:52 AM

If you are using C# take a look at coroutines. They allow you to run events in order. I use them for all my ordered processes and cut scenes.

Reading what you are trying to achieve perhaps you could do something fancy with Delegates (Action<>) passing pre-defined functions into coroutines.

Here are the docs on Action Delegates: http://msdn.microsoft.com/en-gb/library/system.action.aspx

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Captain_Dando · Feb 15, 2013 at 08:08 AM 0
Share

Thanks, I'm not too experienced in co routines and action delegates but you've given me a good lead. I've seen one or two examples of co-routines, from what I understand they are a vital component of finite state machines?

avatar image Jeremy Hindle · Feb 15, 2013 at 08:12 AM 1
Share

Yep they are incredibly important. I highly recommend just learning how coroutines work completely separate from unity3d before applying them to a situation. Once you've got to grips with them you will find things like this pretty trivial.

avatar image Captain_Dando · Feb 15, 2013 at 08:14 AM 0
Share

Sorry, one last question, will the next function in the co-routine only run once the previous function returns something? for example

 IEnumerator $$anonymous$$yCoroutine()
 {
     DoSomething();

     DoSomethingElse();
 }

if DoSomething() had the button, would I need to have it return something after the user clicks the button before the next function will run?

avatar image dubbreak · Feb 15, 2013 at 08:18 AM 1
Share

Yes. Those methods are synchronous (DoSomethingElse won't happen until DoSomething is finished). Of course unless DoSomething launches a coroutine as well.

When you launch your coroutine (with start coroutine) you code that launches it will continue onto the next line (similar to threading). That is unless you launch it with

 yield return StartCoroutine($$anonymous$$yCoroutine);

In which case it will wait (of course that's not what you want right now, I think but it's good to know).

avatar image dubbreak · Feb 15, 2013 at 08:45 AM 1
Share

You'll also want to look into events (not action delegates.. you need delegates for events and action and func delegates are great built in delegates to make writing quicker.. but best to start with writing your own delegates).

Why events? You might want to fire off something asynchronously with a coroutine, but tell something else to fire off (or multiple things) once it has finished. I use events a lot. Unfortunately you can't declare events in js (you can subscribe to c# events and even declare delegates and do lambdas but no event creation).

 //declared in class
 public delegate void RequestFailedDelegate(string message);
 public event RequestFailedDelegate RequestFailed;
 
 event Action Connected; //uses action rather than delegate void ConnectionDelegate()
 
 //subscribing to event
 RequestFailed += doSomethingOnRequestFailed;
 RequestFailed += somethingElseThatHappensOnRequestFail;
 Connected += HandleConnected;
 
 //event handlers - must match delegate otherwise you'll get a compile error
 
 void doSomethingOnRequestFailed(string message)
 {
    RequestFailed -= doSomethingOnRequestFailed; //unsubsribe
   //use message
 }
 
 void somethingElseThatHappensOnRequestFail(string message)
 {
     RequestFailed -= somethingElseThatHappensOnRequestFail; //unsubsribe
   //use message
 }
 
 void HandleConnected()
 {
    //can now do stuff relying on connection
 }
 
 //raising event
 
 if(Connect())
 {
    if(Connected!=null) Connected(); //always check whether there are any subscribers before raising event
 }
 else
 {
    if(RequestFailed!=null) RequestFailed("Connection failed");
 
 }
 
 
Show more comments
avatar image
0

Answer by $$anonymous$$ · Feb 15, 2013 at 12:59 AM

does

 void Awake() { FadeIn(); ContinueButton(); FadeOut();}

work for you?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Captain_Dando · Feb 15, 2013 at 07:36 AM 0
Share

Sorry, it all runs at once without waiting for the player to click the action key

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Pause game while sound plays 1 Answer

delayed function problem 1 Answer

3, 2, 1 GO! 3 Answers

A yield function for function Update? 1 Answer

yield WaitForSeconds makes GUI button disappear 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges