- Home /
A list of commands that will wait before running the next
I have a list that is filled randomly with vector3s and a time to go with them. I want to be able to run each element in order but only if the previous has been run. I am curious if any one has programmed something along these lines before or has a good approach.
Vector3 first = transform.up;
float time = 3.0f;
Vector3 second = transform.up;
float time2 = 5.0f;
Vector3 third = transform.up;
float time3 = 8.0f;
applyDirection(first,time);
wait and see if first is complete
if true
applyDirection(second,time2);
else
nothing
use WaitUntil,WaitUntil Suspends the coroutine execution until the supplied delegate evaluates to true.
WaitUntil can only be used with a yield statement in coroutines.
Supplied delegate will be executed each frame after script $$anonymous$$onoBehaviour. Update and before $$anonymous$$onoBehaviour.LateUpdate. When the delegate finally evaluates to true, the coroutine will proceed with its execution.
Example
public class WaitUntilExample : $$anonymous$$onoBehaviour
{
public int frame;
void Start()
{
StartCoroutine(Example());
}
IEnumerator Example()
{
Debug.Log("Waiting for princess to be rescued...");
yield return new WaitUntil(() => frame >= 10);
Debug.Log("Princess was rescued!");
}
void Update()
{
if (frame <= 10)
{
Debug.Log("Frame: " + frame);
frame++;
}
}
}
@SohailBukhari shows a valid way to handle this task using coroutines. I was writing my comment at the same time. :)
The code you shared is not a list, but I'm going to assume you're using an actual generic List of Vector3's and a List of times. If you're not, it sounds like you probably should be.
The easiest way would involve removing each List element as it's used. If you can't remove them for some reason, you could keep an integer index as a record of the last element used. If that won't work for some reason, you could create a duplicate version of the original list and remove elements from that. If that won't work, we'll talk. There's always a nice way to model simple behaviors like this.
If you did not mean a generic list, you shouldn't use words like list or element. In that case, perhaps the best way involves creating a new kind of object. (Simply because managing a pair or group of related lists can be messy if you're not careful.)
Your new class could be something like a $$anonymous$$oveOrder, which has a Vector3 direction, a float timeToBegin (or timeUntilComplete, whatever), and a bool hasCompleted. Then you could have a list of these objects, and refer to the paragraph above for making use of them.
Thank you for your help :)
I ended up removing elements as they were used. much appreciated.
Answer by Shrikky23 · May 17, 2017 at 11:19 PM
public bool canProceed = false;
public IEnumerator void WaitForTimer(float X)
{
int timer = 0;
canProceed = false;
while (timer < X)
{
yield return null;
}
canProceed= true;
Debug.Log( " Wait for X number of seconds, You can now Proceed";
}
}
Use Coroutines to create timers and do your calculation. If you want to learn something new, Learn Promise timers http://www.what-could-possibly-go-wrong.com/promises-for-game-development/
Thanks! The example you gave was a simple version of what I ended up with. Yours helped a lot. Also the resource you gave for promises was a really great read. I will definately look into using them for future problems.
Answer by toddisarockstar · May 17, 2017 at 09:47 PM
int idx;
Vector3[] v;
float[] f;
void Start () {
v = new Vector3[3];
f = new float[3];
v [0] = new Vector3 (1f, 1f, 1f);
v [1] = new Vector3 (2f, 2f, 2f);
v [2] = new Vector3 (3f, 3f, 3f);
f [0] = 1f;
f [1] = 2f;
f [2] = 3f;
}
void Update(){
if(Input.GetKeyDown("space")){doit();}
}
void doit () {if (idx < v.Length) {
print ("vector is :" + v [idx] + "float is: " + f [idx]);
idx++;
}
else {print ("im all done doing it");}
}
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Java Script command list 3 Answers
Switching Target during Target Lock 1 Answer
Best way to add item to other script list? 0 Answers
How to draw text based on text above it? 0 Answers