- Home /
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.
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.
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?
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)
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.
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
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?
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.
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?
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).
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");
}
Answer by $$anonymous$$ · Feb 15, 2013 at 12:59 AM
does
void Awake() { FadeIn(); ContinueButton(); FadeOut();}
work for you?
Sorry, it all runs at once without waiting for the player to click the action key
Your answer
Follow this Question
Related Questions
Pause game while sound plays 1 Answer
delayed function problem 1 Answer
3, 2, 1 GO! 3 Answers