SRL user-defined procedures!
With the release of revision #29, SRL now has a new feature available; user-definable procedures. Simply explained, they consist of an array of procedure variables which are called within various common SRL procedures and functions. It is nothing complicated at all, this is how the procedure variables are declared:
SCAR Code:var
srl_Procs: array [0..8] of procedure();
The main purpose of these, as you'll find more about in this tutorial, is to assign your procedures of preferral, to these procedure variables. These assigned procedures will then be called whenever certain SRL functions which call these procedures, are called. For example, if a procedure is assigned to srl_Procs[SRL_AntiBan], that procedure will be called whenever Flag or FFlag is called. This might be useful for i.e. doing antiban actions while waiting for the flag to disappear of the minimap.
Before you continue reading, you might want to take a look inside the globals.scar include (found in Includes/SRL/core folder). Once you open the include, scroll down to where you see all the SRL_On(...) constants. Here, you'll see which procedure variables that currently are available.
SCAR Code:const
srl_AntiBan = 0; { Your AntiBan procedure to be called during various SRL functions and procedures. (Flag, FFlag) }
srl_OnFindMod = 1; { After a player or Jagex mod is detected talking in the chat box. }
srl_OnFindDead = 2; { After the text 'Oh dear you are dead' is detected. }
srl_OnFindFight = 3; { After detecting a fighting random. }
srl_OnFindTrade = 4; { After the trade has been attempted (either success or failure). }
srl_OnNextPlayer = 5; { While the players are logged out and before CurrentPlayer changes. }
srl_OnSendStats = 6; { After SRL Script Stats are sent to the server. }
srl_OnRandomCall = 7; { Called in FindNormalRandoms, FindInventoryRandoms, FindNonInventoryRandoms. (NOT ONLY WHEN RANDOMS ARE DETECTED). }
srl_OnFindRandom = 8; { After a random event is detected. (FNR, FIR, FNIR, FT) }
At the time of me writing this, the procedure array consists of 9 procedure variables. All of them have self-explanatory names and are clearly stating upon which situations they are called. In addition, the comments should help you understand every single one of them. But - remember that these constants only are integers which represent the position of the procedure that the constant represents, in the [0..8] array of procedures.
Regarding that, let's take this constant as an example:
SCAR Code:const
srl_OnFindRandom = 8;
That constant has an assigned value of 8, meaning that calling SRL_Procs[srl_OnFindRandom] would be the same as calling SRL_Procs[8] (Keep that in mind, we'll get right back to that.)
Inside AntiRandoms.scar, where all the random detecting functions are, we currently have four procedures/functions that use the above mentioned constant. Whenever one of the procedures in that include (FindNormalRandoms, FindInventoryRandoms, FindNonInventoryRanoms, FindTalk) detects a random event in RuneScape, something like this gets executed:
SCAR Code:if (srl_Procs[srl_OnFindRandom] <> nil) then
srl_Procs[srl_OnFindRandom]();
Two lines. So simple, yet so awesome. The first line, if (SRL_Procs[SRL_OnFindRandom] <> nil) then, checks whether the procedure SRL_Procs[SRL_OnFindRandom] variable is assigned to an actual procedure. Earlier above, our constant example "SRL_OnFindRandom" is declared as nothing more than an integer constant, yes? That makes the two lines above be the same as simply checking whether SRL_Procs[0] points to a procedure. So, basically - if a procedure has been assigned to SRL_Procs[SRL_OnFindRandom], this line returns true.
And, the next line ofcourse calls the procedure assigned to the procedure variable, if it points to a procedure.
Assigning a procedure to one of the procedure variables is really easy, it's almost like assigning normal variables! Just do like this:
SCAR Code:srl_Procs[srl_OnFindRandom] := @AfterRandom;
I told you it was easy!
In the example line above, we assign srl_Procs[srl_OnFindRandom] to a procedure named AfterRandom; which should be declared in your script somewhere above where you actually assign it to a srl_Proc. Just don't remember to add a "@" before the procedure's name, as this is crucial for SCAR to interpret it correctly. Also, the procedure must NOT have any parameters!
And that's all there is to it, now AfterRandom will be called every single time a random is found!
Now with a full script example!
SCAR Code:program SRLprocvarsexample;
{.include SRL/SRL.scar}
procedure LOL;
begin
WriteLn('Yay?');
end;
begin
SetUpSRL;
srl_Procs[0] := @LOL; { Here, we assign the SRL_Procs[0] variable (declared in Globals.scar) to procedure LOL;. }
if (SRL_Procs[0] <> nil) then { Not really needed in this exact case, but just an example on
how to check if a procedure variable actually is assigned to a procedure. }
SRL_Procs[0](); { Here, we finally call the procedure assigned to the variable. }
end.
So, what is this system really good for?
It has countless possibilities, the limit here is your mind.
For example, why not do some quick antiban while waiting for the flag to dissapear? Switch gametabs, or something else less time consuming. And, what do you want to happen whenever you detect a RS moderator or if your player dies? While we're at it, why not switch worlds on NextPlayer or do something time consuming (like a GetPage) while the player is logged out?
The possibilities are endless, use your imagination!
This marks the end of this short tutorial. I hope you didn't mind reading it despite that it is in a slightly bad state. (Which I plan to improve on after I get some feedback.) Please give me and ideas or comments for what parts I explained wrongly or poorly, and any ideas, comments or tips are welcome!
As a final notice, I highly recommend you to check out globals.scar to get better understanding of this feature. If there were any part that were hard to understand in this "tutorial", please let me know.
Happy scripting ^^
- EC!