PDA

View Full Version : Cases, What are they?



Ashur2Good
03-13-2008, 09:49 AM
Well, many people don't know what cases are. Which is a real shame, cause they are VERY useful.


Contents:

1. Introduction
I: Why should i learn this?
II: What will i learn after this?
III: What is a case?
2. Learning time
I: Basic procedure
II: Different types of cases
III: When to use cases
3. Ending
I: Final comments
II: Good bye's






Introduction




I: Why should i learn this?


Well, cases are a very important part in making good scripts. And everyone wants to make a good script...Right? Well, they are very useful in every way you use them, as you will see after :D



II: What will i learn after this?


At the end of this tutorial, you will learn how to use cases in both ways (Just wait and see!) and how to PROPERLY use them.



III: What is a case?

A case is a simple function that does whatever functions/procedures you tell it to do in order. But, the other way to use a case
is to make it choose a RANDOM function/procedure. Cases are effective in either way.



Learning time!


I: Basic procedure


Here is a basic procedure showing a case being used:


program Casez;
{.include SRL/SRL.scar}


var
x, y, i : integer;

procedure NormalCase;
begin
for i := 0 to 3 do //The i is just a normal integer, nothing special :P(Remember to include in var!)
case i of //with 4, it will only do the things you say to do from 0-3, Not 4!
1: Writeln('Ashurs cases tutorial!'); //You can use 0: as a case as well!
2: Writeln('You will learn how to use cases,');
3: Writeln('In both ways!');

end;
end;


procedure RandomCase;
begin
case random(4) of //Remember how 0 was included, it might randomly pick 0 and not do anything! :o
1: Writeln('Ashurs cases tutorial!');
2: Wait(20);
3: Mouse(x, y, 0, 0, true);
{4: WriteIn('It isnt very hard to do! GL!'); //This procedure will not be used! as it is in 4.
//That is why i have made it a comment :p }
end;
end;


begin
NormalCase;
RandomCase;
end.



Now, let's go into detail shall we.


procedure NormalCase;
begin
for i := 0 to 3 do //The i is just a normal integer, nothing special :P(Remember to include in var!)
case i of //with 4, it will only do the things you say to do from 0-3, Not 4!
1: Writeln('Ashurs cases tutorial!'); //You can use 0: as a case as well!
2: Writeln('You will learn how to use cases,');
3: Writeln('In both ways!');

end;
end;


Now, with a case we put it IN the procedure obviously :P
Next is the begin for the procedure. Now we are up to the case bit.
"case" will start the case bit. Then we will tell it that it will do everything from 1-3. Wow, now you're starting to get it, aren't you? :P
Then we write "of" :p. After that we start of with what we want it to do.
First thing is we want it to Writeln, then it will do all of those from 1-3. Simple? Yes!
If anyone has any questions about this part, just ask. ;)

Now, the next procedure is a RANDOM case, it do the same thing as the other procedure,
except RANDOMLY choose thing to do from the list of whatever you assign it to do. Now, the (4) Will be how many things you want it to go thru. BUT, the thing with that is for e.g.
If i choose (4) it will do a random thing i tell it from 0-3, NOT 4. So if it was 5 then it would be 0-5. Get it? Good.
So, if i choose "case RANDOM (2) of....0: Writein... 1: Mouse...."
It will pick a random one, so it will either Writeln one message, or the other message! But remember, it's all chance(random)!




II: Different types of cases

There are two diffrent types of cases (As mentioned before) Order or Random ones.




Here is a Order case:

procedure NormalCase;
begin
case (4) of //with 4, it will only do the things you say to do from 0-3, Not 4!
1: Writeln('Ashurs cases tutorial!'); //You can use 0: as a case as well!
2: Writeln('You will learn how to use cases,');
3: Writeln('In both ways!');
end;


And here is a random case:

procedure RandomCase;
begin
case random(4) of //Remember how 0 was included, it might randomly pick 0 and not do anything! :o
1: Writeln('Ashurs cases tutorial!');
2: Wait(20);
3: Mouse(x, y, 0, 0, true);
{4: WriteIn('It isnt very hard to do! GL!'); //This procedure will not be used! as it is in 4.
//That is why i have made it a comment :p }
end;
end;




III: When to use cases



You can use cases when you have a long list of things to do, OR when you need SCAR to randomly pick a function out of whatever choices you choose.




Ending


I: Final comments



Please reply with any rants/compliments and most importantly, Improvments!



II: Good Byes


Thanks for reading this, i hope your journey with SCAR improves from here, and i wish you luck on your ride.

P.S: I also attached the simple case script. For anyone that wants it :P


Cheers
-Ashur

Bonfield
03-13-2008, 10:24 AM
good tut really explains alot, its clear and concise thanks for this

also welcome back i havent seen u around here in a while

ZephyrsFury
03-13-2008, 10:48 AM
'case (4) of' doesn't do anything in your example because nothing has a case number of 4.

EDIT: From what I understand do you mean:


for i := 0 to 3 do
case i of
1: Writeln('blah');
2: Writeln('blah2');
3: Writeln('Blah3');
end;

?

Ashur2Good
03-13-2008, 11:25 AM
'case (4) of' doesn't do anything in your example because nothing has a case number of 4.

EDIT: From what I understand do you mean:


for i := 0 to 3 do
case i of
1: Writeln('blah');
2: Writeln('blah2');
3: Writeln('Blah3');
end;

?


Well, you are right about one thing, but wrong about the other my friend :P

As i explained in the tutorial(By my knowledge) if it's a case of (4) it will NOT do 4, only everything up to 4 but for randoms. Get the problem, but it's half my fault too :(

But i think i would of got a lot more replys about that so lucky i fixed it now :D

But you are right about the other bit, so i'll fix it now :D

@BonField: Lol thx :P And yeah, i'm starting to get back active here :D


Cheers
-Ashur

Negaal
03-13-2008, 01:12 PM
Well, you might want to standarize your examples? also mention that case needs an end;, you forgot 1 end; in 1 of your examples.

Also you do something like random(4) or for i:= 0 to 3 do, but use case 1, 2 and 3, they both should be 0, 1, 2 and 3.

Hope I was correct and you will correct your tutorial a bit.

gerauchert
03-13-2008, 02:03 PM
maybe show examples of cases with strings and booleans like:


case Correct of
True: Writeln('Its True');
False: Writeln('Its False');
end;

// or with strings

case Something of
'tree': Writeln('Its a tree');
'lamp': Writeln('Its a lamp');
'potion': Writeln('Its a potion');
end;


:D

Ashur2Good
03-13-2008, 02:14 PM
maybe show examples of cases with strings and booleans like:


case Correct of
True: Writeln('Its True');
False: Writeln('Its False');
end;

// or with strings

case Something of
'tree': Writeln('Its a tree');
'lamp': Writeln('Its a lamp');
'potion': Writeln('Its a potion');
end;


:D


I was planning to acctually :P I might do that after though :spot:

But i wanna see if people can understand this part good first :eek:

-Ashur :eek: (lol funny smily :P) :eek:

ZephyrsFury
03-14-2008, 08:12 AM
As i explained in the tutorial(By my knowledge) if it's a case of (4) it will NOT do 4, only everything up to 4 but for randoms. Get the problem, but it's half my fault too :(



When I run your example it doesn't display anything.

Ashur2Good
03-14-2008, 09:08 AM
When I run your example it doesn't display anything.


Yeah theres a mistake in that example lol :S I'll update the correct one soon.

Sorry to anyone that got affected
-Ashur

iamcool04
08-01-2008, 02:48 AM
awesome tut, this will make my scripting a thousand times better

Argentinian
08-09-2008, 03:14 PM
Thanks for the tut, it helped me a lot.

WhoCares357
08-12-2008, 06:46 AM
Nice tutorial. You should also show some uses with strings.

Edit: Didn't see that one thread that suggested it before :D. Oh well, the suggestion stands.

shynie
10-27-2008, 07:55 AM
Wow, thanks a lot for this! This really helped me :)