PDA

View Full Version : Guide on the Many Statements in Simba!



Nava2
08-06-2008, 04:13 AM
Guide on the Many Statements in Simba!
For Beginners to Intermediates.

To understand this tutorial, you need to understand English, decently, as well as have some simple Simba scripting knowledge and some heavier for the later examples.

2. Table of Contents:

This is a large tutorial. To find something specific please use Ctrl + F or the Search Tutorial button at the top.


Introduction
Table of Contents
Simba Basics
Conditions
General Rules
Boolean Variables
Integer Checks
String Checks
If..Then..Else Statements
Breakdown of the Statement
Examples
Cases
Breakdown of the Statement
Uses
Why use a case?
Examples
Loops
Different Types of Loops
Repeat..Until Loops
Breakdown of the Statement
Examples
While..do Loops
Breakdown of the Statement
Examples
For..to..do Loops
Breakdown of the Statement
Examples

Conclusion
Useful Links
Acknowledgements


3. Simba Basics:

Well, I said you needed to know some basics, here is what I will be using and won't be taught. You need to know what everything listed is:

begin
end;

var

Integer
String
Boolean
Extended

//Symbols:
= + - > < <> , ;
If anything listed above is confusing to you, please read other tutorials before continuing with this one.

4. Conditions:

In this tutorial, I will use three different conditions which always are Booleans. They are:
Boolean variables
Integer checks
String checks

General Rules:

If you use a condition which is a combination of several conditions, it is much better practice to put the more Computer Intensive condition first in the series. This is due to a system known as short-circuit evaluation (http://en.wikipedia.org/wiki/Short-circuit_evaluation). A series of conditions is something like:
(FindColor(x, y, 65536) or (GetColor(x, y) = 0) and (z = 10))

Notice how I placed the most Computer Intensive function first, FindColor, then I placed GetColor and finally an integer check (more on these later).

Boolean Variables:

These are very easy to understand. This is because any condition is actually a boolean, so when you use an actual boolean variable, it just eliminates the thinking. Although, this can be harder. When using a function like IsUpText(Text: string): boolean you need to treat it as a variable. It sets itself as true or false and Simba will read it as a variable.

Integer Checks:

A harder boolean ;). Integer checks are math, they use things like =, >, <, and <>. Any math function is either true or false. Something like: 1 + 1 = 2, would be true; but 1 + 1 = 3 would be false. Using this thinking we can set up conditions to create true and false answers.

String Checks:

String checks are even harder conditions. They use things like = and <> but they also have many other harder parts.

With strings, one must remember that they ARE case-sensitive when used as a condition. There are some very handy built-in Simba functions to help us beat this problem. String functions are listed on this page: http://docs.villavu.com/simba/scriptref/string.html. The documentation is fairly incomplete, please feel free to contribute or ask questions.

Several of the functions are self explanatory. The most used are: Lowercase, Uppercase, and Pos. Lowercase and Uppercase simply make it so that the scripter does not have to worry about Case changes in the string. Pos is a very helpful text function, although sometimes hard to understand.

Pos searches in the inputted string for another string. If it finds it, it returns the number of characters from the left which the string is found inside the string, 0 being not found at all. This is helpful in using if..then..else statements because it easily can tell you whether a string is in another and can be called like so:
if (Pos(str, str2) <> 0) then

If you need more explanation on the previous, please, feel free to ask. I apologize for the brevity.

5. If..Then..Else Statements:

If..Then..Else statements are the core to any script. They are the building block of a good fail-safe-filled script. They, simply put, tell the script to check if one thing occurs, if it does then preform this action, if it does not, then preform a different one. The actual condition of the statement must return a boolean. Now, any integer check can return a boolean, as well as any boolean-returning function. As this is an easier statement to understand, I will directly move into the examples.

Breakdown of the Statement:

IF (Condition) THEN
begin

Action1
end ELSE
begin

Action2;
end;

IF: The beginning…
(Condition): The boolean condition which declares whether Action1 is run.
THEN: then Final or Middle part of this statement.
Action1: The Action which is completed if the condition is as the user set it.
ELSE: Final part, used if scripter would like to use an alternative to Action1 aka Action2.
Action2: Self explanatory.

Examples:

Using different types of conditions, all of which are still booleans.
procedure IfThenElseStatements;
var
i : integer;
str : string;
Bool : boolean;
begin
i := 5;
if (i = 5) then
begin
Writeln('i = 5!');
end else
begin
Writeln('i <> 5');
end;

Bool := false;
if not(bool = false) then
Writeln('bool is true!')
else
Writeln('bool is false!');
str := 'I like big buttox!';
if (str = 'I like big buttox!') then
Writeln('He likes big buttox!')
else
Writeln('He doesn''t like big buttox.');
end;

Now, there was a lot thrown into that. Lets break down these examples.

After every If..Then you must have a begin, IF there is more than one line to the action which you are doing. So although this is correct:
if (i = 5) then
begin
Writeln('i = 5!');
end else
begin
Writeln('i <> 5');
end;
This is better because it uses up fewer lines making it a smaller file size as well as easier to read:
if (i = 5) then
Writeln('i = 5!')
else
Writeln('i <> 5');
NOTE: You must remember to not put a ; after the first line if you are including an else part, and you must put one after the last part of your If..Then..Else statement.

6. Cases:

After a length explaination of If..Then..Else statements, here is a much smaller read, but even more useful statement in Simba. Cases are used to replace things like this long and confusing code:
begin
if Lowercase(Str) = 'apple' then
Writeln('apple');
if Lowercase(Str) = 'orange' then
Writeln('orange');
if Lowercase(Str) = 'pear' then
Writeln('pear');
if Lowercase(Str) = 'grape' then
Writeln('grape');
if Lowercase(Str) = 'banana' then
Writeln('banana');
if Lowercase(Str) = 'cumquat' then
Writeln('cumquat');
if Lowercase(Str) = 'blueberry' then
Writeln('blueberry');
end;
With a much easier and simpler to read:
begin
case Lowercase(str) of
'apple': Writeln('apple');
'orange': begin
Writeln('orange');
end;
'pear': Writeln('pear');
'grape': Writeln('grape');
'cumquat': Writeln('cumquat');
'banana': Writeln('banana')
'blueberry': Writeln('blueberry');
else
Writeln('Not one of the regular fruits! :)');
end;
end;
Cool right? Super hard? No, not really!

Note: the latter case could be replaced with: WriteLn(Lowercase(str));

Breakdown of Statement:

CASE (Condition) OF

Option1:

Action1;
Option2:

Action2;
ELSE

Action3;
END;

CASE: Declares the statement.
(Condition): The boolean condition which declares whether an Action is run.
OF: Finishes declaration of statement.
Option: Options which the case can be true for.
ELSE: Failsafe, completes action after it if none of the Options are met.
END;: Ends the entire statement.

Why Use a Case?

Frankly, the answer is simple, clean code and versatility. Cases allow you to place many options in fewer lines with more options to use! In our example, we had seven different fruits, and if it wasn’t any of them, the if statements would do NO action. With the case statement, not only does it look nicer, easier to read, it gave us an ELSE option. This ELSE option allows the function to do an action if the condition isn’t true with ANY of the options in the case. This is very useful.

Examples:

Here is an example of a Random Integer case which is the most common case it seems used in anti-ban procedures.
procedure RandomIntegerCase;

begin
case Random(10) of
0: Writeln('Hi');
1: Writeln('G''bye.');
2, 3, 4, 5: Writeln('Nava2 is a cool cat!');
6..9: Writeln('I''m a poopy poop! ;)');
end;
end;

Okay, easy enough. There are a few new things in there. One, its with integers which are no different. Two, it uses ‘,’s and ‘..’s! Both of these are very useful. Now, with using ,’s you can tell a script to do the same thing if two different answers come up. The ‘..’ is the same thing, only instead of writing out every number in between the first and second numbers, you can use the ‘..’ for it. So, 6..9 is really 6, 7, 8, 9! Also, although rarely useful, the .. can be used in such things as A..z meaning from Capital A to lowercase z. In other words, any character (remember characters are just smaller integers)! :)

For more information or different information please check the Useful Links Section.

7. Loops:

Loops are a key part of any script written for RuneScape as well as any for another program. In Simba, we have several different types of loops they are:
repeat..until
while..do
for..to..do

Repeat loops are the simplest kind of loops, and in being so, are considered rather simple to use as well as less useful. (Sorry if that was redundant) While..Do loops are the same as repeat loops, only set up differently. For..To..Do loops are extremely useful for both simple functions, to advanced functions.

It is important to remember, that with ALL LOOPS, they will continue till they are done if they are not told to stop. That is why they are called Loops, they repeat.

Control Commands:

While we are in a loop, we can use several Loop Specific commands. They are:
Continue;
Break;
Exit;

First off, Continue;. Continue is a relatively useful command, when you need it. When called, it will force a loop to return to the beginning of the loop and continue through.

Secondly, Break;. Break; is the most useful Loop command. It, as you might think, breaks out of the loop. When called it will stop the loop, not do anything else in the loop, and continue with the script.

Lastly, Exit;. Exit; is not Loop Specific, but I mention it here so people are not confused. Exit; will stop the ENTIRE function where it is and move on in the script. It will exit the loop and the running function itself.

Repeat..Until Loops:


Breakdown of Statement:

REPEAT

Action;
UNTIL (Conditions);

REPEAT: The beginning of the loop
Action: The action(s) to be completed every time the loop is repeated.
UNTIL: The end of the loop
(Conditions): The conditions which will end a loop when met at the end of running the Action

The Repeat..Until loop is the most common loop used in Simba scripting. It lets a loop progress until the conditions are met.

Here is an example of a Repeat..Until Loop:
a := Seconds * 100;
repeat
Wait(a);
Inc(b);
until (b = 10);

In the example, you can see the script waits for a certain amount of time and each time it Increases the variable b. When b is 10, the script will stop the loop and continue through the script.

Note: Inc(var x: integer); is a faster way of doing x := x + 1.

While..Do Loops:

Breakdown of the Statement:

WHILE Negative(Condition) DO
begin

Action;
end;

WHILE: Begins statement.
(Condition): This is just like any other condition but it must be NEGATIVE to what you want. So, if you want the loop to break when the condition is true you would put: not(true).
DO: Ends statement.
Action: action to do while the condition is still true.

The While..Do loop could be considered more advanced thatn that of the Repeat..Until. It is far from necessary in scripts, but can be useful. The biggest difference in the loops is that it checks that the condition at the beginning of Loop is still true as compared to at the end of the loop. Small difference, but it means that if the condition isn’t true when the loop is first called, then the loop won’t run at all. (Thanks Metagen for clearing a bit of that up for me)

Example:

begin
while (z <> 10) do
Inc(z);
end;

This just shows the negative at work in the condition. You must remember, with a while statement, if you are doing more than one action after the statement, you need a begin and an end.

For..To..Do Loops:

For..To..Do Loops are a little more complicated than the previous. The For..To..Do loop increases the inputted variable by one each time at the beginning of the loop. They are extremely useful when using arrays, as well as when just counting how many loops to run though. WARNING: THERE ARE SOME ADVANCED EXAMPLES HERE.

Breakdown of the Statement:

FOR (Start Integer/Var Assignment) TO (Final Integer) DO
begin

Action;
end;

FOR: Begins statement
(Start Integer/Var Assignment): Integer to start counting from, must be a variable assigned. Example:
i := 0
TO: Statement Part saying the loop will count upwards, can be changed to DownTo, which will count downwards.
(Final Integer): When your variable assigned in the start integer reaches this number, the loop will continue on but at the end of the actions the loop will end.
DO: End of statement.
Action;: Action to complete on each cycle through the loop.

The For..To..Do statement is the most useful loop in my opinion. It is used in every TPA function as well as in general other functions. For this section, I’m going to show examples and explain them.

Examples:

for i := 0 to 10 do
begin
WriteLn(IntToStr(i));
if (GetColor(x, y) = 0) then
Break;
end;

Here, I have had the script WriteLn the variable i every time the loop repeats. Although the if statement is probably useless, it will check if (x, y) is black, if it is, then the loop will break. Remember, with for..to..do statements, the variable is increased by one each time. The equivalent repeat..until loop is the following:
i := 0;
repeat
Writeln(IntToStr(i));
Inc(i);
if (GetColor(x, y) = 0) then
Break;
Until (i = 10);

The for..to..do statement works the same as the previous function, but faster and cleaner.

Now for a more complex example, a TPA function! :D.
Function TreeDown: Boolean;
var
colors1 : TPointArray;
colors2 : TIntegerArray;
begin
if Not LoggedIn then Exit;
Wait(RandomRange(50, 650));
FindColorsSpiralTolerance(x, y, colors1, treecolor, MSx1, MSy1, MSx2, MSy2, 10);

for i := 0 to High(Colors1) do
Colors2[i] := GetColor(Colors1[i].x, Colors1[i].y);

repeat
Wait(100+random(25));
for i := 0 to High(Colors1) do
if (not(Colors2[i] = GetColor(Colors1[i].x, Colors1[i].y)) )then
begin
result := true;
break;
end;
until result;
if (Result) then WriteLn('Tree down.');
end;

This was a function I wrote for IllKillTill; for his new woodcutter. Now, I am unsure if it works, but it is simple and a good example of loops. Now, assuming that you have knowledge of arrays, you can see that the For..To..Do loop runs through each piece of the array Colors2 getting the color with its corresponding TPoint. Using a loop is a lot faster than doing Colors2[0] := GetColor(Colors1[0].x, Colors1[0].y); and then rewriting it for each number. Would take ages to write out as well, it uses mass amounts of code.

8. Conclusion:

Well, this has been my longest and most intensive guide. I hope that you learned a lot about a lot of things or a lot about a few things. Either way, please feel free to message me on IRC (navatwo), post here, or message me via PM if you have further questions! :)

9. Useful Links:

Note: May be outdated.

Great Beginners Guide to SCAR:
A brief tutorial on scar. ~ by i pro leechin’ (http://www.villavu.com/forum/showthread.php?t=32990)
Note: May be outdated, most logic will apply to Simba, as well.

A Guide I use just too much:
A brief lesson on fixing annoying errors :P ~ by JAD (http://www.villavu.com/forum/showthread.php?t=6413)

A less in-depth guide on a similar topic:
A lesson on If-then's, For, While, and Cases! ~ by The[Cheese] (http://www.villavu.com/forum/showthread.php?t=30844)


10. Acknowledgements:

Bullzeye95: Old Unofficial editor of my scripts/anything I write for Simba scripting. ;)
Metagen: Firmed up some facts!

Thanks for reading!

Timer
08-18-2008, 01:44 AM
Holey ****!

Nice tut!

P1nky
08-18-2008, 01:50 AM
Great tut! for beginners!

Nava2
08-18-2008, 07:02 AM
Any suggestions?

nabi34
08-18-2008, 12:13 PM
great tuto

lordsaturn
08-19-2008, 02:53 AM
Add math and boolean statements please.

e.g. Mod, Xor, etc.


Great tut by the way. :)

Nava2
08-19-2008, 03:47 AM
Add math and boolean statements please.

e.g. Mod, Xor, etc.


Great tut by the way. :)

I mentioned booleans, everything I mentioned revolves around them. But, I think I might add in the math part. Although, I did say that it is assumed you know the basic maths.

Also, Xor is hard to explain as a beginner.

Flame
08-19-2008, 05:36 AM
Thanks for your hardworking , It's great one ^^

lordsaturn
08-19-2008, 05:41 AM
I mentioned booleans, everything I mentioned revolves around them. But, I think I might add in the math part. Although, I did say that it is assumed you know the basic maths.

Also, Xor is hard to explain as a beginner.


Just a suggestion, because you asked for them. :p

markuska815
08-19-2008, 05:43 AM
Great beginner tut... detailed but simple :)

Nava2
08-19-2008, 02:54 PM
Just a suggestion, because you asked for them. :p

I didn't mean to shoot it down. I will add that actually, I just need to get up the motive.

tarajunky
08-19-2008, 04:21 PM
Small typo.

Bool := false;
if not(bool = false) then
Writeln('bool is true!')
else
Writeln('bool is true!');

Naum
08-19-2008, 04:27 PM
Xor isn't that hard it will check if one side of the statement is true e.g.

run = true Xor run1 = false; xor results true;
run = false Xor run1 = true; xor results true;
run = true Xor run1 = true; xor results false //as no side is 'true'
run = false Xor run1 = false; xor results false //as no side is 'true'

But anyway good job.

Nava2
08-20-2008, 12:55 PM
Xor isn't that hard it will check if one side of the statement is true e.g.

run = true Xor run1 = false; xor results true;
run = false Xor run1 = true; xor results true;
run = true Xor run1 = true; xor results false //as no side is 'true'
run = false Xor run1 = false; xor results false //as no side is 'true'

But anyway good job.

Your code there is confusing. But, all it does is make sure that ONLY one of the specified conditions is true, correct?


Small typo.

Bool := false;
if not(bool = false) then
Writeln('bool is true!')
else
Writeln('bool is true!');

Whoops, is it obvious I copy and pasted a bit? :D

Naum
08-20-2008, 04:41 PM
Yes thats all it does, It's used in 'SetRun' which is pretty efficient :).

Nava2
08-21-2008, 05:49 AM
Yes thats all it does, It's used in 'SetRun' which is pretty efficient :).

Hmm, I might do a tutorial on math in scar. SIMPLE math. :P. I'm no good with trig functions, and when to apply them. I know trig well, just not how it gets applied here.

Firebrand
08-21-2008, 05:54 AM
A really handy one I use all the time is forwarding, Had a quick scan through but didn't see it in there :D

Otherwise that's really good, when I first started I had trouble with all the Pascal statements.

mastaraymond
08-22-2008, 01:15 AM
Meh.. Xor isn't really necessary to know o.O

Nava2
08-24-2008, 05:16 AM
Meh.. Xor isn't really necessary to know o.O

Yeah, honestly, I have never used it :). It just kinda makes a function longer, IMO, I haven't found a really good use for it.

Naum
08-24-2008, 09:51 AM
Yeah, honestly, I have never used it :). It just kinda makes a function longer, IMO, I haven't found a really good use for it.


Meh.. I learned it from Raymond, just nice to know.

JuKKa
08-24-2008, 10:08 AM
I even have xor in a science book and still dont know what it means.

ShowerThoughts
08-24-2008, 10:17 AM
Xor returns true if the 2 booleans are different.

Like
True xor False = true
true xor true = false
false xor true = true
false xor false = false

Nava2
08-24-2008, 06:15 PM
Yes, now, how would you go about using that for three different things etc. There is a way.

Nava2
06-25-2009, 12:09 PM
So.. when did this get stickied.. :<

ZephyrsFury
06-25-2009, 09:42 PM
So.. when did this get stickied.. :<

When we started moving the tuts around probably... Why the sad face? Do you want it unstickied?

Nava2
06-25-2009, 10:47 PM
When we started moving the tuts around probably... Why the sad face? Do you want it unstickied?

No, sad face was because I didn't know. :(

Laimonas171
08-03-2009, 10:22 AM
I'm starting SCAR, so this one will be bookmarked. rep++

Yush Dragon
08-13-2010, 12:13 AM
NICE, gotta check it out tomorow :D
it seems i can learn lots of it , Thanks bro! ^^ :D

+rep

mrpickle
08-13-2010, 03:29 AM
This was exactly what I was looking for when I first started scripting. :)

wundertüte
03-12-2011, 10:57 AM
thank you for this guide - i have learned how to work with points :=)

Nava2
11-07-2011, 05:31 AM
Out of curiousity, why is this Outdated? It's all still valid.. :)

E: I see why it was outdated, it is not anymore!

Daniel
07-24-2012, 08:38 AM
4. Conditions:

... it is much better practice to put the more Computer Intensive condition first in the series. This is due to a system known as short-circuit evaluation (http://en.wikipedia.org/wiki/Short-circuit_evaluation). A series of conditions is something like:
(FindColor(x, y, 65536) or (GetColor(x, y) = 0) and (z = 10))

Notice how I placed the most Computer Intensive function first, FindColor, then I placed GetColor and finally an integer check (more on these later).

Nit-picking here. Shouldn't it be the other way around? ;)

NCDS
07-24-2012, 07:07 PM
Nit-picking here. Shouldn't it be the other way around? ;)

The keyword "Continue;' also skips the the end of a loop, rather than the beginning as explained here, but no need to be sticklers. ;)

nava<3

litoris
07-24-2012, 07:14 PM
Nit-picking here. Shouldn't it be the other way around? ;)

That's pretty much common sense, so anyone that codes well enough to consider something like this will put the thing that is easiest to compute first.