• Pulling variables from table with different names with for i
    8 replies, posted
Hello, I am trying to make a customizable button layout for one of my projects I'm remaking. I'm wanting to have the button read the text variable and the command variable from a table yet it's kinda confusing how to do that. Code: (it's a little hacky with the context menu) [CODE] if SERVER then return end function FBinds() mainframe = mainframe or NULL if mainframe:IsValid() then return end FBindsConfig = { --BEGIN COMMANDS CONFIG cb = 15, text1 = "CTP", cmd1 = "ctp", text2 = "CTP Menu", cmd2 = "ctp_toggle_menu", text3 = "", cmd3 = "", text4 = "", cmd4 = "", text5 = "", cmd5 = "", text6 = "", cmd6 = "", text7 = "", cmd7 = "", text8 = "", cmd8 = "", text9 = "", cmd9 = "", text10 = "", cmd10 = "", text11 = "", cmd11 = "", text12 = "", cmd12 = "", text13 = "", cmd13 = "", text14 = "", cmd14 = "", text15 = "", cmd15 = "", --START PAC EVENTS CONFIG pb = 15, etxt1 = "", evnt1 = "", etxt2 = "", evnt2 = "", etxt3 = "", evnt3 = "", etxt4 = "", evnt4 = "", etxt5 = "", evnt5 = "", etxt6 = "", evnt6 = "", etxt7 = "", evnt7 = "", etxt8 = "", evnt8 = "", etxt9 = "", evnt9 = "", etxt10 = "", evnt10 = "", etxt11 = "", evnt11 = "", etxt12 = "", evnt12 = "", etxt13 = "", evnt13 = "", etxt14 = "", evnt14 = "", etxt15 = "", evnt15 = "" } if not file.Exists("FAddons/fbinds_config.txt","DATA") then file.Write("FAddons/fbinds_config.txt",util.TableToJSON(FBindsConfig)) end if file.Exists("FAddons/fbinds_config.txt","DATA") then util.JSONToTable(file.Read("FAddons/config.txt","DATA")) end local pos = ScrW()+1 mainframe = vgui.Create("DFrame") mainframe:SetSize(150,ScrH()-32) mainframe:SetTitle("") mainframe:SetDraggable(false) mainframe:ShowCloseButton(false) mainframe:MakePopup() mainframe.Paint = function() draw.RoundedBox(0,0,0,150,ScrH()-31,Color(240,240,240,200)) end mainframe.Think = function() mainframe:SetPos(pos,31) pos = math.Clamp(pos-10,ScrW()-149,ScrW()+1) end local name = vgui.Create("DLabel",mainframe) name:SetPos(5,0) name:SetColor(Color(0,0,0)) name:SetText("FBinds") name:SetFont("FScoreboardLabel") name:SizeToContents() local CatCommands = vgui.Create("DCollapsibleCategory",mainframe) CatCommands:SetPos(0,17) CatCommands:SetSize(mainframe:GetWide(),mainframe:GetTall()/2) CatCommands:SetLabel("Commands") local CmdSPanel = vgui.Create("DScrollPanel",CatCommands) CmdSPanel:SetSize(CatCommands:GetWide(),CatCommands:GetTall()-20) CmdSPanel:SetPos(0,20) for i = 1,FBindsConfig[cb] do local cmdbutton = vgui.Create("DButton",CmdSPanel) cmdbutton:SetPos(5,0+(25*i)) cmdbutton:SetText("placeholder") --text var will go here cmdbutton:SetSize(mainframe:GetWide()-5,24) cmdbutton.DoClick = function() RunConsoleCommand("placeholder") --command var will go here end end end hook.Add("OnContextMenuOpen","FBinds",FBinds) hook.Add("OnContextMenuClose","FBindsClose",function() if mainframe:IsValid() then mainframe:Close() end end) [/CODE] Thanks, Flex
Just do like. [code] local mytable = {}; mytable['cmd'] = "Text" [/code]
[QUOTE=crazyscouter;45664571]Just do like. [code] local mytable = {}; mytable['cmd'] = "Text" [/code][/QUOTE] Then how would I save the variables to my config file?
Like you already are
[QUOTE=crazyscouter;45664604]Like you already are[/QUOTE] So would I put this in my for i area or does it not matter?
What? Do you mean this [code] for i = 1,FBindsConfig[cb] do local cmdbutton = vgui.Create("DButton",CmdSPanel) cmdbutton:SetPos(5,0+(25*i)) cmdbutton:SetText("placeholder") --text var will go here cmdbutton:SetSize(mainframe:GetWide()-5,24) cmdbutton.DoClick = function() RunConsoleCommand("placeholder") --command var will go here end end[/code] if so you'd do something like this instead [code] for k,v in pairs(FBindsConfig) do local cmdbutton = vgui.Create("DButton",CmdSPanel) cmdbutton:SetPos(5,(25*k)) cmdbutton:SetText(v) --text var will go here cmdbutton:SetSize(mainframe:GetWide()-5,24) cmdbutton.DoClick = function() RunConsoleCommand(k) --command var will go here end end [/code]
When I try to do SetPos(5,(25*k)), I'm getting this this error that's getting cut off for some reason. [CODE] Player [6][&#3670;&#1763;&#1756;Flex (LUModder)] ERR: [Hook] OnContextMenuOpen:FBinds removed: lua/autorun/client/faddons/fbinds.lua:49: bad argument #1 to '__mul' (string expected, got number) stack traceback: [C]: in function 'error' lua/helpers/check.lua:35: in function '__check' lua/helpers/check.lua:40: in function 'check' lua/helpers/check.lua:69: in function 'checkstring' lua/helpers/string_extensions.lua:9: in function '__mul' lua/autorun/client/faddons/fbinds.lua:49: in function <lua/autorun/client/faddons/fbinds.lua:3> [C]: in function 'xpcal [/CODE]
because k is a string. just do like local count = 0 outside the loop and multiply by count instead of k. also, be sure to add one at the bottom of the loop
Thanks, just got some spacing issues to fix then it should work.
Sorry, you need to Log In to post a reply to this thread.