Crates.io | cvars |
lib.rs | cvars |
version | 0.4.2 |
source | src |
created_at | 2021-08-02 14:56:12.101998 |
updated_at | 2023-07-10 11:28:15.805099 |
description | Configuration variables - a simple and ergonomic way to store and edit configuration at runtime |
homepage | https://github.com/martin-t/cvars |
repository | https://github.com/martin-t/cvars |
max_upload_size | |
id | 430471 |
size | 27,299 |
Cvars (console variables or configuration variables) are a simple way to store settings you want to change at runtime without restarting your program.
Consoles are the most ergonomic way to set cvars but you can write your own UI or read them from stdin if you want. They are available for Fyrox and Macroquad.
These crates are inspired by the idTech (Doom, Quake) and Source family of game engines but they can be useful outside games. Cvars allow you to iterate faster by letting you test certain gameplay changes without recompiling. They also make your game more moddable if you expose (a subset of) them to players.
TL;DR: Set and get struct fields based on the field's name as a string. User writes the cvar's name and new value into the console, it sets the appropriate field in your config struct and the game now behaves differently. Your gamecode uses cvars as regular staticly typed values.
Usage example video worth 15*1000 words per second
Zero boilerplate - there are no traits to implement manually and no setup code to call per cvar.
Minimal performance cost - just struct field access instead of a hardcoded constant. Cvars are cheap enough to keep everything configurable even after you're done finding the best values - you can keep things tweakable in your released game for players to experiment themselves.
Cargo.toml
:cargo add cvars
SetGet
:use cvars::cvars;
// This generates a Cvars struct containing all your config options
// and a corresponding Default impl.
cvars! {
g_rocket_launcher_ammo_max: i32 = 20,
g_rocket_launcher_damage: f32 = 100.0,
}
// Store this in your game state.
let mut cvars = Cvars::default();
// These normally come from the user
// (from stdin / your game's console / etc.)
let cvar_name = "g_rocket_launcher_damage";
let new_value = "150";
// This looks up the right field and sets it to the new value.
cvars.set_str(cvar_name, new_value).unwrap();
A player/modder/gamedev wants rockets to do more damage.
He types g_rocket_launcher_damage 150
into the game's console or stdin.
The code gets both the cvar name and new value as strings
so you can't write cvars.g_rocket_launcher_damage = 150
.
You need to look up the correct field based on the string - this is what cvars
does - it generates set_str
(and some other useful methods). You call cvars.set_str("g_rocket_launcher_damage", "150");
which looks up the right field, parses the value into its type and updates the field with it.
From then on, rockets do 150 damage.
The important thing is that in the rest of your application,
you can still access your cvars as regular struct fields - e.g. player.health -= cvars.g_rocket_launcher_damage;
.
This means you only need to use strings when the user
(player or developer when debugging or testing a different balance) is changing the values.
The rest of your gamelogic is still statically typed and using a cvar in gamecode
is just a field access without any overhead.
A typical game will have hundreds or thousands of tunable parameters. With cvars and a console you can keep them all configurable for advanced players, modders and your-gamedev-self without having a build an elaborate settings menu. You can keep everything configurable using a TUI while also exposing common settings to normal players in your game's GUI.
See cvars/examples/stdin.rs for a small runnable example.
For real-world examples, look at games using cvars:
The Fyrox console is a separate crate in this repo. To use it in your game, add it to your Cargo.toml
and call its methods on the relevant engine events.
See the crates.io page or its docs for more information.
The Macroquad console is a separate crate in this repo. To use it in your game, add it to your Cargo.toml
and call its update
method every frame.
See the crates.io page or its docs for more information.
SetGet
to create settters and getters for cvars based on their name
set
, get
)set_str
, get_string
)cvars!
macro to declare type and initial value on one lineFeatures I am currently not planning to implement myself but would be nice to have. I might accept a PR if it's clean and maintainable but it's probably better if you implement them in your own crate:
const
)cvars
currently doesn't.Compared to these, cvars either has no overhead at runtime or requires less setup code. The downside currently might be slightly increased incremental compile times (by hundreds of milliseconds).
Cvars also serves a slightly different purpose than inline_tweak and const-tweaker. It's meant to stay in code forever, even after releasing your game, to enable modding by your game's community.
Finally, you might not need a specialized crate like cvars or inline_tweak at all. A lot of common wisdom in gamedev is wrong or outdated. What you need might be just a file containing RON or JSON which is loaded each frame and deserialized into a config struct. It'll be cached by the OS most of the time and nobody minds a dropped frame during development after editing the file.
The repo is organized as a cargo workspace for the main functionality, with consoles and benchmarks as separate crates - see Cargo.toml
for the technical reasons.
Testing: Use cargo test
in the root directory to test everything in the workspace. To test the consoles, cd
into their directories and run cargo test
there.
Debugging:
cargo expand --package cvars-macros --example testing-fnlike
to see what the proc macros generate. There is a similar file for derive macros. You can use println!
and dbg!
in the macros as well.cargo expand --package cvars-macros --example testing-fnlike > cvars-macros/examples/testing-expanded.rs && cargo build --package cvars-macros --example testing-expanded ; rm cvars-macros/examples/testing-expanded.rs
. One exception is when the macro produces syntactically invalid code, in which case its output will be missing entirely.Benchmarking: Run ./bench.sh
in cvars-bench-compile-time
to benchmark incremental compile time when using the proc macros.
Useful commands:
cvars-bench-compile-time
(e.g. e.g. cargo llvm-lines --features string,typed,fnlike,cvars-1000
) to find out which functions generate a lot of LLVM IR and which compile to a lot of code. This is a good indicator of what is causing long compile times. Lines of LLVM IR is a bit more important because it better indicates how much work the backend has to do even if it compiles down to a small amount of machine code.CVARS_STATS
to make the macros print how long they took - e.g. CVARS_STATS= cargo bloat --features string,typed,fnlike,cvars-1000
. If it's small compared to the total compile time, most of the time is spent in codegen, dealing with the large amount of code generated. Note that the compiler's output, including proc macro output, is cached if the compiled code hasn't changed so you might need to set the variable and also edit the code to see the stats.You can always find me on the Rusty Games Discord server if you have any questions or suggestions.
Issues and Pull Requests are welcome. See the good first issue label for easy tasks.
AGPL-v3 or newer