Official Soldat Forums

Server Talk => Scripting Discussions and Help => Topic started by: Furai on June 10, 2010, 03:10:11 am

Title: Arrays -why do we need them?
Post by: Furai 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.
Title: Re: Arrays -why do we need them?
Post by: Hacktank 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.
Title: Re: Arrays -why do we need them?
Post by: EnEsCe 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.
Title: Re: Arrays -why do we need them?
Post by: Furai 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.
Title: Re: Arrays -why do we need them?
Post by: DarkCrusade 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
Title: Re: Arrays -why do we need them?
Post by: Furai 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?
Title: Re: Arrays -why do we need them?
Post by: DarkCrusade 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;
Title: Re: Arrays -why do we need them?
Post by: Swompie on June 10, 2010, 10:06:53 am
Since no one really gives an answer..
No you don't need them in simple scripts.
Title: Re: Arrays -why do we need them?
Post by: dnmr 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)
Title: Re: Arrays -why do we need them?
Post by: Furai 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?
Title: Re: Arrays -why do we need them?
Post by: DarkCrusade 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).
Title: Re: Arrays -why do we need them?
Post by: Furai 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)
Title: Re: Arrays -why do we need them?
Post by: Gizd 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...
Title: Re: Arrays -why do we need them?
Post by: Furai 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.