Derma Question

The code is supposed to change a players class when clicked. I’m a bit confused on the bit with the for loop and local line.
I think I understand line.ply holds a player, but I’m not sure how the double-click knows which player was clicked.
If someone could explain to me what’s going on with the local line and and line.ply bit that would be appreciated.



-- Clientside

function MyCoolMenu()
	local DermaPanel = vgui.Create( "DFrame" )
	DermaPanel:SetPos( 100,100 )
	DermaPanel:SetSize( 500, 700 )
	DermaPanel:SetTitle( "Make A Cop" )
	DermaPanel:SetVisible( true )
	DermaPanel:SetDraggable( true )
	DermaPanel:ShowCloseButton( true )
	DermaPanel:MakePopup()

	local DermaListView = vgui.Create("DListView")
	DermaListView:SetParent(DermaPanel)
	DermaListView:SetPos(25, 50)
	DermaListView:SetSize(450, 625)
	DermaListView:SetMultiSelect(false)
	DermaListView:AddColumn("Name")

	for k,v in pairs(player.GetAll()) do
		local line = DermaListView:AddLine(v:Nick())
		line.ply = v
	end
	function DermaListView:DoDoubleClick(lineid, line)
		net.Start("Test")
		net.WriteEntity(line.ply)
		net.SendToServer()
	end
end

-- Serverside

util.AddNetworkString("Test")
net.Receive("Test",function(len, ply)
	local target = net.ReadEntity()
	target:changeTeam(TEAM_CITIZEN, true)
end)




for k,v in pairs(player.GetAll()) do
end


Every player is stored in a table on the client.
This loop goes through the entire table like so:



key1 = value1
key2 = value2
key3 = value3
...
keyn = valuen


K is the key, and v is the value associated with that key.
So for every player there is, it gets the player entity (the value) and does the following:



local line = DermaListView:AddLine(v:Nick())
line.ply = v


Firstly, it creates a new local variable.
Then, it creates a new Line to add in the ListView, and sets the value of it to the player’s name.

DermaListView:AddLine() returns the line we just created, so if you store it to a variable like here, you can call upon it again very easily.
That’s what the second part does:



line.ply = v


Now I’m not exactly sure what the best way to explain this is, but this ‘line’ variable can be seen as a table itself, and using ‘.’ you can add more to it.
Think of it as setting the key ‘ply’ of ‘line’ to contain the player object. Key and value.
You can store all kinds of stuff like this.

So now, every line has also got the actual player entity associated with it stored, meaning you can call on it.
You wouldn’t be able to do this with just the name, as that’s merely a string.
The doubleclick function then sends the player entity to the server, so the server will know exactly what player to change the class of.

Hope that helps.

I’m still confused. Wouldn’t line.ply be equal to the last player iterated in the for loop?

No, as you are creating a line for each player, the line corresponding to the player will have that variable, it wont have the last one.

[editline]15th August 2016[/editline]

The first line will have the first player and the last line will have the last player in the line.ply variable.
The 3rd line will have the 3rd player etc.