https://github.com/fastestmolasses/convars
Console config variables library for game engines
https://github.com/fastestmolasses/convars
bevy console-variables convar cvars game game-engine rust
Last synced: about 1 month ago
JSON representation
Console config variables library for game engines
- Host: GitHub
- URL: https://github.com/fastestmolasses/convars
- Owner: FastestMolasses
- License: mit
- Created: 2023-10-01T07:12:20.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-10-10T06:49:58.000Z (over 2 years ago)
- Last Synced: 2026-04-04T18:21:26.804Z (2 months ago)
- Topics: bevy, console-variables, convar, cvars, game, game-engine, rust
- Language: Rust
- Homepage:
- Size: 58.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# ConVars
ConVars is a Rust library to create console variables for games made. Designed with the [Bevy engine](https://bevyengine.org/) in mind but can work in any project. It allows developers to access and modify game configuration variables at runtime through a console or other user input mechanisms.
## Features
* Define configuration variables with default values
* Update configuration variables at runtime
* Structs supported
* Serialize and deserialize configuration variables
## Usage
Add this to your Cargo.toml:
```
cargo add convars
```
```toml
[dependencies]
convars = "0.1.0"
```
Here's a basic example:
```rust
use convars::convars;
convars! {
max_enemies: i32 = 10,
view_distance: f32 = 100.0,
}
fn main() {
let mut convars = ConVars::default();
// These values will come from a UI or console
convars.set_str("max_enemies", "20").unwrap();
}
```
## User Defined Types
You can define your own configuration variable types by implementing the ConVarType trait:
```rust
use convars::ConVarType;
#[derive(Debug, PartialEq)]
pub struct PlayerConfig {
pub level: i32,
pub damage: f32,
}
impl ConVarType for PlayerConfig {
fn from_convar_str(value: &str) -> Result {
let parts: Vec<&str> = value.split(',').collect();
if parts.len() != 2 {
return Err("invalid format for PlayerConfig");
}
let level = parts[0].parse::().map_err(|_| "failed to parse i32")?;
let damage = parts[1].parse::().map_err(|_| "failed to parse f32")?;
Ok(PlayerConfig { level, damage })
}
fn to_convar_str(&self) -> String {
format!("{},{}", self.level, self.damage)
}
}
convars! {
player_config: PlayerConfig = PlayerConfig { level: 5, damage: 30.0 },
}
```