English Arrays

19 replies
Goto Page
To the start Previous 1 Next To the start
06.02.11 01:54:43 am
Up
J4x
User
Offline Off
Hi guys, can someone explain me what is an array, and for what i use it? Thanks for the amswers.
06.02.11 02:21:52 am
Up
Yasday
User
Offline Off
An array is used to store multiple data. Like money for every player on the server, so you won't need 32 variables for 32 players. Let's say it's just a table.
edited 1×, last 06.02.11 02:49:35 am
06.02.11 02:53:36 am
Up
J4x
User
Offline Off
Can u give me a example? Thanks for the answer.
06.02.11 03:02:40 am
Up
Yasday
User
Offline Off
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function array_of_money()
     local tbl = {}
     for pl = 1,32 do
          if player(pl,"exists") then
               tbl[pl] = player(pl,"money")
          else
               tbl[pl] = 0
          end
     end
     return tbl
end

money = array_of_money()

money[1] = money of the first player
...

An array is a table.
06.02.11 03:17:48 am
Up
J4x
User
Offline Off
I dont want to be anoying but can u explain what si a table.. lol.
06.02.11 03:21:57 am
Up
Yasday
User
Offline Off
Well.. this
Code:
1
me = 1
isn't a table, but this
Code:
1
me = {[1] = 1}
is a table.
06.02.11 03:45:42 am
Up
Unknown_Soldier
User
Offline Off
FN_Linkin Park has written:
I dont want to be anoying but can u explain what si a table.. lol.


The table is an array, and that is like a variable, but it stores more than 1 value, to create an empty table, you need this

Code:
1
name = {}


you can write the values by yourself by 2 ways

Code:
1
name = {25,12,6,4}


that is like I write

Code:
1
2
3
4
name[1] = 25
name[2] = 12
name[3] = 6
name[4] = 4


this is usefull to make a variable to every player in the server, like money, lives, HP, armor, etc.
Should I come back and make one last map for CS2D?
06.02.11 03:54:10 am
Up
J4x
User
Offline Off
let me see if i understand, i write this
Code:
1
2
3
4
Health[1] = 100
Health[2] = 120
Health[3] = 107
Health[4] = 400

But i still dont understand for what i use tables..
06.02.11 04:00:40 am
Up
Yasday
User
Offline Off
You use tables because you don't want to make 100 variables if you can use 1 table.
Code:
1
2
3
4
variable1 = 1
variable2 = 2
...
variable100 = 100


Code:
1
2
3
4
5
table = {}

for i = 1,100 do
     table[i] = i
end
06.02.11 04:04:06 am
Up
J4x
User
Offline Off
oh, i think i understand. so this is a table :
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
TileMaterial = {
     [0] = "Soundless",
     [1] = "Wall",
     [2] = "Obstacle",
     [3] = "Wall (No Shadow)",
     [4] = "Obstacle (No Shadow)",
     [10] = "Dirt",
     [11] = "Snow",
     [12] = "Step",
     [13] = "Tile",
     [14] = "Water",
     [15] = "Metal",
     [16] = "Wood",
     [50] = "Deadly - Normal",
     [51] = "Deadly - Explosion",
     [52] = "Deadly - Toxic",
     [53] = "Deadly - Abyss"
     }
06.02.11 04:34:29 am
Up
Yasday
User
Offline Off
Yes it is.
06.02.11 04:38:49 am
Up
J4x
User
Offline Off
ok,thanks.
06.02.11 11:03:43 am
Up
oxytamine
User
Offline Off
Code:
1
2
3
4
5
6
7
8
9
program test; uses crt;
var
X:array [1..5] of integer; 
i:byte;
begin
clrscr;
for i:=1 to 5 do 
read (X[i]);
end.
edited 1×, last 07.02.11 01:23:12 pm
07.02.11 01:59:49 am
Up
J4x
User
Offline Off
Oxytamine, thats chinese for me ...
07.02.11 09:29:38 am
Up
Banaan
User
Offline Off
That's because it isn't Lua and therefore pretty useless here. I think it's Pascal, ain't it?
07.02.11 11:32:53 am
Up
SQ
Moderator
Offline Off
It is Pascal.
Crt lib is pretty useless there.

Code:
1
2
3
4
5
6
7
8
program Test;
var X : Array[1..5] of Char;
     Y : Byte;
begin
     for Y := 1 to 5 do X[Y] := Char(Random(255));
     for Y := 1 to 5 do WriteLn(X[Y]);
     ReadLn;
end.
edited 1×, last 07.02.11 11:42:56 am
07.02.11 01:24:45 pm
Up
oxytamine
User
Offline Off
BlazingEyed, I usually use crt library just to clean screen. So I just put it in every TP7 program I make, even if it's example like I gave him.
19.02.11 08:10:42 pm
Up
J4x
User
Offline Off
Well, can some one explain me what does
Code:
1
initArray(32)
means?
19.02.11 08:13:15 pm
Up
Lee
Moderator
Offline Off
It creates a table with 32 indices (1 to 32) and fills them with 0. In most cases you do not need them, it was carried over from expectations I had from other programming languages.
19.02.11 08:17:42 pm
Up
J4x
User
Offline Off
Thanks

Edit: is this correct:
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
----------------------------------------
--- Effects script by FN_Nemesis 
--- for more info visit: Unrealsoftware.de
--- Date: 11/2/2011
----------------------------------------
Weapon tables = {
[Machinegun] = "40",
[rifles] = "30,31,32,33,38,39",
[snipers] = "34,35,36,37",
[pistols] = "1,2,3,4,5,6",
[Smg] = "20,21,22,23,24",
[Shotguns] = "10,11"}

addhook("attack","shake")
function shake(id)
if wpn == Machinegun then
parse("shake "..id.." 8")
end
end

addhook("attack","shake2")
function shake2(id)
if wpn == rifles then
parse("shake "..id.." 3")
end
end


addhook("attack","shake3")
function shake3(id)
if wpn == snipers then
parse("shake "..id.." 5")
end
end


addhook("attack","shake4")
function shake4(id)
if wpn == pistols then
parse("shake "..id.." 2")
end
end

addhook("attack","shake5")
function shake5(id)
if wpn == Smg then
parse("shake "..id.." 6")
end
end

addhook("attack","shake6")
function shake6(id)
if wpn == Shotguns then
parse("shake "..id.." 4")
end
end

addhook("hit","shake7")
function shake7(id)
parse("shake "..id.." 10")
end
edited 3×, last 19.02.11 09:06:30 pm
To the start Previous 1 Next To the start