• (very) Simple module for improved convars!
    8 replies, posted
The current convar system is a little wonky and hard to use, but the idea of a server/clientside variable that is easy to save across leaving/joining and stopping/restarting is quite a good one, so I decided to write a little library that would do that for you. It's very simple, only one file and about 200 lines of code, but I tried to make it very easy to use. the library is called 'setting', and is used like this: [CODE] setting.Set( "my_setting", 200 ) --the setting now has a value of 200 local my_setting = setting.Get( "my_setting", -1 ) --the variable now has a value of 200, but would be -1 if the setting was not found print( setting.Exists( "my_setting" ) ) --prints true setting.Sync( "my_setting" ) --any client that joins or is currently connected will have this setting sent to them and updated, as well as anytime the setting changes hook.Add( "SettingChangeCallback", "my_callback", function( name, old_value, new_value ) --anytime a setting is changed, this hook is called print( name, old_value, new_value ) end ) setting.Remove( "my_setting" ) --this setting no longer exists for k,v in ipairs( player.GetAll() ) do print( v:GetSetting( "my_setting", -1 ) ) --prints the value of the setting of each connected client or -1 if it doesn't exist end for k,v in ipairs( player.GetAll() ) do v:SetSetting( "my_setting", 100 ) --sets the value of my_setting to 100 for each connected client end [/CODE] There's also a Readme.txt that explains basically the same thing, as well as documentation within the code as well. [url]https://www.dropbox.com/s/jlf7ujbnvizv2gf/settings.zip[/url] edit:fixed small bug. If you downloaded it and got an error upon trying to sync a setting, redownload it and use that version edit2: added a default option for setting.Get if the setting is not found edit3: added console command for each setting as it's created edit4: added ply:GetSetting and ply:SetSetting
Is "cvar 0/1" really too tough? The only way I can think of simplifying that is by removing the space.
[url]http://wiki.garrysmod.com/page/ConVars[/url] Not sure what you mean by "cvar 0/1" Maybe we're talking about different things, because I'm talking about the current ConVar system in Gmod. Having to retrieve convars by knowing their data type ahead of time has, in the past, forced me to unnecessarily complicate some code. Combined with the fact the FCVAR_REPLICATED and FCVAR_ARCHIVE have historically had issues, I decided to write my own, and share it. Additionally, my settings addon has the ability to store any data type, including tables. The current convar system can only store strings and numbers, and you have to specify which you are retrieving.
But these don't show up in the console, so there is no point in even having this as a convar replacement. (Console Variable means having a variable that can be changed via the console) This is just a thing to store data in a text file now and send it to the client. (why is there no way to send my settings to the server btw) Nice documentation though.
Oh. I'm silly. I'll change it to also create a console command for server convars. The reason that you can't change server settings from the client is because that's easily abusable. Any connected client would be able to change server settings with a single line of code.
Player:GetSetting( name_of_one_declared_on_client, default_value ) Player:SetSetting( name_of_setting, value ) These seem more of a replacement for the Global* functions at the moment.
Well I've added the fact that each setting now comes with a console command. If a setting is synced, the client cannot change the serverside version of the setting edit: [QUOTE=Kogitsune;44681159]Player:GetSetting( name_of_one_declared_on_client, default_value ) Player:SetSetting( name_of_setting, value ) These seem more of a replacement for the Global* functions at the moment.[/QUOTE] That already exists in the form of PData
No, there's Player:GetInfo( name ) which lets you fetch client convars from the server. If the client could have their own settings, you'd want to use a simple method like that to access / control them from the server.
Oh, I see. I'll add that also. [editline]29th April 2014[/editline] Okay, I've added ply:GetSetting and ply:SetSetting
Sorry, you need to Log In to post a reply to this thread.