SRL Archive - OSR Advanced Scripting Tutorials

Unknown\unusual things in SCAR!


mastaraymond

2007-09-14 04:23:16

Hello,

This tut is going about some procedures you may not know. It contains some random things, but i think you would like to know it ;). Lets start i guess?

Table of index:

Slow functions
–Inc\Dec
–Length
Random things
–clColors
–Continue
–Chr
–Integer as Procedure
–Const as a parameter
Compiler directives
–List of commands
–Example
Unusual functions
–Xor
–SHR and SHL
–And,or,Xor,Not as math functions
–Hex
API Calls
–Explanation
GetSelf and GetApplication
–List of commands
–Example
Types
–Type list
–Example of using procedures in types


Slow functions

In this version, scar 11, there are some functions that are slower then others ^^.

Inc\Dec(x : integer): This like 2 times slower than x := x +\- 1;
Length(Arr : Array); If you use Length for getting the length of an array, you should use GetArrayLength. Length is just a bit slower but every ms counts ^^.

Random things


clColors
Most of you know it already, for those who don't: SCAR has got a big library of already set 'Consts'. You have got consts for RuneScape, like: MSX1, MSCX etc.. But there are also a lot of COLORS defined! For example, if you need black you can just type : clBlack. Its the defined const for 0. You have got loads of these. Try this: Type cl + ctrl + space in scar, you see a huge list of colors popping up ;).

Continue
This command has been posted somewhere else on the forum, but for those who dont know. The continue command goes back to the begin of a loop, its like a 'Goto' command, but without defining the label, its really useful. Here is a example:
program New;
begin
Repeat
Writeln('Before continue!');
Continue;
Writeln('After continue!');
Until(false)
end.
program New;
var
I : integer;
begin
For I:= 0 to 15 do
begin;
if (I = 5) or (I = 14) then Continue;
Writeln('The number is: '+inttostr(I));
end;
end.

Chr(x : Byte)

You can replace Chr(63) by #63 . Its a lot shorter to write, here is a example.

program New;
begin
Writeln(chr(72)+chr(101)+chr(108)+chr(108)+chr(111)+chr(32)+chr(119)+chr(111)+chr(114)+chr(108)+chr(100));
Writeln(#72#101#108#108#111#32#119#111#114#108#100);
end.

Integer as procedure!

Did you know, you can use some types as a procedure to? I've done a few tries and heres a example:
program New;
var
C : integer;
D : Char;
begin
C := Integer(5);
D := Char('A');
Writeln(Inttostr(C));
Writeln(D);
end.

Const in parameters?

This is kinda useless:spot:. If you make a procedure can user parameters like (var I : string;B : integer). But did you know you can add 'const' to it? It would become like this: Procedure test ( const s : string);. Not really useful or so, but just cool to know ^^.

Compiler directives

SCAR has got a few compiler directives, they aren't very useful, but cool ^^.
A compiler directive is something that communicates with the compiler himself, its a piece of code that tells the compiler to do, or not do something.
As far as i know, scar only supports 'if' statements in the directives. Lets take a look at the directives, how to use them is bellow:
$Define: This declares something, like setting a boolean to true.
$UnDef: This UNdeclares something, like setting a boolean to false.
$IfDef: This is a IF loop, if the thing that comes behind this statement is defined then…
$IfOpt: Im not sure, i think its the same as $IfDef.
$IfNDef: If something is NOT defined then..
$Else: Just like if .. else.
$EndIf: You must place this after a if loop, to show the compiler your done with the loop!

Here is a example with using the most directives:

program New;
{$Define Bla}
begin
{$IfDef bla}
WritelN('Defined!');
{$endif}
{$IFNDef bla}
Writeln('We didnt Define it!');
{$endIf}
{$UnDef bla}
{$IfDef bla}
WritelN('Defined!');
{$else}
WritelN('Not defined!');
{$endIf}
end.
Also scar has got 1 defined directive if im right: divi.
Look in the test folder, copied it for you:

program DiviDirective;
begin
{$IfDef Divi}
WriteLn('This is SCAR Divi 3.05 or higher!');
{$Else}
WriteLn('This is SCAr Divi 3.04 or lower!');
{$EndIf}
end.

Unusual functions

Xor

You guys all know keywords like or, not , and. There is a forth, called Xor. Xor checks if only 1 side of the statement is true. If only 1 side returns true, the xor command returns true. Here an example:
program New;
begin
if (50 = 50) xor (50=40) then Writeln('Only 1 side returns true!');
if (50=50) xor (50=50) then
Writeln('Both sides return true!')
else
Writeln('Both sides return true, or both sides return false!');
end.

SHR and SHL
(For more info about this I like to refer to yakmans_ tutorial about bitwise operations)
My brabble below is wrong, It's not exactly what the function does. Do not take it for granted.

First of all i want to thank Wizzup? For explaining this to me :rolleyes:.
SHL and SHR are functions that shift bits, of the binary of a integer, if im right ^^. Its kinda easy: 2 shl 10 = 2048. Other written its: 2 x 2^10. Another example, 5 shl 4 = 80. Other written its: 5 x 2^4. SHR is kinda the same, except it divides the integer. 1 shr 2048 = 1. Other written it would be: 1024 div 2^10. Im not quite sure how shr really works, but google it if you really want to know^^. Im not good at explaining this, so heres a example:
program New;
begin
Writeln(inttostr( 1 shl 10));
Writeln(inttostr( 1 shr 1024));
Writeln(FloatTostr(pow(2,10)));
Writeln(FloatToStr(1024 div Pow(2,10)));
end.

And, Or, Xor, Not as math functions!

You know the keywords above, but did you know they can be used as math functions to? And, Or, Xor and Not can be used as math functions on integers. It compars the binary of the numbers you put in, and results something 'new'. Below a explanation of what they do, those numbers are binary.
Xor | 10110001 Xor 01100110 = 11010111. If the bits are equal, the result bit becomes a 0 else a 1.
Or | 10110001 Or 01100110 = 11110111. If both bits are 0, the result becomes a 0 else a 1.
And | 10110001 And 01100110 = 00100000. If both bits are 1, the result bit becomes a 1 else a 0.
Not | Not reverses the input, the 0 becomes a 1 and a 1 becomes a 0.

Hex!

Hex (Hexadecimal) is a different numeral system that we are used to. Hex is a 16-base numeral system, while we are used to the decimal system (10-Base).
Heres a comparing:
Decimals: [0,1,2,3,4,5,6,7,8,9]
Hex: [0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F]
The decimal number 10 in hex is A, 11 is B etc.. Hex is often used for HTML, as a color. Its a quite simple system. Colors are based on a R,G,B value. The max of each value is 255. In hex FF is equal to 255, this means you can declare R,G,B by using max 2 digits per color. The HTML hex colors are like this
#RRGGBB. The first 2 digits represent the red value, the GG green and BB blue.
So, #FFFFFF : R = 255, G = 255, B = 255. In scar to use a Hex number you need to put a $ in front of it. $F = 15. I've made IntToHex and HexToInt, its somewhere in the snippet board.

API calls

A API call is basically calling procedures out of other files. Windows has made a lot of DLLs, this are files which contain procedures and functions that edit or do something in windows. API Calls allow us to call those functions and use them. If you want to do a API Call you must enable 'API Calls' in the Script tab of SCAR. Then use the syntax below to load the procedure into scar, and then just use it ^^. For more info check the DLLTest.Scar in your Script>Test folder!
function FindWindow(C1, C2: PChar): Longint; external 'FindWindowA@user32.dll stdcall' ;

GetSelf and GetApplication

I think most of you know GetSelf and GetApplication, but for those who don't ;). This 2 commands are objects, that belong the scar itself. Ill give you a short list of useful commands:

GetSelf.Caption : This changes the title of the scar window, you can also do this by Disguise(Title : string);
GetSelf.Enabled : This is a nasty command. If you set this command to false you can Disable scar, which means you cant press buttons or change the windows position, its like freezing scar.
GetSelf.Handle : This is the HWND handle of scar. Every window has got a handle, with this handle you can change the windows size, move it, change title its often used in
GetSelf.Height\Width : This are commands which indicate the size of the scar window.
GetSelf.Left\Top : This are the x and y of the left top from scar. If you change these, scar will move.
GetSelf.Hide\Show : This .Hide hides the scar window until you have called .show . When the window is hidden, i dont think there is a way to get it back, without using GetSelf.Show.
GetSelf.Visible : This is almost the same as show\hide. Once you made it invisible, you must set the boolean to true to get it back!
GetSelf.WindowState : This is the state of the window, in scar can give it 3 states: Minimized, Maximized and Normal. Use it like this: GetSelf.WindowState := wsMaximized;
GetApplication.Minimize : With this procedure you can minimize the scar window.
GetApplication.Handle : Also indicates a handle, I dont know exactly what the difference with GetSelf.Handle is, i guess the getself is a ChildForm handle..
GetApplication.MessageBox : This is a cool feature, it allows you to show MessageBoxes to the user, with for defined boxes. The first parameters is what text you want to show, the second what title the box should have and the 3th indicates what kind of box you want to have. I made a example.

program New;
var
I : integer;
begin
I := GetApplication.MessageBox('You want to writeln Hello world?','Question',4);
if I = 6 then Writeln('Hello World');
if I = 7 then Writeln('You basterd, why you dont want to write Hello World?');
end.
Types

Ok, i guess we all know Types, if not read Nielsies tutorial. Types in scar are quite limited but we can still do some cool stuff. A type doesn't have to be a RECORD. I made some examples of types i know:
Temp = Array of array of array of integer; This one makes a type that has got 3 dimensions.
Temp = (True,False,Maybe); This one makes a type that can hold 3 values: True,False, Maybe. When you load this type, its declared as True by default, because True is the first on in the list :p. If you make it: (False,True,Maybe) Its loads False by default.
Temp = record x,y : integer end; This one makes a type that can hold the values x and y.
Temp = Function ( X : integer) : string; This one makes a type for a function. This is cool, its like a pointer to a procedure. It also allows you to use a function as a parameter. Its hard to explain this, so thats why i added a example script! Look and understand ;).
program New;
Type
Temp = Function(x:integer): string;
Cool = record
x : integer;
Func : Temp;
end;
var
A : Cool;
B : Temp;
Function Boo ( Funct : Temp;x : integer): string;
begin;
Result := Funct(x);
end;

Function Test (x : integer): string;
begin;
Result := IntToStr(x);
end;
begin
B := @Test;
Writeln(B(25));

A.Func := @Test;
A.x := 500;
Writeln(A.Func(A.x));

Writeln(Boo(@Test,5));
end.

Santa_Clause

2007-09-14 04:53:32

Nice tutorial!

n3ss3s

2007-09-14 04:54:11

Good job.

I was familiar with types, but shr and shl really bugged me, thx.

nielsie95

2007-09-14 07:16:37

Nice tut! :)
But the "slow functions" aren't slow anymore in the next scar ;)

mastaraymond

2007-09-14 12:17:43

From: nielsie95
Nice tut! :)
But the "slow functions" aren't slow anymore in the next scar ;)

Yeah i know. If freddy just find a way to add ASM to the scripting engine, scar might become really fast ^^.

EDIT: Woot, i've got the tutorial writer cup! ^^.

R0b0t1

2007-09-14 14:53:15

Actually, and multiplies and or divides. (Is what I was told).

Shifting left is like multiplying by two, vice versa for dividing.

Ehhh…. Nothing else I think :p.

mastaraymond

2007-09-14 15:03:52

From: r0b0t1
Actually, and multiplies and or divides. (Is what I was told).

Shifting left is like multiplying by two, vice versa for dividing.

Ehhh…. Nothing else I think :p.

Like this?
1 shl 10 = 1024. Other written its: 1 x 2^10 = 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2
^^.

Zezi

2008-03-31 18:27:14

I leant a lot, but i don't think i'll ever be making a script that actually uses any of this. :\

Maybe self.hide etc but.. Dunno :P I'll have to reread :p

Woah, i totally grave digged, SORRY.
Shupid search button :(

SKy Scripter

2008-03-31 18:49:00

I was hoping i would find something new in here :(.

edit: this is like an old topic -.-

Negaal

2008-04-01 08:23:36

Wow, this is most intresting tut what I have found recently, though integer as procedure and compiler directives were only one where I got lot of smarter:)

Im not quite sure how shr really works, but google it if you really want to know


Actually…it's right and left shift… it does all in binary system…

See,


program New;
begin
writeln(intToStr(10 shr 1));
{
10 is 1011.
Now, it shifts 1 bit from right.
and it becomes to 101, whats 5 in decimal
}
writeln(intToStr(10 shl 1));
{
shifts 1 bit to left and now there will be 0 at the end, what will be 20 in decimal.
}
end.

{
1 - 1
10 - 2
11 - 3
100 - 4
101 - 5
110 - 6
111 - 7
1001 - 8
1010 - 9
1011 - 10
1100 - 11
1101 - 12
1111 - 13
10000 - 14
10001 - 15
10010 - 16
10011 - 17
10100 - 18
10101 - 19
10110 - 20
}

Im not sure did anyone understood much. There good explanation for this in wikipedia

Logical right shift one bit

Logical left shift one bit

http://en.wikipedia.org/wiki/Logical_shift

:)

Good job.

Edit:
Fak. I thought SKy scripter said this is old thread because he knows all. I hate grave diggers.
-.-

mastaraymond

2008-04-01 11:17:27

First of all this tut is pretty old. I know how SHL and SRL work right now, I've used it quite often, but at that time I didn't :).


@Sky: Meh, you won't.. Guess its pretty much done with "newness" in scar xD.

Wizzup?

2008-04-01 11:26:43

From: mastaraymond
First of all this tut is pretty old. I know how SHL and SRL work right now, I've used it quite often, but at that time I didn't :).


You already knew about it when you wrote this tut. ;)

First of all i want to thank Wizzup? For explaining this to me :rolleyes: .

ShowerThoughts

2008-04-01 12:33:20

Sweet, still a bit confusing but! it is cool!

EvilChicken!

2008-04-01 13:37:45

Gah, if its already been gravedug you might aswell add RandomRange to the slow procs/functions list. Twice as slow as int + random(int) I assume?

Negaal

2008-04-03 09:33:58

Pretty intresting and useful function is InRange(), though I'm not sure how fast is it..

Markus

2008-04-05 06:37:31

Suggestion: a try .. finally block :)
They aren't used that much (actually, I never saw them used to their full potentional), but are EXTREMELY useful for avoiding memory leaks:


program TryFinallyDemo;
procedure ThisIsNowANonLeakingProcedure;
begin
try
writeln('Here I load a Bitmap/DTM');
exit; //SOme condition here
writeln('And here I often get a leak');
finally
writeln('But with Try ... Finally, I won''t leak, wewt :)');
end;
end;
begin
ThisIsNowANonLeakingProcedure;
end.

See? Instead of

if randomcraphere then
begin
FreeBitmap(SomeBitmap);
exit;
end;
FreeBitmap(SomeBitmap);
We can just do:


try
if RandomCrap then
exit;
finally
FreeBitmap(SomeBitmap);
end;

Result: code is clean, no leak, no more messing around and it owns :)

dweg

2008-11-01 17:18:56

thanks for the help :)