S&box Wiki

Revision Difference


<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 server it is sent to the server. These values are accessible on the server using <page>Client.GetUserString</page>( "var_name" ). # 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 ) { 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; ```