1. Learn to use Cases!

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:
SCAR Code:
`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:
SCAR Code:
`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:

SCAR Code:
`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:
SCAR Code:
`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:
SCAR Code:
`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:
SCAR Code:
`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.

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.

SCAR Code:
`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 [ u] 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!

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

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

3. Originally Posted by 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?

4. Banned
Join Date
Dec 2008
Posts
2,829
Mentioned
0 Post(s)
Quoted
0 Post(s)
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..

SCAR Code:
`function Wat: Integer;begin  case GetColor(0, 0) of    12345: Result := 1;    67890: Result := 2;  end;end;begin  Writeln(IntToStr(Wat));end.`

5. Originally Posted by 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..

SCAR Code:
`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.

6. Banned
Join Date
Dec 2008
Posts
2,829
Mentioned
0 Post(s)
Quoted
0 Post(s)
It's still a nice thing to know..

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

~RM

9. Originally Posted by 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.

10. Registered User
Join Date
Mar 2012
Location
Shenzhen China
Posts
19
Mentioned
0 Post(s)
Quoted
0 Post(s)
usefull tut.
i always use 'and' for some detection before. 'case' is look more clear.

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

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

13. Registered User
Join Date
Jan 2012
Posts
297
Mentioned
0 Post(s)
Quoted
0 Post(s)
awsome tut =D
case are usefull

14. Ultra grave dig?

15. Originally Posted by Gala
Ultra grave dig?
It was on his signature, they probably didn't look at the date it was written.

16. Originally Posted by 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

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

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

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

20. Ok.

Nice tut btw

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

plus bumping a tut can't always hurt