- Home /
Create a delay between function executions
I've been reading over the C# Yield answers for hours - I cannot seem to make anything work with what I want to do. Half my game is created and I can't believe this is where I got stuck! Haha!
So, What I want to have happen is a delay inbetween some functions...
`public void executeAttack(int pa){
/*
* Enemy select attack based on players selected attack and weakest defence.
* Attempt to execute an attack BASED ON THE SPEED OF THE PLAYER AND OPPONENT
* To do that, if (PlayerSpeed > Enemy){player.Attack(); enemy.Attack()} else { enemy.Attack(); player.Attack() }
*/
if ((player.Vitals[1] - skillist.skills[pa,5,0,0]) >= 0){ // Check if you have enough AP to attack
enemy.SelectedAttack = enemySelectAttackAI(pa); // Enemy select Attack AI (Currently just random based on what skills the enemy has)
player.SelectedAttack = pa; // Set the player selectedAttack
if(player.Attributes[6] >= enemy.Attributes[6]){ // Attribute[6] = speed
player.Attack(enemy);
//I want to create delays in between these functions!
player.checkStatusEnd();
checkHealth(enemy); // Should make this into a function
checkReason(player);
enemy.Attack(player);
enemy.checkStatusEnd();
checkHealth(player);
checkReason(enemy);
} else if (player.Attributes[6] > enemy.Attributes[6]) { // Need a better way to display messages...
enemy.Attack(player);
yield return new WaitForSeconds( 2.5f );
enemy.checkStatusEnd();
checkHealth(player);
checkReason(enemy);
player.Attack(enemy);
player.checkStatusEnd();
checkHealth(enemy);
checkReason(player);
}
} else {
showMessage("You do not have enough AP to use that attack");
}
}`
Now I tried so many different solutions... Obviously, I tried make executeAttack return IEnumerator... no joy.
I've tried creating while loops that loop through till time > x (x being the delay time) Most my results end in either 1) Nothing at all happening 2) Infinite Iterations (Unity Crashes) 3) If I call a seperate delay function using StartCoroutine(delay(10)); etc, it just runs along side the code I'm trying to pause.
Most of these are actually expected results.. I'm just baffled as to the logic behind making this work.
Answer by SilverTabby · Aug 08, 2011 at 03:43 PM
Your main problem is that your method has to 1) say it returns an IEnumerator, and 2) actually return one. The statement yield return
returns the correct IEnumerator and will cause it to compile fine.
I'll try to provide a working example for C# coroutines. Hopefuly this will help you figure out what's going on.
public bool useEarlyOut = false;
public void Start()
{
//creates a coroutine that is completely independent of this method.
StartCoroutine(timingTest());
}
public IEnumerator timingTest()
{
//creates a coroutine that is completely independent of this method.
StartCoroutine(waitAndPrint("Third, this is printed", 1.0));
StartCoroutine(waitAndPrint("Second, this is printed", 0.5));
Debug.Log("First, this is printed");
//waits until this coroutine finishes.
yield return StartCoroutine(waitAndPrint("Fourth, this is printed", 1.5));
Debug.Log("Fifth this is printed");
if(useEarlyOut == true)
{
Debug.Log("The Loop will not be printed");
//kills this coroutine.
yield return break;
}
for(int count = 10; count > 0; count--)
{
Debug.Log("This will be printed "+count+" more times, once every frame.");
yield return null; //waits a frame
}
}
public IEnumerator waitAndPrint(String text ,float howLong)
{
//waits until this amount of time has passed.
yield return new WaitForSeconds(howLong);
Debug.Log(text);
}
yield return ____________________________________________;
- null //waits for one frame to pass and resumes during the Update calls
- break //kills this coroutine immediately.
- StartCoroutine(nameOfCoroutine()) //waits for nameOfCoroutine to finish
- new WaitForSeconds(X) //waits for X seconds
- new WaitForFixedUpdate() // waits for one frame to pass and resumes during the FixedUpdate calls
Hmm thanks for that, I guess a full rethinking of the program is in order.
Well - I guess this is how you learn! Will plan for this in future.
Thanks again