[Help] Problem with "sleep" function
Hi i'm trying to make a "/go" command that will do a countdown from 3 to 1 and then say go
for event's ,battles etc' etc'.
and for this i used Sleep (1000) between the numbers (1000 = 1 sec).
meaning i want the cammand to say 3 then wait 1 sec and the say 2 and then wait one sec and so on.
But when i do this in game ("/go") it waits the Combined time of the 3 Sleep functions and then prints:
3
2
1
go
one after the other with no delay.
why doesn't it print:
3
(1 sec delay)
2
(1 sec delay)
1
(1 sec delay)
go
and prints:
(3 sec delay)
3
2
1
go
im adding pics so you can better understand my problem.
https://image.prntscr.com/image/rmXJ...xXRjwXoDhQ.png
https://image.prntscr.com/image/WGfv...hl4vsw68sg.png
BTW: I'm using this files :
http://forum.ragezone.com/f197/relea...4-6-a-1094452/
http://prntscr.com/sfbdxu
Re: [Help] Problem with "sleep" function
Post the complete function code
Also, don't simply sleep for 3 seconds like this:
Quote:
print
sleep
print
sleep
print
sleep
but use a for loop, similar to this:
Quote:
for(int i = 3; i >= 1; i--)
{
print(message + i); // where i is the current second
sleep(1000);
}
Also, use separate threads for things that use sleep.
Don't sleep on the main thread or you'll have surprises where the GameServer will simply crash.
Re: [Help] Problem with "sleep" function
Quote:
Originally Posted by
ispyder
Post the complete function code
Also, don't simply sleep for 3 seconds like this:
but use a for loop, similar to this:
Also, use separate threads for things that use sleep.
Don't sleep on the main thread or you'll have surprises where the GameServer will simply crash.
So you say to make a new .cpp file just for it?
also i didn't understand the loop that well , care to demonstrate using my situation?
(the other code is not in that much importance is just checking if the player is a GM and printing a side note to the player who initiated the Command and one for logging it.)
1 Attachment(s)
Re: [Help] Problem with "sleep" function
That for loop is exactly your situation (except the 'go' message).
The code inside it is executed 3 times and each time it prints a message and sleeps for 1 second.
You don't need to create a separate .cpp file to create a thread.
A thread is not a .cpp file.
How much programming do you know?
I'm pretty sure you can't know what a thread is if you don't know what a for loop is...
You can treat the whole operation from the beginning to the end inside the new thread.
You should also detach it to not block anything. (The detach operation depends a lot on what you need to do and how you do it, so be careful if doing so...)
###
The following is just a quick example that would solve your problem.
You can use Win32 Threads instead of std if you want to:
Quote:
Create a new file (.h) with the following content:
struct Go_Operation_task
{
// the content is in the attached file:
// Go_Operation_task.txt
};
In CommandManager.cpp:
#include "Go_Operation_task.h"
case Command::Your_Go_Command:
{
unsigned int secondsToSleep = 3;
Go_Operation_task goOperation_task(secondsToSleep);
std::thread goOperation_thread(goOperation_task);
goOperation_thread.detach();
}
Don't know if IA Julia 4.6 supports C++11 for std::thread though (I never checked those files)... If it doesn't just change the thread type and adapt the code for it.