SRL Archive - Outdated Tutorials

Procedures and Functions' importance


Naum

2009-04-18 12:12:02

A brief Note on Procedures and Functions



Intro.
Lately I have seen many people using procedures when they should be using functions and visa verse. Maybe they do not know the usage's of them the benefits and drawbacks. In this tutorial I will explain to the beginner scriptwriter on the usages of procedures and functions. I'll also touch on the interdependence of the two (how they need each other)


The Procedure (Proc)
The procedure is widely used to achieve something through a given way. It doesn't output a result but can change one.

Definition - Defines a subroutine that does not return a value

Version 1. The procedure looks like this:
Procedure Work;

This procedure operates with no parameters, thus meaning that it will preform a task which cannot be altered by the user without tankering with the procedure itself.

Version 2.
Procedure Work(Physically : Boolean);

This procedure operates within given parameters and is specified to work within them depending on the boolean; for instance here is the procedure

Procedure Work(Physically : Boolean);
Begin
If Physically Then
WriteLn('Hard physical labour')
Else
WriteLn('yoga');
End;

If you change Physically to True it will output - Hard Physical Labour
Otherwise it will output - Yoga


The Function(func)
The function is widely used to return a value of result which shall effect the way a script shall run.

Definition - Defines a subroutine that returns a value

Version 1. The function looks like this:
Function Eat : Boolean;
This function will return a data-type only one value can be returned from a function.

Version 2.
Function Eat(What : String) : Boolean;
This returns a datatype as same as the first one but it also has parameters to work in. So it will be influenced by it and will also return a result.

For instance:
Function Eat(What : String) : Boolean;
Begin
If GetDebugLine(GetDebugLineCount - 1) = 'yoga' Then
Result := False
Else
Result := True;
End;

What this will do is return a result based on what the work procedure outputs in the debug box.

Please note that when you make a function SCAR automatically creates a boolean for it.

Functions can also be used as so:

To Output a string - 'Function Thing:String;'

To Output an Integer - 'Function Thing:LongInt;'

Many otherways can be used all you do is 'shove' a datatype at the end.



Interdependance

Procedures and functions can depend on each other every good scripter will know about the advantages of having functions in procedures and so on.

For example say using our Work procedure and our Eat function we want to make the person who did manual labour to get GoodFood

So:

Function Eat : Boolean;
Begin
If GetDebugLine(GetDebugLineCount - 1) = 'yoga' Then
Result := False
Else
Result := True;
End;

Procedure Work(Physically : Boolean);
Begin
If Physically Then
WriteLn('Hard physical labour')
Else
WriteLn('yoga');
End;

Begin
If Random(2) = 0 Then Work(True) Else Work(False);
If Eat Then WriteLn('Ate food') Else WriteLn('STOP YOGA!!');
End.

In this example The function Eat relies on The procedure Work, therefore they work with each other.

As an example In real-time Scar-scripting I used:

Procedure DetectGas;
Begin
If GasFinder(x, y) then
begin
MouseFlag(MMCX, MMCY, 20, 20, 0);
SleepAndMoveMouse(7000 + Random(300));
end;
end;

The function GasFinder :

function GasFinder(GX, GY : Integer): Boolean;   //Thanks Wizzup!
Var
x1, y1, x2, y2, I, time, c: Integer;
R, G, B: Integer;
H, S, L: ExtEnded;
X, Y, Z: ExtEnded;
Gas, Gas2: TPointArray;
Colors, C2: TIntegerArray;
begin
Try
x1 := gx - 35;
If x1 < 0 Then
x1 := 0;
y1 := gy - 35;
If y1 < 0 Then
y1 := 0;
x2 := gx + 35;
If x2 > 515 Then
x2 := 515;
y2 := gy + 35;
If y2 > 336 Then
y2 := 336;
Time := GetSystemTime;
Result := False;
c := GetColorToleranceSpeed;
ColorToleranceSpeed(2);
SetColorSpeed2ModIfiers(0.2, 0.2);
FindColorsTolerance(Gas, 8886170, x1, y1, x2, y2, 20);
ColorToleranceSpeed(c);
If length(Gas) < 10 Then
Exit;
Colors := GetColors(Gas);
C2 := Colors;
ClearSameIntegers(C2);
// writeln('Colors Length: ' + inttostr(length(colors)));
// writeln('C2 Length: ' + inttostr(length(C2)));
If Length(C2) < 40 Then
Exit;
For I := 0 To High(Gas) Do
Begin
ColorToRGB(Colors[I], R, G, B);
If Abs(R - G) <= 10.0 Then
Begin
ColorToHSL(Colors[I], H, S, L);
If (H > 11.5) And (H < 16.0) And (S < 12.0) Then
Begin
ColorToXYZ(Colors[I], X, Y, Z);
If (Abs(X - Y) < 5.0) And (Abs(X - Z) < 5.0) And (Abs(Y - Z) < 5.0) Then
Begin
SetLength(Gas2, Length(Gas2) + 1);
Gas2[High(Gas2)] := Gas[I];
End;
End;
End;
End;
//WriteLn('time: ' + IntToStr(GetSystemTime - Time));
//WriteLn('gas2 length: ' + IntToStr(Length(Gas2)));
Result := (Length(Gas2) > 30); // The only number you might want to tweak.
//debugtpa(Gas2, '');
if result then
begin
SaveScreenshot(ScriptPath + 'Found WIZZUP gas ' + TimeRunning + '.bmp')
Exit;
end;
//debugtpa(gas2, '');
Except WriteLn('No window selected'); end;
End;

Will try to detect a gas leak whilst mining and will alter the procedure according to its result.

It can be used in many other ways, such as in my firemaking script.

Procedure Relocate;
Begin
MFF(MMCX-Random(5), MMCY+Random(4), 5, 5);
FFlag(0)
end;

Function CantLight : Boolean;
Begin
If FindBlackChatMessage('ight a fir') Then
Result := True;
end;


Procedure WaitWhileFireMaking;
Begin
If Cantlight Then
Begin
Relocate; //here

This uses the function CantLight to trigger the procedure Relocate.


Thanks all.

From Uncle Hermpie:
From: Hermen
if something then result :=true else result := false;
can be replaced by:

Result := something


e.g

Result := GetColor(304, 809) = 55656




(its 5 am here so if I missed something or goofed something up, its because i am shattered).

There is also a version 3 of a function or procedure this involves using it as a type such a SRL_Procs

ian.

2009-04-18 12:16:29

:)

you're making too many tuts. lol

glad you made this though.. now make one saying that leechers should stop being stupid D:<

impiwimpi

2009-04-18 12:43:38

I already knew this, though it's very handy to see it all in 1 tut :)

Naum

2009-04-18 14:14:32

I dont make that many tutorials, I haven't released one for about 2 months, so this is only a warm-up :p.

Thanks for the comments.

Da 0wner

2009-04-20 13:46:21

Functions can do everything procedures can do and more.

Sir R. M8gic1an

2009-05-03 00:40:00

From: NaumanAkhlaQ

Please note that when you make a function SCAR automatically creates a boolean for it.



Function IsThisWrong: string;
begin
if true then
Result := 'yes'
else
Result := 'well, it is';
end;

i wonder what boolean was created there?

~RM

Da 0wner

2009-05-03 01:30:20

Please note that when you create a function in SCAR it automatically creates a result variable which is what returns the value.

ShowerThoughts

2009-05-08 23:33:33

if something then result :=true else result := false;
can be replaced by:

Result := something

Naum

2009-05-09 00:04:50

Thanks Rep+++