The Official SCAR Scripting Guide

From SCAR Divi Manual
Revision as of 04:16, 19 December 2011 by Freddy (talk | contribs)
Jump to: navigation, search

This tutorial is under construction, check back later for updates!

Introduction

History

SCAR was originally created by Kaitnieks (Aivars Irmejs) in 2003 with the purpose of replacing his previous creation AutoRune, a program designed to automate tasks in RuneScape. The program rapidly evolved into a very powerful color based macroing application and was used not only for RuneScape, but also to automate other games andtasks not related to gaming. Late 2006 Kaitnieks retired from the scene and passed on the task of developing SCAR to Freddy1990 (Frédéric Hannes), who up to this day still develops SCAR. When the development was passed on, SCAR was renamed to SCAR Divi. Divi is the Latvian word for "two", as now 2 people had developed the program.

Scripting

SCAR has been from the start a scriptable macroign environment. Scripting SCAR is done in a Pascal based language. The syntax of this is very different from C based languages, however, it was originally designed for teaching purposes which makes it very easy to comprehend.

Basics

Distinctions

When thinking about scripting/programming, tehre are a few important distinctions you have to make. There is data and logic. The data in your script originates from values you hardcode into a script or data you get from SCAR's API. This data can be stored in variables, which we'll discuss later on. The logic of your script is the code which makes it work. It is a blueprint for the behavior of your script, it tells it what to do under which circumstances.

Hello World

The first thing you'll want to do when learning any new programming language is create a "Hello World" application. This is an extremely basic script which outputs "Hello World!" in a way the end-user can read it. The reason why you want to do this is to get a basic idea of how your programming environment works. "Hello World!" is a string, this basically means that it's a sequence of characters. The Pascal programming language marks strings by surrounding them with apostrophes. If you want to add an apostrophe to a string, you need to place 2 inside of the string which makes the first one "escape" the second one and add it to the string. For basic output we can use the WriteLn function, which simply outputs text to the debug box in SCAR's main GUI.

Our resulting script looks like this:

program HelloWorld;
begin
  WriteLn('Hello World!');
end.

Output:

Hello World!

The "Hello World!" string we passed to the WriteLn function is surrounded by rounds. When calling any function that takes arguments, in this case the message to output, you have to pass the arguments by placing them in between rounds after the function name and separate them with commas. Except for a few exceptions which we'll see in the future, you should always terminate lines that contain a funciton call witha semicolon. The "begin .. end." clause we see in the script is always the last thing you'll find in your script (everything after "end." is ignored). It represents the entry point of your script, it will be the first thing that is being executed and you call everything else from there. At the top of the script we notice the program line. This line should always be the very first of the script, if you place things above it, it will cause errors. This line simply denotes a name for your script but it is not required to include it in a script, you can simply choose to remove it. Future examples in this guide will not include this line.

Constants

The most basic form of data storage are constants. These are fields in the memory which contain a single data entry and which are denoted by a specific reference name. In SCAR constants can only contain basic data such as strings, numbers and boolean values. A very important thing to note about constants is that they are immutable, which means that they can not be changed. You can only set constants once by manually assigning them a value in your script, they can not be assigned dynamically during runtime.

Constants are defined in the constants sections of a script, there can be multiple of these sections and they have to be defined in the global scope and not in a code section. All constants must have a different name, if they don't, the compiler does not know which is which.

This example shows the simple use of constants:

const
  StringConst = 'Hello World!';
  IntConst = 12345;
  FloatConst = 12.345;
  BoolConst = True;

begin
  WriteLn(StringConst);
  WriteLn(IntConst);
  WriteLn(FloatConst);
  WriteLn(BoolConst);
end.

Output:

Hello World!
12345
12,345
1

Variables

A second form of data storage are variables. Variables work like constants in the way that they are denoted by a unique name (which can't clash with names of constants), they can't however be assigned a value directly. All data in varaibles has to be assigned inside of code sections. this means that thye can both be read and written to. Variables can not only hold basic data types, but also abstract data types which we'll take a look at later. They are defined in the variable section in the global scope or a local scope. To define them, you place a colon and the data type after the name of the variable and terminate it with a semicolon. You can also add several variables of the same type to a single declaration by separating them with commas.

In this example we declare 3 variables, 1 of the type string and 2 more of the type Integer, the latter of which holds a whole number:

var
  Str: string;
  i1, i2: Integer;

begin
  Str := 'Hello World!';
  WriteLn(Str);
  i1 := 5;
  i2 := 6;
  WriteLn(i1);
  WriteLn(i2);
end.

Output:

Hello World!
5
6

Types

Basic Types

Pascal provides a series of basic data types which are the most basic form of data you can store.

Logical data types:

Name Data Example
Boolean True or False (1 or 0) True,False

Integer data types:

Name Data Example
Byte 0..255 0,1,6,77,98
ShortInt -128..127 -48,-6,0,75,125
Word 0..65535 0,5,96,7768,15568
SmallInt -32768..32767 -2587,-68,0,96,15568
Cardinal 0..4294967295 0,125,16875,4987542,65987423
Integer -2147483648..2147483647 -15266984,-687450,0,4987542,65987423
Int64 -18446744073709551616..18446744073709551615 -15224679526487264,-687458366350,0,78479875757242,659757875777423

Decimal data types:

Name Data Example
Single Single precision floating point value (4 bytes) -2.9,0,5.874
Double Double precision floating point value (8 bytes) -136.236542738,0,4875.56974584
Extended Extended precision floating point value (10 bytes) -136.23654573742738,0,4875.569174537384

Text data types:

Name Data Example
Char A single character a,9,B,!,@
String A string of characters Hello World!
AnsiString An ansi string of characters Hello World!