Author Topic: Arrays -why do we need them?  (Read 1383 times)

0 Members and 1 Guest are viewing this topic.

Offline Furai

  • Administrator
  • Veteran
  • *****
  • Posts: 1908
    • TransHuman Design
Arrays -why do we need them?
« on: June 10, 2010, 03:10:11 am »
After I'll get my notebook back I'm going to sit my ass in front of it and start to script a lil' bit. I've already tried (couple years ago) and  I did not understand why do we need arrays in scripts. Can someone explain it to me and back it up with simple examples of code? Help is really appreciated.
"My senses are so powerful that I can hear the blood pumping through your veins."

Offline Hacktank

  • Camper
  • ***
  • Posts: 462
  • Soldat Scripter
    • HTZRPG
Re: Arrays -why do we need them?
« Reply #1 on: June 10, 2010, 04:19:35 am »
0.o

You need arrays to store large ammounts of data. For example for each person in a server. Here is an example:

Code: [Select]
type plr = record;
level,xp,extrahealth,deathstillkick: integer;
damagemultiplier,mana: single;
end;

var
player: array[1..32] of plr;

procedure foo(ID: byte);
begin
player[ID].level := 9;
player[ID].damagemultiplier := 2.6;
player[ID].deathstillkick := 20;
player[ID].mana := 5.5;
player[ID].xp := 0;
end;

procedure ActivateServer();
var i: byte;
begin
for i := 1 to 32 do foo(i); // calls foo for all players, setting 160 variables all at once
end;

Without arrays you would have to do this:

Code: [Select]
var
p1lvl,p2lvl,p3lvl,p4lvl,p5lvl,p6lvl, (ect)

The most important part of arrays is the ability to use the index

arrayname [ INDEX ];

Which can be a byte variable, allowing you to use it for player ids, file lines, advanced string use. And MANY other things. You cant call yourself a programmer unless you know how to use arrays well.
« Last Edit: June 10, 2010, 04:21:25 am by Hacktank »


Offline EnEsCe

  • Retired Soldat Developer
  • Flamebow Warrior
  • ******
  • Posts: 3101
  • http://enesce.com/
    • [eC] Official Website
Re: Arrays -why do we need them?
« Reply #2 on: June 10, 2010, 04:31:06 am »
Dude... how could you even ask that question, arrays would have to be the most valuable data type in existence; as demonstrated above.

Offline Furai

  • Administrator
  • Veteran
  • *****
  • Posts: 1908
    • TransHuman Design
Re: Arrays -why do we need them?
« Reply #3 on: June 10, 2010, 09:31:25 am »
But do I need them in really basic scripts?

And I could ask this question. We live in (almost) free world. :) When I do not understand - I ask. Is something wrong with it? And my question concerned soldat's scripting. I do know that arrays are very important and make programmer's life much more easier and intersting.
"My senses are so powerful that I can hear the blood pumping through your veins."

DarkCrusade

  • Guest
Re: Arrays -why do we need them?
« Reply #4 on: June 10, 2010, 09:51:22 am »
You never need to use them, you can just create 32 vars instead of one array, too, if you really want to

Offline Furai

  • Administrator
  • Veteran
  • *****
  • Posts: 1908
    • TransHuman Design
Re: Arrays -why do we need them?
« Reply #5 on: June 10, 2010, 09:54:48 am »
Nah, array will be easier to do. :) I ask you if every single script needs an array.
Now I'll give you an lame example  - after someone types "/pred" ingame he gets predator bonus. Does this basic script needs anything like array?
"My senses are so powerful that I can hear the blood pumping through your veins."

DarkCrusade

  • Guest
Re: Arrays -why do we need them?
« Reply #6 on: June 10, 2010, 10:00:59 am »
Just look:

Code: (pascal) [Select]
Function OnPlayerCommand(ID:Byte; Text:String):Boolean;
  begin
    if LowerCase(Text) = '/pred' then GiveBonus(ID,1);
    Result := false;
  end;

Offline Swompie

  • Camper
  • ***
  • Posts: 390
Re: Arrays -why do we need them?
« Reply #7 on: June 10, 2010, 10:06:53 am »
Since no one really gives an answer..
No you don't need them in simple scripts.

Offline dnmr

  • Camper
  • ***
  • Posts: 315
  • emotionally handicapped
Re: Arrays -why do we need them?
« Reply #8 on: June 10, 2010, 10:13:51 am »
this thread actually amuses me.
Don't use em if you don't want to, nobody's going to make you do it... (we'll just laugh, that's all)

Offline Furai

  • Administrator
  • Veteran
  • *****
  • Posts: 1908
    • TransHuman Design
Re: Arrays -why do we need them?
« Reply #9 on: June 10, 2010, 10:14:30 am »
If I understood Hacktank - I need them in scripts which have to hold information about players itself?
"My senses are so powerful that I can hear the blood pumping through your veins."

DarkCrusade

  • Guest
Re: Arrays -why do we need them?
« Reply #10 on: June 10, 2010, 10:24:54 am »
If you need to save stats for players you'll need to use types and arrays (a type is an array which consists of more arrays).

Offline Furai

  • Administrator
  • Veteran
  • *****
  • Posts: 1908
    • TransHuman Design
Re: Arrays -why do we need them?
« Reply #11 on: June 10, 2010, 12:39:52 pm »
Ok, thanks you all for input. If I'll have further question I'll post them. :) (Probably in new thread)
"My senses are so powerful that I can hear the blood pumping through your veins."

Offline Gizd

  • Flagrunner
  • ****
  • Posts: 586
  • (Re)tired
    • Eat-this! community site
Re: Arrays -why do we need them?
« Reply #12 on: June 11, 2010, 08:02:56 am »
Code: [Select]
var Kills1, Kills2, Kills3, Kills4, Kills6, Kills7, Kills8, Kills9, ..., Kills32: byte;

procedure OnPlayerKill(Shooter, Victim: byte; Weapon: string);
begin
  if Shooter = 1 then Kills1:= Kills1 + 1
  else if Shooter = 2 then Kills2:= Kills2 + 1
  else if Shooter = 3 then Kills3:= Kills3 + 1
  else if Shooter = 4 then Kills4:= Kills4 + 1
  else if Shooter = 5 then Kills5:= Kills5 + 1
  else if Shooter = 6 then Kills6:= Kills6 + 1
  ...
  else if Shooter = 32 then Kills32:= Kills32 + 1;
end;
Isn't it beautiful?

edit: Btw, don't make thread for each problem...
« Last Edit: June 11, 2010, 08:08:09 am by Gizd »

Offline Furai

  • Administrator
  • Veteran
  • *****
  • Posts: 1908
    • TransHuman Design
Re: Arrays -why do we need them?
« Reply #13 on: June 11, 2010, 09:02:50 am »
You don't have to be sarcastic and show me how would look script without an array. I just asked when do we need them.
"My senses are so powerful that I can hear the blood pumping through your veins."