Results 1 to 8 of 8

Thread: Tutorial: SetTimeout

  1. #1
    Join Date
    May 2009
    Posts
    799
    Mentioned
    2 Post(s)
    Quoted
    16 Post(s)

    Default Tutorial: SetTimeout

    program TimeoutTutorial;
    begin


    Set Timeout is a quite fancy function. Some of you may know it from javascript. It adds some kind of a rudimentary Multithreading to scar, just like timers would do (Check out my Tutorial about Timer).

    SCAR Code:
    function SetTimeout(secs: Integer; procname: string): Integer;

    For those of you who don't know what settimeout does, thats quite simple. SetTimeout Runs a given procedure or function after a certain time has passed.


    Here's one example on how to use SetTimeout to write your progress Report after every second that your script is running.
    SCAR Code:
    program New;
    var
      loads:integer;

    procedure proggy;
    begin
       ClearReport;
       AddToReport('Loads Done:' + inttostr(loads));
       AddToReport('Time Running:' + inttostr(GetTimeRunning));
      // Woohow, a recursion ;]
       SetTimeout(1000,'proggy');
    end;

    begin
      //Initialize Proggy
      proggy;

      //MainLoop
      repeat
        wait(5000);
        inc(loads);
      until false;
    end.


    Functions to control SetTimeout

    I scar there are two functions that you can use to control timeouts.

    SCAR Code:
    ClearTimeout(s:String);
    //Cancels a single timed procedure set with SetTimeout.

    SCAR Code:
    procedure ClearTimeouts;
    //Cancels all timed procedures set with SetTimeout.
    (Quoted from freddies helpfile)

    You can use these functions to break a timeout operation at any point after it has been initiated.


    end.
    Last edited by caused; 03-15-2010 at 12:49 AM.

  2. #2
    Join Date
    Jul 2007
    Location
    Right now? Chair.
    Posts
    8,491
    Mentioned
    3 Post(s)
    Quoted
    11 Post(s)

    Default

    I'd just like to point outhat although cool, these are somewhat unreliable. More often than not, from my experience, they'll mess up your script, although of course encourage people to play around with it Just keep a close eye on it

    ~RM

    I & I know Zion. It is in the spirit, body and mind of every one of us
    RMouse(obj: TMSIObject): boolean;

  3. #3
    Join Date
    Mar 2007
    Posts
    3,117
    Mentioned
    0 Post(s)
    Quoted
    2 Post(s)

    Default

    I would use timers for this as these seem to cause scar to crash quite often.

  4. #4
    Join Date
    Oct 2006
    Posts
    1,072
    Mentioned
    0 Post(s)
    Quoted
    1 Post(s)

    Default

    Can you guys clarify some stuff? I have a few questions.

    So caused, when you say it adds multithreading, does that mean it is running simultaneously to the main loop? Because just looking at the code, I don't stand why AddToReport gets executed if you're just looping wait and inc until false.

    What happens when proggy calls itself at SetTimeout(1000, 'proggy')? I don't know how recursion works, but wouldn't it just keep looping back to proggy instead of going on to the wait inc loop?

    Sorry guys, I'm one of those members who doesn't actually know a lot about coding Thanks for any explanations.

  5. #5
    Join Date
    Mar 2007
    Posts
    3,117
    Mentioned
    0 Post(s)
    Quoted
    2 Post(s)

    Default

    Well it means that every 1 second when the script isn't already running a procedure to rune proggy.

  6. #6
    Join Date
    Sep 2007
    Location
    Pennsylvania
    Posts
    3,405
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    I've used these for a simple way to utilize a RunScriptForXTime constant Nice tut.

    Does anyone know/think these would be feasible for things such as antirandom checks (just 30ms) or camera antiban?

  7. #7
    Join Date
    Jul 2007
    Location
    Right now? Chair.
    Posts
    8,491
    Mentioned
    3 Post(s)
    Quoted
    11 Post(s)

    Default

    Quote Originally Posted by Tad View Post
    I've used these for a simple way to utilize a RunScriptForXTime constant Nice tut.

    Does anyone know/think these would be feasible for things such as antirandom checks (just 30ms) or camera antiban?
    No.

    Camera antiban => It might be moving the mouse somewhere, and by moving the camera it'll end up it the wrong place.

    Keep in mind it'll be running tw threads and therefore performing simultaneous actions.

    Although not simutaneous (programming wise) the effect in real (noticeable) time will be simultaneous.

    I've tried it for antirandoms and it didn't work out well either.

    It would clog up the system when going through a complicated / consuming algorithm and checking randoms a the same time.

    Also, if the randoms need to check the music tab, you have problem #1 again.

    ~RM

    I & I know Zion. It is in the spirit, body and mind of every one of us
    RMouse(obj: TMSIObject): boolean;

  8. #8
    Join Date
    May 2009
    Posts
    799
    Mentioned
    2 Post(s)
    Quoted
    16 Post(s)

    Default

    Quote Originally Posted by Tim0suprem0 View Post

    So caused, when you say it adds multithreading, does that mean it is running simultaneously to the main loop?
    Yup.

    What happens when proggy calls itself at SetTimeout(1000, 'proggy')?
    Well, technically a thread or timer(not sure what freddy used) starts, which is delayed by 1000ms, after 1000 ms your function is called. And thats what happens over and over again.

    Sorry guys, I'm one of those members who doesn't actually know a lot about coding Thanks for any explanations.
    Not a problem :].. Thats what the tutorial Island is for .

    ~caused

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •