PDA

View Full Version : Arrays ~ Great for saving time



mat_de_b
10-08-2007, 06:50 PM
This is more of an intermediate tutorial but since i dont think non-members can see them i thought i would post it here... need a fair bit of prior knowledge, including Setting up variables, for to do clauses <- not 100% necessary, findcolorspirals and a general understanding...

1. Background Info
What is an array you ask?
I like to think of it as a way of storing many variables to one name...

How is this useful?
When finding a NPC for instance this will life far easier and more proffesional... all i can say is lucky you!

2. Our first Array

How does one go about making an array?
Like any other variable an array must be called at the begining of a script or procedure like this


var
Color : array [0..4] of Integer;

This allows us to store 5 values to the variable Color. The first number here 0 is the first value that can be stored and the second number here 4 is the last value that can be set

How do we set values in our Array?
Arrays are indexed using a number in square brackets after the array name, like so:

Color[0] = 12345;

This sets the first value in our array to 12345 but the others remain unchanged to change them all one would use:

program New;
var
Color : array [0..4] of Integer;

begin
Color[0] := 12345;
Color[1] := 1234;
Color[2] := 123;
Color[3] := 12;
Color[4] := 1;
end.
3. How does a l33th4ax0r such as yourself use this to find an NPC?
Now that we have set our colors we must look for each one in order to find the NPC, now we could write out a million find colors? but here is the promise posed by an array.

program New;
{.include srl/srl.scar}
var
Color : array [0..4] of Integer;
i,x,y : integer;
Found : boolean;

procedure LoadColors;
begin
Color[0] := 12345;
Color[1] := 1234;
Color[2] := 123;
Color[3] := 12;
Color[4] := 1;
end;

begin
LoadColors;
For i:= 0 to 4 do
begin
if(FindColorSpiral(x,y,Color[i],MSX1,MSY1,MSX2,MSY2))then
begin
Found := True;
Writeln('Found the color you speak of');
Break;
end;
end;
if(Found = False)then
Exit;
end.

For each repetition of this I will be increased until it equals 4 (the last value of our Array) this means that it searches for each color 1 after another, then if it finds the color it will break the loop meaning that you get the correct x and y :D Happy us

4.Taking this that one step further!

Now you say i like what your telling me but surely theres more to it? and there is! here is how to edit your array on run time and allow yourself to get the correct colors!

First we establish our array and look for it with a tolerance doing almost the same as in 3.

program New;
{.include srl/srl.scar}
var
Color : array of Integer;
i,x,y : integer;
Found : boolean;

procedure LoadColors;
begin
SetArrayLength(Color,5);
Color[0] := 12345;
Color[1] := 1234;
Color[2] := 123;
Color[3] := 12;
Color[4] := 1;
end;

begin
LoadColors;
For i:= 0 to 4 do
begin
if(FindColorSpiralTolerance(x,y,Color[i],MSX1,MSY1,MSX2,MSY2,15))then
begin
Found := True;
Writeln('Found the color you speak of');
Break;
end;
end;
if(Found = False)then
Exit;
end.

However this time we want to add the color to our array! we therefor 2must add length to our array and store a color to it, this is how its done!


SetArrayLength(Color,GetArrayLength(Color) +1); // This increases the amount of values Color an handle
Color[GetArrayLength(Color) - 1] := GetColor(x,y); // This makes the largest value of Color equal the colour you just found!
// The -1 is used becuase the starting value of the array is 0 and so all values will be 1 lower than the total length...

And this is how that is used in a script:

program New;
{.include srl/srl.scar}
var
Color : array of Integer;
i,x,y : integer;
Found : boolean;

procedure LoadColors;
begin
SetArrayLength(Color,5);
Color[0] := 12345;
Color[1] := 1234;
Color[2] := 123;
Color[3] := 12;
Color[4] := 1;
end;

begin
LoadColors;
For i:= 0 to (GetArrayLength(Color) - 1) do
begin
if(FindColorSpiralTolerance(x,y,Color[i],MSX1,MSY1,MSX2,MSY2,15))then
begin
SetArrayLength(Color,GetArrayLength(Color) +1); // This increases the amount of values Color an handle
Color[GetArrayLength(Color) - 1] := GetColor(x,y); // This makes the largest value of Color equal the colour you just found!
Found := True;
Writeln('Found the color you speak of');
Break;
end;
end;
if(Found = False)then
Exit;
end.

This code will add all new found colors to the Array and will then look for all of the colors...


Hope you enjoyed this tutorial and i hope its beneficial, if i've left you a bit :confused: then feel free to pm/comment here and ill help you out...

ShowerThoughts
10-08-2007, 06:51 PM
w00tness, great! but i am to lazy to read :p

Da Der Der
10-08-2007, 11:48 PM
Most definitely helped me. I have been gone for a long time (5 months to be exact) and I came back yesterday and now I'm reading all the tuts again and I'm starting to script again :) Thanks! Great tut!

- Da Der Der -

EvilChicken!
10-23-2007, 08:11 PM
w00tness, great! but i am to lazy to read :p

So, basically what you are doing is that you spam and try to get your post count up by saying this tut, wich you never read, is good. Nice.



Back on topic:
Gj, guess this is understandable. Thanks. You COULD try to go a little more in-depth, idk, maybe add an exaple script wich uses arrays. Thanks.

ShowerThoughts
10-23-2007, 08:22 PM
no that means i already understand it :)

Dan Cardin
10-23-2007, 08:27 PM
So, basically what you are doing is that you spam and try to get your post count up by saying this tut, wich you never read, is good. Nice.



Back on topic:
Gj, guess this is understandable. Thanks. You COULD try to go a little more in-depth, idk, maybe add an exaple script wich uses arrays. Thanks.

i personally think my tut goes more into detail :p(no offense to mat) but mine doesnt give the in-script example that you want

which his does by the way-finding an npc...

Tri
11-02-2007, 01:51 PM
Hey, thanks for the array tutorial - goooooood :)

n3ss3s
11-02-2007, 02:15 PM
Dude, arrays of colors if the obj is not multi-coloured like purple, blue, green are out. Tolerance and CTS(2) FTW!

Good tutorial though, BUT for this purpose, you dont necassarily need an array ;)

Case I of :)

Trifonius
01-06-2008, 04:46 PM
Thx for this tutorial! helpful!

Pure1993
02-08-2008, 04:14 PM
Thanks for this tutorial, now I finally seem to understand arrays! :D
I only have a little question, could you use strings instead of integers? For example like this:

Var
EntTree: array [0..3] of String;
begin
EntTree[0] := 'Normal';
EntTree[1] := 'Oak';
EntTree[2] := 'Willow';
EntTree[3] := 'Yew';
end;

Thanks a lot :)

Negaal
02-08-2008, 04:55 PM
Thanks for this tutorial, now I finally seem to understand arrays! :D
I only have a little question, could you use strings instead of integers?

Well, if you meant "can I use strings instead of integers" then I say you can...You can use any variable in arrays...

Nice tut

Though it could be little hard for peoples who try to understand it first time...

Pure1993
02-08-2008, 05:52 PM
Thanks for your help Negaal. :)

Jewish Zombeh
02-08-2008, 07:48 PM
Thank you for posting this, I am learning so much so fast from this site about SCAR.

0wn 4 skill
08-08-2008, 12:14 AM
Thanks. Really Helped me alot :)

m4g3owns
08-14-2008, 06:50 AM
Wow best array tut ive found that works good for me :) nice job

happyme
08-19-2008, 05:25 PM
One thing:

The index numbering starts from 0 (his example had an index 0, but it wasn't stated).

mightezezima
09-10-2008, 06:51 PM
this will help meh :)

crunkjmp007
10-20-2008, 07:09 PM
Wow thanks a lot the arrays will definetly aid me when attempting to script

shynie
10-27-2008, 07:59 AM
Thanks :)

This could be really useful for finding things.. shame my script isn't advanced enough for it just yet. :]

designed by
12-22-2008, 11:52 PM
Thanks! I Finally understand arrays you made it a lot simplier than other tuts.

Jmcver
02-02-2009, 02:08 AM
Okay ya thanx for this tut i think i understand arrays alot better, and u said u could use strings like the trees, so i could do it with monsters to then right??

Hot Script
03-07-2009, 02:21 AM
definitely what i needed for my bow string script. :)

Ogre
03-11-2009, 02:49 AM
Great Tutorial. Do you have one TStringArrays? I've seen these before and would like to know how to use them.