PDA

View Full Version : All-Proof - Walking Through Variables



Daniel
09-17-2008, 07:39 AM
The SCAR
Walking Through Variables

Guide written by Dan's The Man. Supposed to be absolute noob-proof.

If you enjoy this tutorial, please add points to my reputation to show your appreciation ;)

For a much more thorough understanding of types and variables, please refer to this tutorial (http://www.villavu.com/forum/showthread.php?p=601951#post601951) ;)

Introduction:
This chapter takes you through the use and wonders of variables. "The Scar", walks you through what to use variables for and when. This information is vital for all scripters. If you don't know then you should read on as this is one of the steps on your way to becoming a more advanced scripter into the customized pascal language, SCAR.

Requirements:
None

Contents:
What are Variables
When to use Variables
Common Variable types.
Setting up Variables.
Difference between Constants.
Converting one type to another.
Use of correct formatting.
Receiving Errors and Fixing them.

1 - What are variables:
Variables are a way of defining things in SCAR. Every script should contain at least one variable, no matter what type it is. It can change as many times you want it to and it can be called as a replacement. They're the most used item out of an entire script, especially a script that is for Runescape or any other massive online role playing game.

There are two locations a variable can be put, and they have their own special names too, a local variable and a global variable. A local variable is a variable that can only be used in a procedure or a function, such as brackets. A global variable, on the other hand, can be used throughout the entire script, it can be used inside functions and procedures and then in another one which is declared later on.

2 - When to use variables:
The use of variables can be called at any time. You should use a variable when you're dealing with progress reports or when you want to count something or change ones value to another.

3 - Common Variable types:
Variable types is the type of a variable. There are four mainly used types that can be used. The types are as follows:
Integer - An Integer is a variable type that contains numbers, e.g 10.
String - A String is a variable type that contains letters or words in between apostrophe's, e.g 'Hello'.
Boolean - A Boolean is a variable type that contains True or False statements, e.g True.
Extended - An Extended is a variable type that contains numbers with decimal places, e.g 4.6.
Byte - A whole number that consists of any whole number between 0 and 255.
Char - A string which only consists of one character.
Double - The largest variable which holds numbers, both an Integer or an Extended type. A double can contain no more than 15 numbers both negative or positive (meaning the minimum and maximum values are: -999999999999999 and 999999999999999). If the length is longer than 15, but less than 19, it will return: 1E*, where the * is the number of digits stored in the variable. If, however, it exceeds 19, then it will return 0. To convert this type, use the Float conversion methods (e.g FloatToStr, Round, etc).
Variant - A variable which can store/support anything of the above. Basically it's everything above joined together. For a complete tutorial on how to use them, please click here (http://www.villavu.com/forum/showthread.php?t=36456?p=478144#post478144).

4 - Setting up variables:
You have to declare a variable of course. To declare a variable means that you need to call it something and then have a type after it. To declare a variable, put the word var and then press enter/return on your keyboard. Now below that var line everything called below it will be a variable as well, except if you have a begin, end, procedure or a function, then the script will expect the name a colon and a type. I have pretty much told you how to set up a variable in that last sentence but i want to make it more thorough.

To set up a variable, you have to have give that variable a name, so think about that for a while. That variable name should be unique, but also contributing to the type as well, what i mean by that is if i call a variable as LoadsDone and i put it as a String and that string holds 'Hello' then it is not contributing to the type. So once you have thought of a name then add a space and then a colon. After this colon, has the variable type, such as String. So put the variable type then a semi-colon to stop other variables being recognised as that type your specified.

5 - Difference between constants:
There are two main differences between constants. Number One, is that constants don't have to be declared as a type, you can just enter a String, Integer, Boolean or an Extended number. Number Two, constants can not change from one thing to another while variables can change.

6 - Converting from one type to another:
Converting is very useful in scripts, especially in progress reports. Most people convert types to strings, but there are many other methods as-well :) For example, converting an integer to a string or an extended to an integer. Below are a list of methods for converting stuff to one another in order, also, take note that you can use one of these conversion methods to convert it to any one of the types specified (e.g w:=StrToInt..., where 'w' represents a Word type):
To an Integer / Word / Byte:
StrToInt(s: String): LongInt;
Converts a string which consists of number only into an integer, word or byte. If it is too large for a word or byte, then it'll return 0.
Round(e: Extended): LongInt;
[i]Converts an Extended number to an Integer, by rounding it to the nearest whole number. It is rounded since Integers only support whole numbers.
Trunc(e: Extended): LongInt;
Converts an Extended number to an integer, by rounding it down to the nearest whole number. It is rounded since Integers only support whole numbers.
Floor(x: Extended): Integer;
Converts an Extended number to an integer, by rounding it down to the nearest whole number. It is rounded since Integers only support whole numbers.
Ceil(x: Extended): Integer;
Converts an Extended number to an integer, by rounding it up to the nearest whole number. It is rounded since Integers only support whole numbers.

To an Extended / Double:
NOTE: There are no Integer conversion functions since Extended variables also support whole numbers. This is what you'd do: e:=i; (where 'e' represents an Extended variable and 'i' represents an Integer variable).
StrToFloat(s: String): Extended;
Converts a string which consists of whole numbers or an Extended value.

To String / Char:

7 - Use of correct formatting:
Formatting, also called Standards, is what most scripters want to see in others scripts. The reason why it is mostly required is because it makes scripts more user friendly and it is better for more advanced scripters to look through the script then suggest things which are to be added or to be shortened.

Ok, to start the formatting part of the tutorial. If you have a word that is bold in your scar script, the next line should contain two or three spaces after the first bolded word, not a symbol such as ; or . but an actual word that is bolded, such as if. If there you're adding a bolded word under another bolded word, then that bolded word does not require it to have two or three spaces. Only in the next line when a word that is not bolded should receive the extra indending.

In "The Scar", the formatting tutorial is very basic as not much work has been achieved into that. So it is recommended you look at the Standards tutorial written by BamBam, it can be found in the SRL Forums: Tutorial Island section.

8 - Receiving errors and fixing them:
When you're creating your first variable, you're most likely to get at least one error. This part of the tutorial will cover all, if not most, of the errors that will be displayed when you make a variable.

PPP = Any Value

Type mismatch in script:
This means that you have tried to use the variable as a different type other then what it was declared as. For example, i make a variable called coordx and i set it as an Integer and i try to make the value of coordx like coordx:='hello';. Instead of a number i added a string. So either change the type of that variable or change the value of that variable.

Unknown identifier PPP in script:
This means that you have put a variable that SCAR cannot recognize. For example, i have a variable called xx and then i want to change the value, i might mis-spell the name like xz:=xx+1;. Simply means that you have added a variable, somewhere in your script that you have not declared. To fix it, add that variable, remove that variable or correct the spelling of that variable.

Variable Expected in script:
This means that a variable needs to be added into the script somewhere. Just add the variable and you'll be on your way to making more variables. This mainly occurs when including an include file.

quickTutes
09-26-2008, 07:24 AM
This is really useful, Is there any site which provides me some online tutorial on Programming and other stuffs.

AzulDrake
09-26-2008, 08:03 AM
quickTutes... Google is your friend :)

Daniel
09-27-2008, 08:33 AM
This is really useful, Is there any site which provides me some online tutorial on Programming and other stuffs.
Thanks quickTutes.

ShowerThoughts
09-27-2008, 08:34 AM
This is way to hard, it's not all-proof :p

Daniel
09-27-2008, 08:49 AM
This is way to hard, it's not all-proof :p
Which parts aren't? Please tell me so i can improve (I don't think i can) :p

Dervish
11-05-2008, 12:14 PM
Sorry for gravedig but isn't there the byte too :s ?

Daniel
11-17-2008, 09:04 AM
Sorry for gravedig but isn't there the byte too :s ?
Since it's already at the top of the list since the posts are arranged in alphabetical order, so it doesn't matter.

Also, for the byte, i put the most common ones used. But i'll add that anyway along with others :)

wired357
12-04-2008, 06:58 AM
I found sections 2 and 6 help as a reference for me. Thank you for working this up. Your other tutorials are a little advanced for me at this point, but I ill use them when I'm ready :)

blitzkrieg
12-26-2008, 03:23 AM
Hah found this quite interesting since I'm into programming quite a lot.
Nice Tutorial m8.
Well explained especially for noobs.