• Derma Menu Slider Values?
    10 replies, posted
Hi, if you seen my other thread, you may know I am creating a automatic clear decals+freeze props addon. Anyway, here is my issue. I am not that good with lua but here goes, In my Derma Menu, I have a DNumSlider, the value is representing seconds [CODE]local Slider = vgui.Create( "DNumSlider", frame ) Slider:SetPos( 70, 200 ) Slider:SetSize( 300, 100 ) Slider:SetText( "Cleanup Every" ) Slider:SetMin( 5 ) Slider:SetMax( 3600 ) Slider:SetDecimals( 0 )[/CODE] What I am trying to achieve, is that the clientside derma menu slider can change the value of the ConVar I created: [CODE]CreateConVar( "AutoULXMenu_Timer", 600, { FCVAR_ARCHIVE } )[/CODE] and with that convar value, I want to be able to get the value and use it on the server script: [CODE]timer.Modify("AutoULXFreezeProps", **CONVAR VALUE HERE**,0, function() game.ConsoleCommand( "ulx Freezeprops\n" ) end)[/CODE] How is the best way to get the clientside slider to be able to change the convar value, and the convar value to be used in the server side timer? Thanks Alot
[QUOTE=Moat;51174202]DNumSlider has a SetConVar function that you can use. [URL="https://wiki.garrysmod.com/page/Category:DNumSlider"]Example 1 on wiki[/URL] Then you can use a net message to tell the server to update the timer with the new convar value: [CODE] -- Server util.AddNetworkString( "slider_update_net" ) net.Receive( "slider_update_net", function( len, ply ) if ( ply:IsAdmin() ) then -- Check if the player is an admin or w/e local yourconvar = GetConVar( "yourconvarname" ) -- Update your timer here with yourconvar:GetInt() end end ) -- Client Slider.OnValueChanged = function( n ) net.Start( "slider_update_net" ) net.SendToServer() end [/CODE][/QUOTE] Thanks! This helped a lot, though I've just come across a problem. Not too sure how to set the CVAR up. I need it to be on the server, and the clients to be able to change the value of the CVAR. Currently this is how it is set up on the server: [CODE]CreateConVar( "AutoULXMenu_Timer", 600, { FCVAR_ARCHIVE, FCVAR_REPLICATED, FCVAR_USERINFO } )[/CODE] The server recognises it, but the client shows that it doesn't exist. Though I added the Replicated and User Info flags... -------------- The previous set up I had was putting the CVAR with Archive on both client and server luas, but I realised that they are both acting as seperate cvars, and the server one wasn't changing alongside the client one
[QUOTE=H4MZ4;51176268]Thanks! This helped a lot, though I've just come across a problem. Not too sure how to set the CVAR up. I need it to be on the server, and the clients to be able to change the value of the CVAR. Currently this is how it is set up on the server: [CODE]CreateConVar( "AutoULXMenu_Timer", 600, { FCVAR_ARCHIVE, FCVAR_REPLICATED, FCVAR_USERINFO } )[/CODE] The server recognises it, but the client shows that it doesn't exist. Though I added the Replicated and User Info flags... -------------- The previous set up I had was putting the CVAR with Archive on both client and server luas, but I realised that they are both acting as seperate cvars, and the server one wasn't changing alongside the client one[/QUOTE] Well, I don't think you'll beable to have the client mod the convar directly on their end and have it update, directly on the console(without rcon command of course), so you need to take the net approach. I suggest adding an "Apply" button at the bottom of your panel (below the slider), so that you don't spam the server with requests while sliding the numslider around. so: [CODE] --Client function applyButton:DoClick() net.Start("update_ulx_convar"); net.WriteUInt(slider:GetValue(), 32); net.SendToServer(); end [/CODE] Then on the Server: [CODE] --Server util.AddNetworkString("update_ulx_convar"); net.Receive("update_ulx_convar", function(size, ply) if (ply:IsSuperAdmin()) then --Make sure the player can even send this message. Change this to what ever you need RunConsoleCommand("AutoULXMenu_Timer", net.ReadUInt(32)); ply:ChatPrint("Convar updated.."); timer.Modify("AutoULXFreezeProps", GetConVar("AutoULXMenu_Timer"):GetInt() ,0, function() game.ConsoleCommand( "ulx Freezeprops\n" ) end) end end) [/CODE]
[QUOTE=Brassx;51176391]Well, I don't think you'll beable to have the client mod the convar directly on their end and have it update, directly on the console(without rcon command of course), so you need to take the net approach. I suggest adding an "Apply" button at the bottom of your panel (below the slider), so that you don't spam the server with requests while sliding the numslider around. so: [CODE] --Client function applyButton:DoClick() net.Start("update_ulx_convar"); net.WriteUInt(slider:GetValue(), 32); net.SendToServer(); end [/CODE] Then on the Server: [CODE] --Server util.AddNetworkString("update_ulx_convar"); net.Receive("update_ulx_convar", function(size, ply) if (ply:IsSuperAdmin()) then --Make sure the player can even send this message. Change this to what ever you need RunConsoleCommand("AutoULXMenu_Timer", net.ReadUInt(32)); ply:ChatPrint("Convar updated.."); timer.Modify("AutoULXFreezeProps", GetConVar("AutoULXMenu_Timer"):GetInt() ,0, function() game.ConsoleCommand( "ulx Freezeprops\n" ) end) end end) [/CODE][/QUOTE] Thanks. I've added a new button: [CODE]local buttonset = vgui.Create("DButton" , frame) buttonset:SetPos(400,468) buttonset:SetSize(45,30) buttonset:SetText('Apply')[/CODE] and then the fuction for it: [CODE]function buttonset:DoClick() net.Start("update_ulx_convar"); net.WriteUInt(Slider:GetValue(), 32); net.SendToServer(); end[/CODE] However once the slider is moved, and Apply is pressed, it gives: [CODE] [ERROR] lua/vgui/dnumslider.lua:135: attempt to index field 'Scratch' (a nil value) 1. GetValue - lua/vgui/dnumslider.lua:135 2. DoClick - addons/autoulxcleaner/lua/autorun/client/timerscript.lua:56 3. unknown - lua/vgui/dlabel.lua:218 [/CODE] in the client console.
Can you post the entirety of the relevant code? Most importantly the parts where you create the slider and do anything with it
Clientside - SLIDER: [CODE]local Slider = vgui.Create( "DNumSlider", frame ) Slider:SetPos( 70, 200 ) Slider:SetSize( 300, 100 ) Slider:SetText( "Cleanup Every" ) Slider:SetMin( 5 ) Slider:SetMax( 3600 ) Slider:SetDecimals( 0 )[/CODE] Apply/Set Button: [CODE]local buttonset = vgui.Create("DButton" , frame) buttonset:SetPos(400,468) buttonset:SetSize(45,30) buttonset:SetText('Apply') function buttonset:DoClick() net.Start("update_ulx_convar"); net.WriteUInt(Slider:GetValue(), 32); net.SendToServer(); end[/CODE] ---------------- Serverside - Convar [CODE]CreateConVar( "AutoULXMenu_Timer", 600, { FCVAR_ARCHIVE, FCVAR_REPLICATED, FCVAR_USERINFO } )[/CODE] Net. [CODE]util.AddNetworkString( "update_ulx_convar" ) net.Receive("update_ulx_convar", function(size, ply) if (ply:IsAdmin()) then RunConsoleCommand("AutoULXMenu_Timer", net.ReadUInt(32)); ply:ChatPrint("Convar updated.."); timer.Adjust("AutoULXNotifcation", GetConVar("AutoULXMenu_Timer"):GetInt(),0, function() BroadcastLua("chat.AddText(Color(0,255,0), 'AutoULXCleaner ', Color(255,255,255), '- ', Color(82,82,82), 'Props Frozen + Decals Cleared!')") end) timer.Adjust("AutoULXFreezeProps", GetConVar("AutoULXMenu_Timer"):GetInt(),0, function() game.ConsoleCommand( "ulx Freezeprops\n" ) end) timer.Adjust("AutoULXClearDecals", GetConVar("AutoULXMenu_Timer"):GetInt(),0, function() game.ConsoleCommand( "ulx cleardecals\n" ) end) end end) [/CODE]
That's quite the odd error. It implies that the slider did not initialize properly. Scratch is created in the Init function of the slider, as shown below: [CODE] self.Scratch = self.Label:Add( "DNumberScratch" ) self.Scratch:SetImageVisible( false ) self.Scratch:Dock( FILL ) self.Scratch.OnValueChanged = function() self:ValueChanged( self.Scratch:GetFloatValue() ) [/CODE] But the numslider works just fine? You can drag the slider around and stuff and the displayed value updates?
[QUOTE=Brassx;51177230]That's quite the odd error. It implies that the slider did not initialize properly. Scratch is created in the Init function of the slider, as shown below: [CODE] self.Scratch = self.Label:Add( "DNumberScratch" ) self.Scratch:SetImageVisible( false ) self.Scratch:Dock( FILL ) self.Scratch.OnValueChanged = function() self:ValueChanged( self.Scratch:GetFloatValue() ) [/CODE] But the numslider works just fine? You can drag the slider around and stuff and the displayed value updates?[/QUOTE] Yeah, I added a concommand to print the value of the slider, which works fine. Shows the same value as the slider.
I am just gonna try and revert to a sort of drop down box options having main numbers, eg 1 minute, 5 minutes, 10 minutes etc etc, might not be as interactive as a slider, but if the slider won't work that I'll have to use a different method. Oh Well!
Rather than using the slider, I changed and used the drop down menu... Works normally now.
Sorry, you need to Log In to post a reply to this thread.