[Help] Problem with "sleep" function

Results 1 to 4 of 4
  1. #1
    Member omercohen122 is offline
    MemberRank
    Apr 2015 Join Date
    50Posts

    information [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.



    BTW: I'm using this files :
    http://forum.ragezone.com/f197/relea...4-6-a-1094452/

    Last edited by omercohen122; 12-05-20 at 02:29 PM.


  2. #2
    Member ispyder is offline
    MemberRank
    Aug 2015 Join Date
    76Posts

    Re: [Help] Problem with "sleep" function

    Post the complete function code

    Also, don't simply sleep for 3 seconds like this:
    print
    sleep
    print
    sleep
    print
    sleep
    but use a for loop, similar to this:
    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.

  3. #3
    Member omercohen122 is offline
    MemberRank
    Apr 2015 Join Date
    50Posts

    Re: [Help] Problem with "sleep" function

    Quote Originally Posted by ispyder View Post
    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.)

  4. #4
    Member ispyder is offline
    MemberRank
    Aug 2015 Join Date
    76Posts

    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:

    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.
    Attached Files Attached Files
    Last edited by ispyder; 12-05-20 at 08:07 PM. Reason: added quick solution



Advertisement