Results 1 to 21 of 21

Thread: Learn to use Cases!

  1. #1
    Join Date
    May 2007
    Location
    NSW, Australia
    Posts
    2,823
    Mentioned
    3 Post(s)
    Quoted
    25 Post(s)

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

    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.

    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!

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

  2. #2
    Join Date
    Oct 2006
    Location
    ithurtsithurtsithurtsithurts
    Posts
    2,930
    Mentioned
    7 Post(s)
    Quoted
    135 Post(s)

    Default

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

  3. #3
    Join Date
    May 2007
    Location
    NSW, Australia
    Posts
    2,823
    Mentioned
    3 Post(s)
    Quoted
    25 Post(s)

    Default

    Quote Originally Posted by senrath View Post
    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. #4
    Join Date
    Dec 2008
    Posts
    2,813
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    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. #5
    Join Date
    Oct 2006
    Location
    ithurtsithurtsithurtsithurts
    Posts
    2,930
    Mentioned
    7 Post(s)
    Quoted
    135 Post(s)

    Default

    Quote Originally Posted by ian. View Post
    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. #6
    Join Date
    Dec 2008
    Posts
    2,813
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    It's still a nice thing to know..

  7. #7
    Join Date
    May 2007
    Location
    NSW, Australia
    Posts
    2,823
    Mentioned
    3 Post(s)
    Quoted
    25 Post(s)

  8. #8
    Join Date
    Jul 2007
    Location
    Right now? Chair.
    Posts
    8,488
    Mentioned
    3 Post(s)
    Quoted
    12 Post(s)

    Default

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

    ~RM

    I & I know Zion. It is in the spirit, body and mind of every one of us
    RMouse(obj: TMSIObject): boolean;

  9. #9
    Join Date
    May 2007
    Location
    NSW, Australia
    Posts
    2,823
    Mentioned
    3 Post(s)
    Quoted
    25 Post(s)

    Default

    Quote Originally Posted by Rasta Magician View Post
    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. #10
    Join Date
    Mar 2012
    Location
    Shenzhen China
    Posts
    19
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

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

  11. #11
    Join Date
    Feb 2012
    Location
    DON'T PM ME ASKING FOR STUFF
    Posts
    2,170
    Mentioned
    38 Post(s)
    Quoted
    423 Post(s)

    Default

    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. #12
    Join Date
    Oct 2011
    Posts
    434
    Mentioned
    0 Post(s)
    Quoted
    8 Post(s)

    Default

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

  13. #13
    Join Date
    Jan 2012
    Posts
    296
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default

    awsome tut =D
    case are usefull
    Sorry for my bad english, i'm french

  14. #14
    Join Date
    Feb 2007
    Location
    Switzerland
    Posts
    583
    Mentioned
    1 Post(s)
    Quoted
    50 Post(s)

    Default

    Ultra grave dig?

  15. #15
    Join Date
    Mar 2012
    Location
    127.0.0.1
    Posts
    3,383
    Mentioned
    95 Post(s)
    Quoted
    717 Post(s)

    Default

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

  16. #16
    Join Date
    Feb 2012
    Location
    Somewhere, over the rainbow...
    Posts
    2,272
    Mentioned
    3 Post(s)
    Quoted
    45 Post(s)

    Default

    Quote Originally Posted by NKN View Post
    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. #17
    Join Date
    May 2007
    Location
    England
    Posts
    4,140
    Mentioned
    11 Post(s)
    Quoted
    266 Post(s)

    Default

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

    Quote Originally Posted by Eminem
    I don't care if you're black, white, straight, bisexual, gay, lesbian, short, tall, fat, skinny, rich or poor. If you're nice to me, I'll be nice to you. Simple as that.

  18. #18
    Join Date
    Feb 2007
    Location
    Switzerland
    Posts
    583
    Mentioned
    1 Post(s)
    Quoted
    50 Post(s)

    Default

    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. #19
    Join Date
    May 2007
    Location
    NSW, Australia
    Posts
    2,823
    Mentioned
    3 Post(s)
    Quoted
    25 Post(s)

    Default

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

  20. #20
    Join Date
    Feb 2007
    Location
    Switzerland
    Posts
    583
    Mentioned
    1 Post(s)
    Quoted
    50 Post(s)

    Default

    Ok.

    Nice tut btw

  21. #21
    Join Date
    Feb 2012
    Location
    DON'T PM ME ASKING FOR STUFF
    Posts
    2,170
    Mentioned
    38 Post(s)
    Quoted
    423 Post(s)

    Default

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

    plus bumping a tut can't always hurt

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •