Arrays
19 replies



06.02.11 01:54:43 am
Hi guys, can someone explain me what is an array, and for what i use it? Thanks for the amswers.
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
Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
...
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.
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
2
3
4
name[1] = 25
name[2] = 12
name[3] = 6
name[4] = 4
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?
let me see if i understand, i write this
But i still dont understand for what i use tables..
Code:
1
2
3
4
2
3
4
Health[1] = 100
Health[2] = 120
Health[3] = 107
Health[4] = 400
Health[2] = 120
Health[3] = 107
Health[4] = 400
But i still dont understand for what i use tables..
You use tables because you don't want to make 100 variables if you can use 1 table.
Code:
1
2
3
4
2
3
4
variable1 = 1
variable2 = 2
...
variable100 = 100
variable2 = 2
...
variable100 = 100
Code:
1
2
3
4
5
2
3
4
5
table = {}
for i = 1,100 do
table[i] = i
end
for i = 1,100 do
table[i] = i
end
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
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"
}
[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"
}
Code:
1
2
3
4
5
6
7
8
9
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.
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
It is Pascal.
Crt lib is pretty useless there.
Crt lib is pretty useless there.
Code:
1
2
3
4
5
6
7
8
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.
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
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.
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.
Thanks 
Edit: is this correct:

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
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
--- 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



