S&box Wiki

Revision Difference

ClientData_ConVars#551274

<cat>Code.Misc</cat> <title>ClientData ConVars</title> A client data convar is like a replicated convar, except instead of being replicated from the server to the clients, it's replicated from each client to the server. When the value changes on the client it is sent to the server. These values are accessible on the server for each client using [Client.GetClientData( "var_name" )](https://asset.party/api/Sandbox.IClient.GetClientData(string,string)). # Defining ``` [ConVar.ClientData( "tool_current" )] public static string UserToolCurrent { get; set; } = "boxgun" ``` # Accessing ``` // On the client you can access it directly currentTool = UserToolCurrent; // Or via the console system currentTool = ConsoleSystem.GetValue( "tool_current" ); // On the server you need to access via a Client object public override void Simulate( Client owner ) public override void Simulate( IClient owner ) { currentTool = owner.GetUserString( "tool_current" ); } ``` # Pawn Member You can also store your convar as a member on an Entity (ie - not a static) ``` [ConVar.ClientData( "tool_current" )] public string UserToolCurrent { get; set; } = "boxgun" ``` This works pretty much the same, except on both the server and client it can be accessed directly. ``` // Will always access the convar value on the client, but on the server will access // it via the entity's GetClientOwner(). If the entity has no client owner, will be null. currentTool = UserToolCurrent; ```