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.
// 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(unsignedint 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;
}
elseif(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.
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(unsignedint 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;
}
elseif(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.