SRL Archive - OSR Outdated Tutorials

Learn to use Cases!


BobboHobbo

2009-09-02 16:46:53

Learn to use Cases!


In this basic tutorial I will be showing you the advantages of using cases, what they are, why we use them and I will show you how much better it makes your script look!

What are cases?
Cases are basically a method which is simply faster and neater, to call many things in one procedure, instead of having many procedures.

Difference between non cased and cased procedures.
For example of a non-cased procedure:

procedure WhatTreeWeCutting;
begin
if ((GetSkillLevel('woodcutting')) < 14) and
((GetSkillLevel('woodcutting')) >= 1) then
TreeToCut := Normal;
if ((GetSkillLevel('woodcutting')) < 29) and
((GetSkillLevel('woodcutting')) >= 15) then
TreeToCut := Oak;
if ((GetSkillLevel('woodcutting')) < 99) and
((GetSkillLevel('woodcutting')) >= 30) then
TreeToCut := Willow;
end;

And a cased procedure of the same thing:

procedure DoWhatToCut;
begin
Case (GetSkillLevel('woodcutting')) of
1..14: TreeToCut := Normal;
15..29: TreeToCut := Oak;
30..99: TreeToCut := Willow;
end;
end;

Note: notice how i use 1..15. Cases dont have to have this, this basicly means the numbers 1 to 15, instead of typing it as 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15: TreeToCut……

The method above is the Integer case way, you can also do a string way as exampled bellow:


procedure DoWhatToCut;
begin
Case LowerCase(TreeToCut) of
'normal': Writeln('weeee normal tree is what we want to cut');
'oak': Writeln('weeee oak tree is what we want to cut');
'willow': Writeln('weeee willow tree is what we want to cut');
end;
end;

The case above converts TreeToCut to all chars to be lowercase so then you wont have any problems identifying your case.

Failsafe cases
To failsafe a case you must use else at the end of it, so if none of the options were picked it does the else, for example if the case doesnt match the criteria it will do the else:

procedure DoWhatToCut;
begin
Case (GetSkillLevel('woodcutting')) of
1..14: TreeToCut := Normal;
15..29: TreeToCut := Oak;
30..99: TreeToCut := Willow;
else
Writeln('We couldnt detect what tree to cut!');
end;

Other types of Cases (Used for antiban or just random things)
This type of Case bellow is mainly used in antiban, where it has a chance of doing the case but in some cases it will just skip through, an example of this is:

procedure SomeCaseExample;
begin
case random(50) of
1: IdleTime(1000 + Random(1000), 500, 0.5);
10: HoverSkill('random', false);
25: PickUpMouse;
end;
end;

You could also do:

procedure SomeCaseExample;
begin
case random(50) of
1..5: IdleTime(1000 + Random(1000), 500, 0.5);
10..15: HoverSkill('random', false);
25..30: PickUpMouse;
35.. 50: wait(100);
end;
end;

Notice how I use Random(50);, there is also a procedure in SCAR which is called RandomRange(1,10); This would choose a random number in the range which is 1-10.

Advance Casing
Casing is particularly easy, but you could use them in more advance situations, for example. If you are trying to make a multiple tree finding procedure you would want to only make one procedure for it, instead of making a new procedure for each tree to cut. In the example bellow is how ill show you to do it, in 3 procedures/Functions.


procedure LoadTrees(WhatTree: string); // procedure that loads the trees given bellow in the next procedure.
begin
case LowerCase(WhatTree) of
'tree':
begin
Tcolour := 1327929;
Tolerance := 10;
Tree := 'Normal';
UpText := 'n Tree';
FireLV := '1';
end;
'oak':
begin
Tcolour := 7386279;
Tolerance := 10;
Tree := 'Oak';
UpText := 'n Oak';
FireLV := '15';
end;
'willow':
begin
Tcolour := 9422778;
Tolerance := 10;
Tree := 'Willow';
UpText := 'n Willow';
FireLV := '30';
end;
'all':
begin
Tcolour := 1327929;
Tolerance := 10;
Tree := 'All';
UpText := 'hop';
end;
end;
end;

procedure DecidePlayer; //This procedure decides on what tree the script will load depending on WC level.
begin
case (Players[CurrentPlayer].Integers[0]) of
0..14: LoadTrees('Tree');
15..29: LoadTrees('Oak');
30..99: LoadTrees('Willow');
end;
end;

function FindTree(var X, Y: Integer; Click, hover, Time: boolean): Boolean;// Finding the tree with TPA(Crappy TPA).
var
TPA: TPointArray;
ATPA: T2DPointArray;
I, H: Integer;
begin
If not LoggedIn then exit;
FindColorsSpiralTolerance(MSCX, MSCY, TPA, [U]Tcolour[/U], MSX1, MSY1, MSX2, MSY2, [U]Tolerance[/U]);
ATPA := TPAtoATPA(TPA, 55);
if Length(ATPA) = 0 then
Exit;
H := High(ATPA);
for I := 0 to H do
begin
MiddleTPAEx(ATPA[i], X, Y);
if Hover then
MMouse(X, Y, 3, 3);
Wait(50 + Random(50));
if IsUpText([U]UpText[/U]) then
begin
if (Click) then
begin
Mouse(x, y, 0, 0, true);
GetMousePos(X, Y);
Writeln('Found ' + Tree + ' Tree at X: ' + IntToStr(X) + ' Y: ' + IntToStr(Y));
end;
if FlagPresent then
if (FlagDistance > 5) then
begin
FFlag(0);
FindTree(X, y, True, True, True);
end;
Result := True;
Exit;
end;
wait(100+random(100));
end;
end;

Ahh i tried to underline the global varribles used to load the trees but it just turned out as thingo :(.

As you notice this is a an advance procedure above, but the casing is simple, Basicly im feeding the script my woodcut level, which then goes through the case to find the correct range and result for the case, then loads the specific tree which i can cut with my current woodcut level.

Now i have all the woodcutting variables loaded for my FindTree procedure, so now I do not need to make 3 different FindTree functions for each tree as i load the one i want to cut and use the loaded variables in the procedure as a substitute to having all of the tree variables.

This makes your script look much neater and easy to read for the person viewing your script!

Overall About Cases:
Cases really do make your script look much neater and make you have way less procedures in your script also. Cases are essential in order to obtain members unless you do not use any type of these methods in your script.




UNDER CONSTRUCTION - :).

senrath

2009-09-02 16:57:05

Nice, but you should cover using else with cases (ie, when none of the other cases are picked).

BobboHobbo

2009-09-03 08:40:19

From: senrath
Nice, but you should cover using else with cases (ie, when none of the other cases are picked).


Done, anything else i missed out?

ian.

2009-09-03 09:05:09

Well, maybe say that if a case isn't true for any of the options, then it returns 0. That helped me for ShopScreen, which isn't used.. :p


function Wat: Integer;
begin
case GetColor(0, 0) of
12345: Result := 1;
67890: Result := 2;
end;
end;

begin
Writeln(IntToStr(Wat));
end.

senrath

2009-09-03 09:22:24

From: ian.
Well, maybe say that if a case isn't true for any of the options, then it returns 0. That helped me for ShopScreen, which isn't used.. :p


function Wat: Integer;
begin
case GetColor(0, 0) of
12345: Result := 1;
67890: Result := 2;
end;
end;

begin
Writeln(IntToStr(Wat));
end.


That's not part of cases, though. That's just because all integer variables are initialized as 0, and Result is an integer variable.

ian.

2009-09-03 10:58:14

It's still a nice thing to know.. :p

BobboHobbo

2009-09-03 11:05:12

Added an advanced section xD.

Sir R. M8gic1an

2009-09-07 13:34:04

Nice going. Do notice that for you advance cases, a using a type record would be better than using vars ;)

~RM

BobboHobbo

2009-09-10 17:02:43

From: Rasta Magician
Nice going. Do notice that for you advance cases, a using a type record would be better than using vars ;)

~RM


Yes it would, but im just showing an example of how it would be used, and thats to advanced for beginners tutorials xD.

pencilbi

2012-04-20 04:45:21

usefull tut.
i always use 'and' for some detection before. 'case' is look more clear.

xtrapsp

2012-04-20 06:07:08

I like this tut, Seriously. Cases are something easily forgotten but very good to use. I forget the all the time (adding this to favs)

:)

alexes

2012-04-20 07:55:46

:O Awesome tutorial. It helped me so much. I understanded cases but not all. Now i completely understand these! :P

RightClick

2012-04-20 08:18:56

awsome tut =D
case are usefull

Gala

2012-04-20 13:21:21

Ultra grave dig?

NKN

2012-04-20 15:04:08

From: Gala
Ultra grave dig?

It was on his signature, they probably didn't look at the date it was written.

Abu

2012-04-20 15:23:28

From: NKN
It was on his signature, they probably didn't look at the date it was written.


They could have looked at the date before posting… this is years old :cartman:

Rich

2012-04-20 16:09:57

It's a useful tut which could help new members. No harm in bumping it.

Gala

2012-04-20 16:39:25

From forum rules:

Bumping and Grave Digging:
Bumping is unnecessary and unneeded. People will reply in time, so please be patient. Grave Digging is when you reply to a topic that has been inactive for over 3 months and the conversation is obviously dead.


I know it would make sense to bump important tutorials, but one of the beginners job is to search the forum. I probably searched 100 times a days at the beginning. :)

BobboHobbo

2012-04-21 03:15:11

No such thing as grave digging a tutorial, especially if its not outdated.

Gala

2012-04-21 04:51:40

Ok.

Nice tut btw :)

xtrapsp

2012-04-21 11:46:12

Tbh Gala I find the search facility isn't always 100%

plus bumping a tut can't always hurt ;)