SRL Archive - Outdated Tutorials

Tutorial: SetTimeout


caused

2010-03-13 09:04:30

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).

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.

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.

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


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.

Sir R. M8gic1an

2010-03-13 09:38:23

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 :p Just keep a close eye on it ;)

~RM

MylesMadness

2010-03-13 10:50:10

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

Tim0suprem0

2010-03-14 15:27:40

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.

MylesMadness

2010-03-14 15:48:02

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

Runescapian321

2010-03-14 15:53:57

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?

Sir R. M8gic1an

2010-03-14 16:05:29

From: Tad
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

caused

2010-03-14 20:44:09

From: Tim0suprem0


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