PDA

View Full Version : Types and Arrays



nielsie95
05-07-2007, 07:59 AM
Hello and welcome to the Tutorial:


Types and Arrays



Chapters

What's a type?
How do I make a type?
How do I set a type?
How do I use a type?
What's an array?
How do I make an array?
How do I use an array?
Combining types and arrays





What is a type?

A type is a list of custom variables. The name of the type is called record. For example DoorProfiles:


type DoorProfile = record // This is the name of your type
Points: array of tpoint; // These are the things you can call out of a DoorProfile type. (Custom variables).
MidPoint: Tpoint;
PixelCount: integer;
Slope: extended;
Color:integer;
end;


Another example, the player array:


type
TUser = record
Name: string; // * User Name
Pass: string; // * User Pass
Nick: string; // * Screen Name for random detection
Active: Boolean; // * Set to True if Ok, False if Lost.
Loc: string; // * User Location
Rand: string;
Skill: string; // * User Action to Perform
Level: array[0..21] of word;
// * Levels of all skills. SetIn GetPlayerLevels.
Ore: string; // * test
WaitTime: Integer; // * Wait Time for Each User, Good for Autofighters
Fight: Boolean; // * If we want the Char to randomly fight
Worked: Integer; // * Time User has worked
Banked: Integer; // * Number of Banks User has done
Killed: Integer; // * Number of Kills User has made
Pin: Integer; // * Current Users Pin Number
Boolean1: Boolean; // * For reports, etc.
Boolean2: Boolean; // * For reports, etc.
Boolean3: Boolean; // * For reports, etc.
Integer1: Integer; // * For reports, etc.
Integer2: Integer; // * For reports, etc.
Integer3: Integer; // * For reports, etc.
Integer4: Integer; // * For reports, etc.
String1: string; // * For reports, etc.
String2: string; // * For reports, etc.
String3: string; // * For reports, etc.
Extended1: Extended; // * For reports, etc.
Extended2: Extended; // * For reports, etc.
Extended3: Extended; // * For reports, etc.
end;

Another example, TPoint:


type Tpoint = record
x, y: integer;
end;





How do I make a type?

If you start making a type, you need to have a name. The name is what you put between type and = record:


type RGB = record


After the name you can just make a list of what you would like to integrate in you type:


type RGB = record
green: integer;
blue: integer;
red: integer;

{SHORTER:}

type RGB = record
green, blue, red: integer;


And what you don't need to forget: an end at the end of your list!!:


type RGB = record
green: integer;
blue: integer;
red: integer;
end;

{SHORTER:}

type RGB = record
green, blue, red: integer;
end;





How do I set a type?

You can only set types at the begin of you script. That means you can't set a type in a procedure or function!
If you set a type at the start of your script, it doesn't mean that it's a variable! If you take a look at the example of the player array above, you can see that the type is called TUser and not Players. That's why you need to set your type as a variable. Example:


var Player: TUser; //Now you can use the TUser in your script.
var RGBColors: RGB //Now you can use RGB in your script.


WRONG:

TUser.Name := ' '
TUser.Pass := ' '
TUser.Active := True;

RGB.Blue :=
RGB.Green :=
RGB.Red


GOOD:

var Player: TUser;
var RGBColors: RGB;

Player.Name := ' '
Player.Pass := ' '
Player.Active := True;

RGBColors.Blue :=
RGBColors.Greem :=
RGBColors.Red :=






How do I use a type?

You use your types just as you use a normal variable. Example:


a := 8;
b := 5;

RGBColors.Blue := 23;
RGBColorS.Green := 3;
RGBColors.Red := 80;

a := GetColor(123, 456);
RGBColors.Blue := GetColor(456, 789);

FindColor(Tpoint.x, Tpoint.y, 123, 456, 789, 765);






What is an array?

An array is a list of variables defined by numbers. Example:


var HeadColor: array[0..2] of Integer;

{ This will contain HeadColor[0], HeadColor[1], HeadColor[2] }

var MonsterColors: array of Integer;

{ This doesn't have a length yet }






How do I make an array?


To make an array, you first need to set your array as an variable:


var MonsterColors: array of integer; //If you don't know the length of the array yet

var MosterColors: Array[0..10] of integer; //If you know that the length will be 11 (you can also set something on 0).


Now you can define everything in the array:

{If you know the length:}
var MonsterColors: array[0..4] of integer;

MonsterColors[0] := 1;
MonsterColors[1] := 2;
MonsterColors[2] := 3;
MonsterColors[3] := 4;
MonsterColors[4] := 5;

{If you DONT know the length:}
var MonsterColors: array of integer;

MonsterColors := rs_ScanMiniMap(1234) //rs_ScanMiniMap will return all similar colors into an array of integer. It sets the length automaticly.






How do I use an array?

Now you have set your array, you can start using it:

If FindMSColor(x, y, MosterColors[0]) or FindMSColor(x, y, MosterColors[1]) or FindMSColor(x, y, MosterColors[2]) then WriteLn('One of the colors was found.);

The way above is the long way and most of the only only works if you know the length of the array. A quicker and way more used way to do it is this:


var i: integer;

{If you know the length:}
for i := 0 to 4 do //This will perform FindMSColor 5 times with every time an other part of the array
begin
if FindMSColor(x, y, MonsterColors[i]) then WriteLn('Color found');
end;

{If you DONT know the length - This way can also be used for the array above}
//With GetArrayLength you get the length of the array. You always have to do -1 after GetArrayLength if you use for..do!!!
for i := 0 to GetArrayLength(MonsterColors) -1 do
begin
if FindMSColor(x, y, MonsterColors[i]) then WriteLn('Color found');
end;





Combinding types and arrays

It may be very handy to combine types and arrays. For example the PlayerArray is built this way. First there was made a type of one user (Tuser) and after that they made an array of TUser (Named Players):


Players: array of TUser;


This way you can easily set all your players:


Players[0].Name :='';
Players[0].Pass :='';
Players[0].Nick :='';
Players[0].String1 :='';
Players[0].Integer1 := 1 ;
Players[0].Active := True;
Players[0].Boolean1 := True;

Players[1].Name :='';
Players[1].Pass :='';
Players[1].Nick :='';
Players[1].String1 :='';
Players[1].Integer1 := 1 ;
Players[1].Active := True;
Players[1].Boolean1 := True;


There are various other this where it may be handy to combine types with arrays. But I hope that after reading this tutorial you have the knowledge you make them yourself! :)

Thanks for reading,
Nielsie95

Fearlesssss
05-07-2007, 10:37 AM
Very nice tut man! :D I first didn't even knew what types were:p.
(W00T first post:p)

~alex~
05-07-2007, 08:13 PM
This is just what I needed.... The srl player array doesnt include enough integers strings and bools for my liking.

rkroxpunk
05-09-2007, 12:26 PM
yay I understand arrays :D yes I know shocking.....I was an SRL member and didn't understand arrays *hangs head*

Anyway thx Niel as always!

Smartzkid
05-10-2007, 11:09 PM
Nice tutorial :p

Was a great refresher for types; I'm always forgetting how to use them correctly

bullzeye95
05-10-2007, 11:28 PM
Great tutorial... even though I knew this stuff :p. I'm just posting a suggestion. You can also use type like this type
Number = Integer;
Which would let you make variables like this var
MyNumber: Number; Which would be the same as saying var
MyNumber: Integer;

Good tutorial though!

nielsie95
05-11-2007, 09:14 AM
Yes, thanks for the suggestion :). But I think people would rather use normal variables for that.

BobboHobbo
05-28-2007, 12:31 PM
nice tut A+

Jason2gs
06-07-2007, 03:10 PM
Oooh, thank you :)

I think I'm gonna have to use Types for my project in Delphi. Thank for for making this ;)

x13om13e12x
06-27-2007, 08:30 PM
is it possible to use an integer as an array length like array of [0..I]

nielsie95
06-27-2007, 08:39 PM
Yes:

Array [0..1] of Integer; :)

Tim0suprem0
06-30-2007, 07:53 PM
Wow thank you for this, have been trying to figure out arrays for awhile now :)

DevilCheater
07-04-2007, 02:00 PM
Great tut!

Lalaji
07-09-2007, 11:17 AM
i have been looking for this tut, just found it. Thanks

dark4mdawn
08-10-2007, 10:14 AM
looks good im goin 2 read now been looking for array tut 4 a while lol ty

lordsaturn
08-10-2007, 09:38 PM
This is exactly what I needed! These had been confusing for me and the already existing tuts didn't make much sense...Thanks!

valesar
08-22-2007, 03:48 PM
Awesome Tutorial, now maybe I can use arrays in my first sript.

WhoCares357
12-22-2007, 10:01 PM
Very helpful. Thank you.

Negaal
01-05-2008, 04:48 AM
Gah, I haven't slept for like 20 hours and I got it in first time...Congratz for me:)
heh my brilliant art piece...

program New;

type
EPoint = record
x : integer;
y : integer;
z : integer;
end;

procedure lol;
var ThePoint : Epoint;
begin
thepoint.x := random(100);
thepoint.y := random(100);
thepoint.z := random(100);
writeln(inttostr(thepoint.x)+' ,'+inttostr(thepoint.y)+' ,'+inttostr(thepoint.x));
end;

begin
lol
end.

Edit:
Thank you for good tutorial

nielsie95
01-05-2008, 03:31 PM
It's a good start :)
Now get some sleep ;)

unrealman
07-14-2008, 11:21 AM
Hey i understood what you were talking about but i still dont understand where all of those things would go in a script. Like im confused... can ne1 help me out?

horizon
03-07-2012, 09:14 AM
nice tut

BraK
03-07-2012, 09:49 AM
You should probably look in the not outdated tutorial for more relevant information. You should also take care to not post spam on topics.