Results 1 to 2 of 2

Thread: [Tut] Cases

  1. #1
    Join Date
    Jul 2009
    Posts
    421
    Mentioned
    0 Post(s)
    Quoted
    0 Post(s)

    Default [Tut] Cases

    Using Cases
    Written by NeehoSoft(Donnie)
    Info:
    Cases are a requirement for applying for members, and clean up your script a lot and are very useful. In this tutorial, i will be showing you how
    to use them and when to use them.
    What Are Cases?
    Cases are like If - Then Statements on Crack. They are way faster and cleaner, but do the same thing. If you have ever used java, they are the same as switches.
    Difference between If Then Statements and Cases
    This is (compiling) code in If then that checks the users Fishing level and display the fish to fish as the variable Fish2Fish:
    SCAR Code:
    procedure What2Fish;
    begin
      if ((GetSkillLevel('Fishing')) < 14) and
         ((GetSkillLevel('Fishing')) >= 1) then
         Fish2Fish := Shrimp;
      if ((GetSkillLevel('Fishing')) < 39) and
        ((GetSkillLevel('Fishing')) >= 15) then
        Fish2Fish := Tuna;
     if ((GetSkillLevel('Fishing')) <75) and
        ((GetSkillLevel('Fishing')) >=40) then
        Fish2Fish := Lobster;
      if ((GetSkillLevel('Fishing')) < 99) and
        ((GetSkillLevel('Fishing')) >= 76) then
        Fish2Fish:= Shark;
    end;
    The code above is dirty and bad standards. Look at this code which does the same thing, but is cleaner (using cases):
    SCAR Code:
    procedure What2Fish;
    begin
      Case (GetSkillLevel('Fishing')) of
        1..19: Fish2Fish := Shrimp;
        20..39: Fish2Fish := Tuna;
        40..99: Fish2Fish := Lobster;
        76..99  Fish2Fish := Shark;
      end;
    end;
    This looks much cleaner, right?
    Notice the 1..19 - Not all cases have this, but this is cleaner than having like, 1: Fish2Fish := Shrimp 2:Fish2Fish...ext ext.

    Also, Cases don't always have to be integers. They can also be for boolean's and strings.

    Lets break down the format of a case. This will be a String Case: (Non compiling example, GetSkillLevel Returns as a integer.
    SCAR Code:
    procedure What2Fish; //Begins the procedure What2Fish
    begin
      Case (GetSkillLevel('woodcutting')) of
        'normal': Writeln('Lets Cut Normal Trees');
        'oak': Writeln('Lets Cut Oaks');
        'willow': Writeln('Lets Cut Willows');
      end;
    end;
    Nothing new here, lets look at:
    Case(GetSkillLevel('woodcutting')) of
    This starts the case (similair to 'Begin') and gets the skill level of woodcutting, and of tells it what to do with each specific level it could be.
    And then the format for a case:
    SCAR Code:
    If it returns this: Do this

    FailSafeing Cases
    Cases can be failsafed, like the "Defualt" in java's switch statements.
    To do so add a "Else" to the end of the case i.e:
    SCAR Code:
    procedure What2Fish;
    begin
      Case (GetSkillLevel('Fishing')) of
        1..19: 2Fish := Shrimp;
        20..39: 2Fish := Tuna;
        40..99: 2Fish := Lobsters;
      else
       //FailSafe could be called here :)
    end;

    Random Cases
    Random cases have a chance of skipping a case and pulls one out of a random time. For example, in, lets say a flax picker it could pick a random path, like...
    SCAR Code:
    procedure PickARandomPath;
    begin
      case random(11) of //11 = 100% - Picks one at random.
        1: PathOne;
        2: PathTwo;
        2: PathThree;
      end;
    end;

    So, here is a full script example (compiling) where the user inputs there woodcutting level and it returns what tree to cut:
    SCAR Code:
    Program WhatTreeShouldICut; //Notice the CamelCapping vs whattree
    const
    WhatsMyWoodCuttingLevel = 'Put your wc level here';
    Begin
    Case (WhatsMyWoodCuttingLevel) of
    1..14 : Writeln('You Should Cut Normal Trees!');
    15..29 : Writeln('You Should Cut Oaks!');
    30..99 : Writeln('You Should Cut Willows!');
    Else
    Writeln('You forgot to input your woodcutting level!');
    TerminateScript;
    end;
    Begin
    WhatTreeShouldICut;
    End.

    Well, that's my tutorial.
    Feel free to post criticism, thanks, and suggestions. (R1Ch, i was not talking about you.).
    Note To WhoEver repped me:
    Thanks, im 1pt from my second block
    And thanks for saying the whole else and stuff was right, all my tuts were from memory.
    Last edited by Neehosoft; 10-09-2009 at 05:30 PM.
    Quote Originally Posted by Cstrike View Post
    Why do I even try these things? I just shit my pants over this god damn tutorial. Fuck, that's uncleanable. I can't even wash that out because there's so much of my shit it will just stain everything else. If I put it in the washing machine, I'm sure to stain the sides.

  2. #2
    Join Date
    May 2007
    Location
    England
    Posts
    4,140
    Mentioned
    11 Post(s)
    Quoted
    266 Post(s)

    Default

    How very kind of you. I was going to post an error out, but after reading that line, I won't.
    <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.

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
  •