Forum

> > CS2D > Scripts > Variable table
Forums overviewCS2D overview Scripts overviewLog in to reply

English Variable table

3 replies
To the start Previous 1 Next To the start

old Variable table

Ace Howl
User Off Offline

Quote
Hello.

I already understand how array actually works, but not this kind of table.
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
function array(d)
     local array = {}
          for i = 1,d do
          array[i] = 0
     end
     return array
end 

player = {health = array(32),armor = array(32),speed = array(32)}

addhook("always","aalways")
addhook("second","ssecond")
addhook("leave","lleave")

function ssecond()
     for _, id in pairs(player(0,'table')) do
          player.money[id] = player.money[id] + 10
          uphud(id)
     end
end

function lleave(id)
	player = {}
end

function uphud(id)
     parse('hudtxt2 '..id..' 1 "©000255000Money '..player.money[id]..'" 510 400 0')
     parse('hudtxt2 '..id..' 2 "©000255000Armor '..player.armor[id]..'" 510 420 0')
     parse('hudtxt2 '..id..' 3 "©000255000Speed '..player.speed[id]..'" 510 440 0')
end
As example, Im playing this table to understand it. However, it come out with having no clue. The leave function is my expectation that content inside player table will reset.

So, any suggestions?

old Re: Variable table

Rainoth
Moderator Off Offline

Quote
Yes that's correct. All values inside table will be reset to nothing. That doesn't make much sense because you'd normally want to reset the values only for the person who left rather than everyone.
Also, naming the table 'player' is not good because there's already a table named 'player' and CS2D makes it. So you're effectively replacing any information about players you could get by using that table. I'd suggest to use 'Player' (with a capital P), 'pl' or just any other word to refer to the player like 'user' and so on.

To make it more understandable. Arrays are like tables except they already get 'filled'. You could do
1
MyArray = array(32)
and that would set 'MyArray' to be a table with 32 entries of '0' like
1
MyArray = {[1] = 0,[2] = 0,[3] = 0,--[[and so on and on until]][32] = 0}
It's not practical as you're just assigning values when there's no need. CS2D servers rarely have 32 players so it's much more practical to set the value when there's a new player:
1
2
3
4
5
6
7
8
9
addhook("team","ye")
function ye(id,t)
	MyArray[id] = 0
end

addhook("leave","yey")
function yey(id)
	MyArray[id] = nil
end
This way on a server with 4 people you'd have a table
1
MyArray = {0,0,0,0}
rather than
1
MyArray = {0,0,0,0,0,0,0,0,0,0,...,0}
edited 1×, last 08.06.15 07:30:01 pm

old Re: Variable table

Apache uwu
User Off Offline

Quote
I like to think of arrays (or "tables" in lua) as a file hierarchy.

Your player array would look like this:

1
player = {health = array(32),armor = array(32),speed = array(32)}

IMG:https://i.stack.imgur.com/4YYy8.png


All folders would contain 32 files.

So, by running this line of code:

1
player = {}

You're deleting everything within the player folder (deletes all health, armor and speed).

IMG:https://i.stack.imgur.com/9QflU.png


A better structure would be having every player contain the three properties (health, armor, speed).

1
2
3
4
player = {}
for i = 1, 32 do
    player[i] = {health = 100, armor = 100, speed = 1}
end

IMG:https://i.stack.imgur.com/4EVtX.png


Now when a player leaves, you can simply remove their data as such:

1
player[id] = {} --or reset to defaults like {health = 100, armor = 0, speed = 1}

To get their health, armor, and speed, just use player[id].health, player[id].armor, and player[id].speed respectively.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview