Hello. I am trying to create a thread so as to manage some delay inside a process. I tried with Sleep and Thread, but it freezes my project because it blocks the main thread. In C# it is named Coroutine. I asked myself if there is something more or less similar in C++. I found some examples on the Web so as to create a new thread, but I cannot achieve my goal. Any help will be appreciated. Maybe just a clean example. Thank you ++
That being said, you don't need coroutines to achieve a particular functionality, it just simplifies things.
Show us the code that you say is causing blocking in main. join() is a blocking call, so if you make a single thread and then immediately wait for it to join, it doesn't do much good.
for (int i = 0; i < noEnemies; i++)
{ // check for collisions with enemies
CollisionDistances cd = enemies[i]->CharacterCollides(character);
if (cd.bottom > 0.1f) // check collision
{
Enemy* enemy = enemies[i];
for (int k = i; k < noEnemies - 1; k++)
{
enemies[k] = enemies[k + 1];
// now enemy is dead
enemy->deadNow(); // start fading
character->Jump(false); // bouncing FX
// need to delay an object destruction here calling waitMe()
// clean array
enemies[noEnemies - 1] = NULL;
noEnemies--;
}
}
Ok. I will try to be clear. When an enemy object is killed by the player, I fade it calling another function which manages GameObject. So I want to delay the destruction because the sprite is fading. Using Sleep, I freezes the main process.
However maybe it could be more interesting for me to destroy the GameObject itself when transparency is near to zero. I tried, but delete(this) kills the main thread too.
This is tricky. Coroutines would certainly be the correct solution, but your design needs to account for them first. They're not something you can just drop into your code and expect it to work.
I found a link with an interesting alternative so as to delay a function. This is exactly what I searched. Maybe it is not the best way, but it works well as expected. I share with you the link - it could be useful for other members. However thank you for your help and your kind and relevant advices ++
There's multiple ways to skin this cat, but personally I would just handle all of this within the update logic within each frame/timestep of your game. If an enemy is dying or whatever, then part of its death animation includes fading away, and the state for this is handled somewhere and updated each frame, still synchronous with everything else.
Thank you for your advice. When I was searching some explanations on the Web, I read the link which you shared with me coder777, but in my case it freezes my main thread instead to create something new in parallel.
Ganado - as you said, there are many ways to reach my goal, but your idea is really relevant. However it seems that I have to rewrite some parts of the code. Thanks ++
I would strongly advice against using threads for this. You really don't want game logic running completely independently of the main thread. It's going to bite you in the ass eventually. It's better to make the change now while the code is still manageable. You don't have to do it with coroutines like I did, it can also be done with state machines. But trust me, you want this to run in the main thread.
I would like to avoid complex threads independently. I would like to do this in the main thread, but trying to reach my goal, I have not found any efficient way. I have to take a look at the state machine. Thanks ++