Running a command on selected row with DListView.OnRowSelected?
4 replies, posted
Hey ya'll i'm new here so sorry for my "noobiness" but i'm stuck on something. I've been getting more in-depth with Derma and trying new things with it. The problem is that i'm trying to "do" something to a person I select through DListView via OnRowSelected. So if I select a player on the row a button comes up that's pretty simple. I want the button to kill the player when I click it (just something simple to test) though. I don't know how to do that.
Here's my code. No, I'm not looking for someone to code for me (although that'd be really neat), just need some advice or to be pointed in the right direction. I've looked everywhere and can't find out what to do. I don't even know if this is possible. I know how to do something when I select a row such as create a button once you select a row, but I can't figure out how to "do" something to a player of the row I selected. I'll try to use comments in the code to help. Sorry, I'm horrible at explaining things.
[lua]local Players = vgui.Create( "DListView", Main)
Players:SetMultiSelect ( false )
Players:SetPos ( 5,5 )
Players:SetSize ( 700,300 )
Players:AddColumn ( "Steam" )
Players:AddColumn ( "Name" )
for k, v in pairs(player.GetAll()) do
Players:AddLine ( v:SteamID(), v:Nick() )
end
-- The above doesn't matter.
Players.OnRowSelected = function ( panel, line )
print( Players:GetLine(line):GetValue(1) ) -- Just to see if I didn't fuck it up and it's working.
-- Create a button when a row is selected. Pretty simple.
Button1 = vgui.Create("DButton", Players )
Button1:SetPos(0,0)
Button1:SetSize(50,50)
Button1:SetText("Kill Player")
Button1.DoClick = function()
-- Now, here's the problem I run into.
-- I can't for the life of me figure this out.
-- Trying to make it do something on the selected player.
-- This is the most recent one I've tried.
-- I've tried so many ways and it's not working.
for k, v in pairs (Players:GetLine(line):GetValue(1)) do
print ("please work already")
end
end[/lua]
I'd really appreciate any help. Thanks!
Don't create the button every time player clicks something, it's a bad idea. Create one button, save command from the list in a local variable and execute that command from your button.
You also didn't end the doclick.
[QUOTE=Robotboy655;42162255]Don't create the button every time player clicks something, it's a bad idea. Create one button, save command from the list in a local variable and execute that command from your button.[/QUOTE]
Ahh, you're right! Good idea I'll get on that right now.
@Acecool I did, but it accidentally left it out when I posted.
[editline]12th September 2013[/editline]
Here is the new one I made. The creating a button when you select a row was a bad idea. Makes things more confusing too. Thanks for the advice on that! So, here is the new one. The derma works and it works fine. The problem is I can't figure out how to do this:
Select a line (AKA player) > Click button > On button click, kill the selected player. I tried to look up and play around with strings, but I can't get it to work. As in like extracting a string from the selected row and comparing it to all players and if it matches kill that player. That's the only thing that I could think up of that would make it work. That didn't work though. Or I'm too stupid to figure out how to do it.
Here's my code:
[lua]
function Test ( )
local base = vgui.Create ( "DFrame" )
local panel = vgui.Create ( "DPanel", base )
local dlist = vgui.Create ( "DListView", panel)
local button = vgui.Create("DButton", panel)
base:MakePopup ( )
base:SetVisible ( true )
base:SetPos ( ScrW()/2 - 360 , ScrH()/2 - 300)
base:SetSize ( 800 , 600 )
base:SetTitle ( "Window" )
base:SetDraggable ( true )
base:ShowCloseButton ( true )
base:SetSizable ( true )
panel:SetPos ( 5 , 30 )
function panel:Think ( )
panel:SetSize ( base:GetWide( ) - 10 , base:GetTall( ) - 35)
end
dlist:SetMultiSelect ( false )
dlist:SetPos ( 5 , 5 )
function dlist:Think ( )
dlist:SetSize ( panel:GetWide( ) - 10 , panel:GetTall( ) - 100)
end
dlist:AddColumn ( "Steam" )
dlist:AddColumn ( "Name" )
for k, v in pairs ( player.GetAll( ) ) do
dlist:AddLine ( v:SteamID( ) , v:Nick( ) )
end
dlist.OnRowSelected = function ( dlist, line )
print ( dlist:GetLine(line):GetValue(2) )
end
button:SetSize ( 200 , 40 )
button:SetPos ( panel:GetWide( ) + 230 , panel:GetTall( ) + 475 ) --Yes, I know it's not a think function. Not worried about it right now.
button:SetText ( "Kill Selected Player" )
button.DoClick = function ( )
print ("yay")
RunConsoleCommand ("something")
end
end
usermessage.Hook ("Window", Test)
function pleasework ( ply, cmd, arg )
print ("too stupid to figure it out") --Yeah, no clue what to do here.
end
concommand.Add ( "something" , pleasework )
[/lua]
I've been playing around with the String library for a couple days now, but I don't think it's possible to accomplish what I want with it. Since you can only use "" strings I can't for example get all players and compare that to the player that was selected via clicking the row (or I'm just stupid). It sucks because that was the only thing I could think of that would work. Get the player you click on. Compare that with all the players on the server and if they match kill that player. I can't think of any other way.
This is what I messed around with. It "works", but not for what I need:
[lua]
function pleasework ( ply, cmd, arg )
local find = "TheLuaNoob"
if ( string.match (find , "NotThat!" ) ) then
print( "It works!" )
else
print( "You fucked up again." )
end
end
concommand.Add ( "something" , pleasework )
[/lua]
Obviously this doesn't even matter as it checks if the local "find" matches with what's in the strings of the string.match.
Sorry, you need to Log In to post a reply to this thread.