Official Soldat Forums
Server Talk => Scripting Discussions and Help => Topic started 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.
-
0.o
You need arrays to store large ammounts of data. For example for each person in a server. Here is an example:
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:
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.
-
Dude... how could you even ask that question, arrays would have to be the most valuable data type in existence; as demonstrated above.
-
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.
-
You never need to use them, you can just create 32 vars instead of one array, too, if you really want to
-
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?
-
Just look:
Function OnPlayerCommand(ID:Byte; Text:String):Boolean;
begin
if LowerCase(Text) = '/pred' then GiveBonus(ID,1);
Result := false;
end;
-
Since no one really gives an answer..
No you don't need them in simple scripts.
-
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)
-
If I understood Hacktank - I need them in scripts which have to hold information about players itself?
-
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).
-
Ok, thanks you all for input. If I'll have further question I'll post them. :) (Probably in new thread)
-
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...
-
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.