#godot #collection #gamedev #variant #traits #book #godot-rust

gdvariants

Rust std library collections wrapper that implements the godot-rust variant traits

9 releases (2 stable)

1.1.0 Apr 19, 2022
0.7.0 Apr 18, 2022

#1615 in Game dev

31 downloads per month

MIT/Apache

180KB
3K SLoC

gdvariants

Rust std library collections wrapper that implements the godot-rust variant traits for convenience when using godot-rust.

Traits implemented on top of the standard library implementation:

  • Export required for the property attribute.
  • FromVariant required for converting Godot types to Rust types.
  • ToVariant required for converting Rust types to Godot types.
  • Borrow for borrowing the base standard library type.
  • BorrowMut for borrowing the base standard library type as a mutable reference.
  • From for converting standard library types to gdvariant types.

Types

  • HashMap
  • HashSet
  • Vec

Usage

Add this to your Cargo.toml:

gdvariants = "*"

Read the godot-rust book for information on how to setup a Godot project that uses Rust.

Property

use gdnative::api::*;
use gdnative::prelude::*;

use gdvariants::collections::HashMap;

#[derive(NativeClass, Default)]
#[inherit(Node)]
pub struct ExampleHashMapProperty {
    #[property]
    players: HashMap<i64, String>,
}

Networking

use gdnative::api::*;
use gdnative::prelude::*;

use gdvariants::collections::HashMap;

...

fn send_data(&mut self, owner: &Node) {
    let mut data: HashMap<i64, i64> = HashMap::new();
    data.insert(1, 0);

    for player in self.players.keys() {
        owner.rpc_id(*player, "data", &[data.to_variant()]);
    }
}
use gdnative::api::*;
use gdnative::prelude::*;

use gdvariants::collections::HashMap;

...

fn receive_data(&mut self, owner: &Node, data: HashMap<i64, i64>) {
    let mut data: HashMap<i64, i64> = HashMap::new();
    data.insert(1, 0);

    for player in self.players.keys() {
        owner.rpc_id(*player, "data", &[data.to_variant()]);
    }
}

Crate Features

  • serde: enables deserialize and serialize for collections.

Dependencies

~9–16MB
~250K SLoC