Delaying a For loop.

I have been working on a JRPG turn based style game and i'm having some trouble with delaying a loop. I currently have a loop that loops through and makes all the enemies attack the players characters but i want there to be a small delay between each enemy attack to render out how much damage the enemy dealt and to what character.
So i have a class for the scene with it's own update and render functions aswell as other functions that are used in the actual battle.
I have tried with a few different options to create a delay in one of the class funtions and i'm currently trying to get this to work:

This is very simplified as there is too much code that is not really neccesary.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// deltaTime comes from the main functions and is updated each frame.
void Class::Update(float deltaTime)
{
	if(delayTimer > 0)
	{
		delayTimer -= deltaTime;
	}
}
// The function the delay should be in
void Class::attackOrder()
{
// What this functions is supposed to do is to cehck the speeds of the characters
// in the fight and let them attack in the right order
// enemyList is a vector with my custom data type of the enemy
for(unsigned int i = 0; i < enemyList.size(); i++)
	{
		// Pause the loop if enemy has attacked
		if(delayTimer >= 0)
		{
			i--;
			continue;
		}

                //characterList is the vector with all the players characters
		if(characterList[characterIndex].speed >= enemyList[i].speed)
		{
			AttackEnemy();
			break;
		}
		else if(characterList[characterIndex].speed < enemyList[i].speed)
		{
			EnemyAttack(i);
                        // Set the delay
                        delayTimer = 3.0f;
		}
	}
}


As you can see if the delayTimer is above 0 i want it to count down to 0 to continue to run the for loop. But the problem is that when i pause the loop by setting the delayTimer to 3 everything else stops too so it never actually starts to count down.
Is there a way i can do something about this or do i need to redesign the entire turn based system?

I might have missed something so tell me if you want more code or if you need it better explained.
use sleep(time in milli-seconds)
the entire if on line 18 doesn't make sense. Remove it.

Instead use a time stamp (time() http://www.cplusplus.com/reference/ctime/time/ if seconds are accurate enough) like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void Class::attackOrder()
{
// What this functions is supposed to do is to cehck the speeds of the characters
// in the fight and let them attack in the right order
// enemyList is a vector with my custom data type of the enemy
for(unsigned int i = 0; i < enemyList.size(); i++)
	{
		if((enemyList[i].timestamp + enemyList[i].delay) <= time(NULL)) // check if the enemy fullfilled the pause
		{
		enemyList[i].timestamp= time(NULL); // restart the time for the next attack
                //characterList is the vector with all the players characters
		if(characterList[characterIndex].speed >= enemyList[i].speed)
		{
			AttackEnemy();
			break;
		}
		else if(characterList[characterIndex].speed < enemyList[i].speed)
		{
			EnemyAttack(i);
                        // Set the delay
                        //delayTimer = 3.0f;
		}
		}
	}
}
This looks good, but i'm unable to test this at the moment as i'm not by my computer.
But i'm a little confused about how it works, so you have a timestamp that is set to a specific point in time, and a delay of how many seconds you want the thing to delay. You then check if the timestamp and the delay together is less or equal than the current time or am i incorrect?
Oh and then you set a new timestamp for the next loop.

But i will try this out when i can get access to my computer, thank you for your help.
you're correct.

there might be two problems:

1. The if on line 8 might be relocated to the else branch on line 17 (before that if) since right now it pauses the AttackEnemy() as well.

2. timestamp must be set before line 8. You might consider to set it to 0 which means no delay
Topic archived. No new replies allowed.