SRL Archive - Outdated Tutorials

Creating Your own procedures/functions in SCAR


drizzt

2008-04-02 18:27:32

Creating Your own procedures/functions in SCAR

first off, an explanation that you hopefully don't need, why make functions/procedures?
well, good question, they can be used for things that you do repetetively in your script, like "bankscreen" and "tradescreen" in SRL, very simple, very easy/powerful.

ok, now you ask, what is the difference between a Function/procedure?
well, also, good question, functions return something, like bankscreen will return true if in a bankscreen, false if not. however, a procedure will just try and get you to the bankscreen

now, to start making some procedures!

the basic way to start a procedure would be
procedure procedurenamehere;
begin
end;
and the only difference between starting a function will be in the name.
function functionnamehere: integer; //second part is type of var to return
begin
Result := integer;
end;
since they are so much alike, hopefully they will be quite easy to learn after learning the other.

so here's an example straight from the Scar Resource Library
function PinScreen: Boolean;
Var
x, y: Integer;
begin
Result := FindText(x, y, 'First', upchars, 175, 80, 212, 100);
end;
so, thats crazy simple, all it does is it returns if the text is found on the screen, and that tells if you are in a pinscreen or not. you might ask "why not just do
if FindText(x, y, 'First', upchars, 175, 80, 212, 100) then
…well uhh, what if you bank 7 times in your script? typing all that would be a lot longer, plus not look as good. get it?

also, a cool thing you can do with them, is make the user put in custom variables to use in the function, it would look like this
function functionname(crazynumber : integer): string;
begin
Result := inttostr(crazynumber);
end;
just like declaring a variable at the beginning of your script

so now that we know how and why to use a function/procedure, lets make our own, shall we?
well, what is something that is tedious and pretty stupid looking? what about increasing an integer? lets go with that

first, we'll start our procedure.
procedure incby(var number: integer; increasedby : integer);
begin
end;

you might think i've just thrown a curveball at you by throwing that "var" in there, but all it does is makes sure that that integer is able to be changed, which we will change in the procedure.
next, we'll just add one line to make a good timesaving procedure for the rest of your script?

procedure incby(var number: integer; increasedby : integer);
begin
number := number + increasedby;
end;

and this is what we end up with. so say in your script you wanted to make a variable 5 more than it currently is, just use it as
incby(insertvariable,5);

a lot easier than writing
insertvariable := insertvariable + 5;
each time right?

moving on to making a simple function now?
lets say..you do a lot of dtm finding in the inventory in your script, and get tired of adding "mix1,miy1,mix2,miy2" to the end, which is the box of the inventory that we use, in case you didn't know.

so we could use a function to find the dtm in the inventory each time..start off the function
function invdtm(dtm : integer) boolean;
begin
end;


using "dtm" as the dtm to find, we'd finish off the function by adding
function invdtm(dtm : integer): boolean;
begin
Gametab(4); //move to inventory box
result := finddtm(dtm,x,y,mix1,miy1,mix2,miy2);
end;

so this function you could use as
if invdtm(axe); then mmouse(x,y,0,0);
which would move the mouse to the dtm. simple huh?

however, since i didn't add the var x and y, you would have to use x and y each time. so lets add those, shall we?
function invdtm(dtm : integer; var x, y: integer): boolean;
begin
Gametab(4); //move to inventory box
result := finddtm(dtm,x,y,mix1,miy1,mix2,miy2);
end;
and there we go, would take a while longer to set up, but more flexibility.

i hope you understood everything in my tut, don't forget to rep+ if i taught you anything at all :D

ozozo

2008-04-14 17:25:30

Thank you.

Vivoxity

2008-05-16 18:30:07

Yea, Thanks man.
Helps.
Kinda sketchy.

Immaminor

2008-05-16 23:11:31

Nice tut. Getting ready to work on my first script.