SRL Archive - OSR Beginner Scripting Tutorials

Programming level: Pascal - Medium!


Rezozo

2012-09-29 02:51:24

Welcome to my second TuT, a sequel to my first!

*Note - This is a continuation of my previous thread: http://villavu.com/forum/showthread.php?p=1106349 , If you have not been through that, please do so now.

We will now be covering the topic of:

Using different types of variables.
Making and Using arrays.
Making and using a loop.

1) Using different types of variables:

If you remember, we already discussed how to output a string variable. Now lets move on to the numbers.

When outputting a number, such as an integer or real number, you use this:
program TuT;
Var
x:integer;
begin
x:= 5;
Writeln(x);
end.

Now notice how I don't use the '' for a number. It is only necessary for a char or string.

Declaring a real doesn't work in Simba, and Char is in the same format as with String.

2) Declaring and using an array -
First of all, what is an array. It is a variable that sets a value in sectors/dimensions. Say for example, you have a 1X1 array. That holds only 1 value. But a 5X1 array will hold a list of values. Now consider a 5X5 array. This goes 5 in one way, 5 in another. Like two dimensions. Same way a 5X3X8 array is a three dimensional array.
Lets declare an array. The normal setup is:
program TuT;
Var
Arr : Array[0..4] of Integer;
begin
end.
That is declaring Arr as an array, carrying 5 values, which are integers. Notice how I start the array with an index of zero. Though this doesn't actually change the script, zero is used in almost all programming languages as the start of an array. (Mainly because zero is the first possible whole number, can't have a -1 in a dimension.
To actually assign values to an array, you must use this format:
program TuT;
Var
Arr : Array[0..4] of Integer;
begin
Arr[4] := 10;
Writeln(Arr[4]);
end.

When outputting a value out of your array, you use this
Writeln(Arr[5]);
outputting the fifth digit of the 1 dimensional array Arr.

3) - Loops! My favorite!
Loops, literally, loop parts of your script, making do things as many times as you want.

One of the most common loops is the For-Do loop.

Its structure is:
 For i:= 1 to 10 do
Begin
End;

This makes, for value of the integer y (must be declared), from 1 to 10 (10 times) do something (whatever is in the Begin End.) This changes the value of y, every time the loop goes on.

Now lets take all this knowledge and put it together!

Lets make a program that fills a one dimensional array with 7 string values each saying "Hello world!", and repeats the output on separate lines 7 times.

What we get is:

program TuT;
Var
StrArr : Array[0..6] of String;
i : Integer;
begin
StrArr[1] :='Hello World';
StrArr[2] :='Hello World';
StrArr[3] :='Hello World';
StrArr[4] :='Hello World';
StrArr[5] :='Hello World';
StrArr[6] :='Hello World';
StrArr[7] :='Hello World';

For i:= 1=0 to 6 do
Begin
Writeln(StrArr[i]);
End;
end.

Now I realize that was a little bit tricky, but if you think about it hard enough, you realize you have to put the value of y (as it changes from 1 to 7) in as the placer of the array X.

If however, you thought even harder, you might've come up with this:
program TuT;
Var
StrArray : Array[0..6] of String;
i : Integer;
begin

For i:= 0 to 6 do
Begin
StrArr[i]:= 'Hello World';
Writeln(StrArray[i]);
End;
end.

Which does the exact same thing, in a more efficient fashion.

This concludes the TuT no.2 for pascal! i will probably be making a no. 3 if there is a demand for it. Though I think you can understand most RS scripting TuTs by now!

If it is requested enough, the next one would be about 2-Dimensional arrays, other types of loops and using inputs with points on the screen. (Not graphical points, but co ordinate points :) )

If you have any questions or comments, please leave them below. This is to be a good/clean thread. No Spam please!

J J

2012-09-29 05:08:10

Good tutorial but I would work a bit on your standards and the capitalization. You can choose to either to Begin or begin but in a tutorial or in a script it looks nice if you constantly use the same one.

Also for this
program TuT;
Var
x : Array[1..7] of String;
y : Integer;
begin

For y:= 1 to 7 do
Begin
x[y]:= 'Hello World';
Writeln(x[y]);
End;
end.

I highly advise anyone to start their arrays at zero. I would also advise to use i for loops and a more detailed name for the String Array. Like this:
program Tut;
var
StringArray: Array[0..9] of String;
i: Integer;

begin
for i:=0 to 9 do
begin
StringArray[i] := 'Hello World';
WriteLn('StringArray['+IntToStr(i)+'] = ' + StringArray[i]);
end;
end.

Output
StringArray[0] = Hello World
StringArray[1] = Hello World
StringArray[2] = Hello World
StringArray[3] = Hello World
StringArray[4] = Hello World
StringArray[5] = Hello World
StringArray[6] = Hello World
StringArray[7] = Hello World
StringArray[8] = Hello World
StringArray[9] = Hello World

It's personal preference though.

weequ

2012-09-29 07:11:14

Great tutorial for newcomers. Just try fixing the standards as J J said (always increase identation after begin and decrease after end;). Rep++ for good work.

Rezozo

2012-09-29 08:42:57

From: weequ
Great tutorial for newcomers. Just try fixing the standards as J J said (always increase identation after begin and decrease after end;). Rep++ for good work.


From: J J
Good tutorial but I would work a bit on your standards and the capitalization. You can choose to either to Begin or begin but in a tutorial or in a script it looks nice if you constantly use the same one.

I highly advise anyone to start their arrays at zero. I would also advise to use i for loops and a more detailed name for the String Array. Like this:
program Tut;
var
StringArray: Array[0..9] of String;
i: Integer;

begin
for i:=0 to 9 do
begin
StringArray[i] := 'Hello World';
WriteLn('StringArray['+IntToStr(i)+'] = ' + StringArray[i]);
end;
end.

It's personal preference though.


Thanks, I will guys! J J, could you check this code again. Plus, what is the need for the IntToStr here, as we don't need the Int i to change?

spianist

2012-10-02 04:22:11

This gives me hope to become finally being able to become a non leecher. Hope you release the next tut too! cheers.

Rezozo

2012-10-02 15:29:16

Thank you, your support is appreciated! If you have any problems, feel free to ask. :)

Runehack123

2012-10-17 03:59:38

Thanks for the great guide Rezozo!

Could you please make a guide maybe just about arrays? I find them so hard + complicated to understand and have no idea how to use them effectively when making a runescape script. :)

WriteLn('StringArray['+IntToStr(i)+'] = ' + StringArray) << No idea what that means. With the + and extra bracket and IntToStr…

Hoping you can help me out in any way :) - love the guides keep it up!

Rezozo

2012-10-17 22:49:40

Yey, thanks! ^^ about the :
WriteLn('StringArray['+IntToStr(i)+'] = ' + StringArray[i]);

Don't mind that, J J posted that because in Simba, reading variables is kinda weird.
The way I posted in the OP is perfectly fine and efficient.

What ^^^ Does is make the output look organized and well…Labeled :P
It allows it to print out: 'StringArray[array number here] = Hello World
thats it.

Hope this helps. I might create a separate section for Arrays soon (after SAT's) but might integrate it into this, makes it more efficient.

If you have any more questions, feel free to ask.

Rezozo

2012-11-02 11:19:57

Bump~

———————————————————————————————————

Hey guys!

It seems I am going to add a third part to this series!! WooHoo!
Right, so i need suggestions on what it should be about. Keep in mind, it should be relatively harder to understand than this one.

Thanks
~Joey

Ian

2012-11-02 11:23:10

Video pixel shift tutorial?

Rezozo

2012-11-02 11:42:28

Hmm, that would be more…Simba-ish right? I mean I guess I could. I was looking for basic pascal language only. Any Ideas?
I was thinking maybe controlling co-ordinates? And maybe teach some Menus? (Iffy on the menus)