SwiftlyS2
Development

Convars

This page introduce various api to interact with game convars. It's not fake convar!

Creating convar

Here's an example of creating a convar.

IConVar<bool> convar = Core.ConVar.Create<bool>("sw_test_cvar", "Help message", true, ConvarFlags.CLIENT_CAN_EXECUTE);

Explanation:

  • sw_test_cvar is the name of the convar.
  • Help message is the help message of the convar.
  • true is the default value of the convar.
  • ConvarFlags.CLIENT_CAN_EXECUTE is the flags of the convar. Can be nullable.

The generic type is the type of the convar, all of the available types are as follows:

  • bool
  • short
  • ushort
  • int
  • uint
  • long
  • ulong
  • float
  • double
  • Color
  • QAngle
  • Vector
  • Vector2D
  • Vector4D
  • string

Finding convar

Here's an example of finding a convar.

IConVar<bool>? convar = Core.ConVar.Find<bool>("sv_cheats");

The result is null if the convar is not found. Both created convar and game original convar can be found through this method.

Various methods on IConVar<T>

There are many methods on IConVar<T> to interact with the convar. Here, we only introduce the most common ones.

Setting / Getting values

You can use the property .Value to directly get or set the value of the convar. Example:

convar.Value = true;
Console.WriteLine(convar.Value);

Setting value internally

In general, use .Value to set value will make your change go into a internal event queue, which means the change won't take effect immediately. This will become a problem when you are setting it temporarily. (e.g. Modify sv_autobunnyhopping in a hook temporarily`.)

To resolve this, we provide method .SetInternal(T value) to set the value internally.

Example:

convar.SetInternal(true);

Replicate to client

You can replicate a specific value to client with IConVar<T> object.

Example:

convar.ReplicateToClient(0, true);

In the example, 0 means player id, which is your target.

Query value from client

You can query the value of a convar from a client with IConVar<T> object.

Example:

convar.QueryClient(0, (value) => {
  Console.WriteLine(value);
});

Notice that value's type is string, which means you need to parse it yourself.

Modifying flags

You have these methods to modify the flags of a convar.

/// <summary>
/// Add flags to the convar.
/// </summary>
/// <param name="flags">flags.</param>
void AddFlags(ConvarFlags flags);

/// <summary>
/// Remove flags from the convar.
/// </summary>
/// <param name="flags">flags.</param>
void RemoveFlags(ConvarFlags flags);

/// <summary>
/// Clear all flags from the convar.
/// </summary>
void ClearFlags();

/// <summary>
/// Get the flags of the convar.
/// </summary>
/// <returns>The flags of the convar.</returns>
ConvarFlags GetFlags();

/// <summary>
/// Check if the convar has the given flags.
/// </summary>
/// <param name="flags">flags.</param>
/// <returns>True if the convar has all the given flags, false otherwise.</returns>
bool HasFlags(ConvarFlags flags);

Example call:

convar.AddFlags(ConvarFlags.CLIENT_CAN_EXECUTE | ConvarFlags.CHEAT);

Reference

See ConvarFlags.

See IConVar.

See IConVarService.

On this page