Hi guys, recently i have a big problem with sf::sleep. The problem is that sf::sleep stops the whole application. I'm using sf::sleep only in threads, but in some threads sf::sleep wants to work properly, in some doesn't.
What is the difference i saw:
When i call the thread during getting event(for example when i press Q then i call the thread), sf::sleep works
but when i made an AI thread in while(window.isOpen()) and not in getting events sf::sleep lags the whole program. Any ideas why it is like that? My whole program has 2800 lines, so it's hard to show you all of them.
while (window.isOpen())
{
// ruchy AI
if(AI_MOVES == 1 && menuswitch == 3)
{
if(waitforr != 2)
{
if(ordershield == 1 && canuse[2][AI] == 0)
{
ordershield = 0;
Wspellenemy();
}
random[0] = rand() % 100;
random[1] = rand() % 100;
random[2] = rand() % 100;
thread_aiattackfunction.launch();
}
else thread_rspellenemy.launch();
}
// ...
void aiattackfunction()
{
if(random[0] >= 0 && random[0] <= 50)
{
if(canuse[0][AI] == 0 && blockspells[0][AI] != 1)
{
canuse[0][AI] = 1;
if(random[1] > stats[avoid][USER])
{
if(random[2] <= stats[critic][AI])
dealdmg(stats[ad][AI]*2, 0);
else dealdmg(stats[ad][AI], 0);
}
else cout << "Uniknalem\n";
}
}
else if(random[0] >= 51 && random[0] <= 75)
{
if( canuse[1][AI] == 0 && blockspells[1][AI] != 1)
Qspellenemy();
}
else if(random[0] >= 76 && random[0] <= 100)
{
if( canuse[2][AI] == 0 && blockspells[2][AI] != 1)
Rspellenemy();
}
}
void Rspellenemy()
{
if(waitforr != 2 && waitforr != 1)
{
if(stunned[0] != 1)
{
waitforr = 1;
canuse[2][AI] = 1;
T_wait = sf::Time::Zero;
blockspells[0][AI] = 1;
blockspells[1][AI] = 1;
blockspells[2][AI] = 1;
string15.setString("Press Space!");
}
}
else if(waitforr == 2)
{
ultiamount += 1;
if(ultiamount >= 30)
S_Rultibar.setTextureRect(sf::IntRect(0, 0, 271, 10));
else S_Rultibar.setTextureRect(sf::IntRect(0, 0, ultiamount*271/30, 10));
if(ultiamount == 30) // ignore
{
dealdmg(50, USER);
statrefresh(hp, USER); // update the hp bar
sf::sleep(sf::milliseconds(200));
dealdmg(50, USER);
statrefresh(hp, USER);
sf::sleep(sf::milliseconds(200));
dealdmg(50, USER);
statrefresh(hp, USER);
sf::sleep(sf::milliseconds(2000));
blockspells[0][AI] = 0;
blockspells[1][AI] = 0;
blockspells[2][AI] = 0;
waitforr = 0;
}
sf::sleep(sf::milliseconds(170));
}
}
The problem is in Rspellenemy before it whole end the program is lagged.
The qeustion is how to do the AI(where to call it), to make its spells not lag the whole program.
if u don't understand what i exactly wrote, please ask, i'll try to translate it better.