PDA

View Full Version : A Tut on Consts and Vars



Naum
02-01-2009, 11:57 AM
Consts and Vars


Table Of Contents

I - Intro
II - Consts(Constants)
III - Vars(Variables)
IV - Differences
V - End Note



I - Intro

Hey, Hello and welcome to my tutorial on Consts and Vars (Consatnts and Variables). In this tutorial I hope to teach you how to use each and what are the pro's and con's of using one or the other :). So do you know what a Const is? No? Read On Because along side working examples I will tell you the meaning. Lets go..
Note : you must know about basic data types before you use this tutorial ;)



II - Consts(Constants)

So you may have heard this word creep up before and wondered wtf it was, well...

Definiton : Consts are a quantity that do not vary, you can set these yourself and they will not change.Consts are used In many different aspects of scripting, they maybe used to display something. They can also be used to let the user input something e.g Color of Road, before

a script starts :).

In this example:
Const Fish = 'Cod'; //Const is SCAR's way of saying Constant

Begin
If Fish = 'Cod' Then
WriteLn('Fish is Cod');
end.

My const is 'Fish' and it is given the quantity of 'Cod' please note how I have defined it with an equals (=) sign please dont use ':=' as that is only used with Variables. After that I have checked to see if that Constant equals something in this case 'Cod'.
If you change Fish to something else then it will not result :).

Other ways of using them:
Const Tolerance = 15;

Var x, y : Integer;

Begin
If Not FindColor(x, y, 65536, 0, 0, 700, 700, Tolerance) Then
WriteLn('please raise/lower tolerance, as object was not found');
end.
^ This uses the value of Tolerance in a function ^


Const Version = '0.1';

Begin
WriteLn('Script is version '+Version);
end.
^ Will write in the version of the script ^


Const Pii = 3.141;

Begin
If Not (Pii = Pi) Then
WriteLn('Const Pii isn''t accurate enough');
end.
^ Will compare two values ^


A constant can be any data value (String, Extended, Integer, Boolean) etc

A constant is always very handy when keeping something the same, but they are times when If you don't find something (refer to Tolerance example) you dont want to change the value it self because you cba. But now you can Introducing...



III - Vars(Variables)

Definition : A variable is a quantity that can assume any of a set of values, Basically it is something that can Vary within the script.

To make a variable you must place a new section into your script. Write var on a line to tell scar that you will be using variables, then write your Variable e.g x,y, and then put a colon after it ':' and then define what data type it is (Integer, String, Boolean etc).

E.g
Var x, y : Integer;
Bool : Boolean;
Str : String;

Thats how you would declare them.


Okay.. Say for instance that, my script needed to find a color. In the last section I used this:

Const Tolerance = 15;

Var x, y : Integer;

Begin
If Not FindColor(x, y, 65536, 0, 0, 700, 700, Tolerance) Then
WriteLn('please raise/lower tolerance, as object was not found');
end.

x, y are variables as they store data and vary on the co-ordinates

But my problem is, I'm not always on the computer so finding the right tolerance for the color can be tricky. Hmm this is where I use variables:


Var x, y, Tolerance : Integer;

Begin
Repeat
If Not FindColorTolerance(x, y, 65536, 0, 0, 700, 700, Tolerance) Then
WriteLn('please raise/lower tolerance, as object was not found');
Tolerance := Tolerance + 1;
Until(Tolerance > 50) Or FindColorTolerance(x, y, 65536, 0, 0, 700, 700, Tolerance)
If Tolerance >= 50 Then
WriteLn('Failed') Else
WriteLn('Color tolerance for object is '+IntToStr(Tolerance));
End.


Okay so I declared my variables x, y and Tolerance. So what this script did was if it didn't find a color with the tolerance it would keep adding I onto the tolerance until it found that color or the tolerance was over 50. It would then say it failed or say it found the color with the amount of tolerance.

That sounds leet, but there are more things you can do:
Var Stairs : Boolean;

Begin
If FindStairs Then
Stairs := True
Else
Stairs := False;

If Stairs = True Then
ContinueChopping
Else
WriteLn('Couldn''t find stairs');
END.
^ Will try and find sairs and continue chopping ^.


Var x,y : Integer;

Begin
If FindColorTolerance(x, y, 255, 0, 0, 700, 700, 15) Then
WriteLn(Find Object at '+InttoStr(x)+', '+InttoStr(y)+');
END.
^ Returns a color found in x and y co-ordinates ^.

There are many, many more uses of Variables.


IV - Differences

There are 3 fundamental differences between Variables and Constants:

1. Variables can change their data during runtime, whilst consts have to be changed before hand.

2. Consts can take any data type to be stored, whilst with Variables Data types must be declared.

3. Variables can be declared as Variants (search bar) where as constants cannot :).


END.

Hope you enjoyed my Tutorial.

Naum
02-01-2009, 12:08 PM
Made a new thread.

ian.
02-05-2009, 01:12 AM
Var x, y, Tolerance : Integer;

Begin
Repeat
If Not FindColorTolerance(x, y, 65536, 0, 0, 700, 700, Tolerance) Then
WriteLn('please raise/lower tolerance, as object was not found');
Tolerance := Tolerance + 1;
Until(Tolerance > 50) Or FindColorTolerance(x, y, 65536, 0, 0, 700, 700, Tolerance)
If Tolerance >= 50 Then
WriteLn('Failed') Else
WriteLn(Color tolerance for object is '+IntToStr(Tolerance));
End.

:p you forgot the first '

^^ looks nice, thanks!

EDIT: Var x,y : Integer;

Begin
If FindColorTolerance(x, y, 255, 0, 0, 700, 700, 15) Then
WriteLn(Find Object at '+InttoStr(x)+', '+InttoStr(y));
END.

same thing :p

bullzeye95
02-05-2009, 01:47 AM
Const Pii = 3.141;

Begin
If Not Pii = Pi Then
WriteLn('Const Pii isn''t accurate enough');
end.

should be
Const Pii = 3.141;

Begin
If Not (Pii = Pi) Then
WriteLn('Const Pii isn''t accurate enough');
end.

Naum
02-05-2009, 08:05 AM
KK corrected :)

pwnzorc
02-21-2009, 01:19 PM
nice tut! learned alot ^^

Q Golden Q
02-21-2009, 05:50 PM
Thank You, this really helped me.

rontherhino
02-22-2009, 09:57 PM
okay... this is why my scripts were mesing up

pl0xmypl0x
03-24-2009, 03:49 PM
thanks this helped me understand better :)

ian.
03-24-2009, 04:11 PM
Const Pii = 3.141;

Begin
If Not Pii = Pi Then
WriteLn('Const Pii isn''t accurate enough');
end.

should be
Const Pii = 3.141;

Begin
If Not (Pii = Pi) Then
WriteLn('Const Pii isn''t accurate enough');
end.


actually.. it SHOULD be..

const
Pii = 3.141;

begin
if(not(Pii = Pi))then
Writeln('Const Pii isn''t accurate enough');
end.

Naum
03-24-2009, 05:08 PM
What It works fine without the brackets?

ian.
03-25-2009, 03:18 AM
correct standards :p

nothing important.. just bugs me lol

by the way Naum.. you should help me with for to do :p

I need help pl0x! :D