Skip to content

Commit 3813cd0

Browse files
ecobiubiuironpeakBromeon
authored
Merge #883
883: Property support for std collection types r=Bromeon a=ironpeak Implemented the `Export` trait for `HashMap`, `HashSet` (new) and `Vec` so that they can be exported. The PR also includes an example project using the types as properties. Co-authored-by: Hrafn Orri Hrafnkelsson <[email protected]> Co-authored-by: Jan Haller <[email protected]>
2 parents ff75813 + 1cf9c34 commit 3813cd0

22 files changed

+351
-24
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ members = [
1616
"examples/resource",
1717
"examples/native-plugin",
1818
"examples/rpc",
19-
"examples/array-export",
19+
"examples/builder-export",
20+
"examples/property-export",
2021
"impl/proc-macros"
2122
]
2223

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,8 @@ The [/examples](https://github.com/godot-rust/godot-rust/tree/master/examples) d
111111
- [**hello-world**](https://github.com/godot-rust/godot-rust/tree/master/examples/hello-world) - Your first project, writes to the console.
112112
- [**spinning-cube**](https://github.com/godot-rust/godot-rust/tree/master/examples/spinning-cube) - Spin our own node in place, exposing editor properties.
113113
- [**scene-create**](https://github.com/godot-rust/godot-rust/tree/master/examples/scene-create) - Load, instance and place scenes using Rust code.
114-
- [**array-export**](https://github.com/godot-rust/godot-rust/tree/master/examples/array-export) - Export more complex properties (here arrays) from Rust.
114+
- [**builder-export**](https://github.com/godot-rust/godot-rust/tree/master/examples/builder-export) - Export using the builder API.
115+
- [**property-export**](https://github.com/godot-rust/godot-rust/tree/master/examples/property-export) - Export complex properties such as collections.
115116
- [**dodge-the-creeps**](https://github.com/godot-rust/godot-rust/tree/master/examples/dodge-the-creeps) - A Rust port of the [little Godot game](https://docs.godotengine.org/en/stable/getting_started/step_by_step/your_first_game.html).
116117
- [**signals**](https://github.com/godot-rust/godot-rust/tree/master/examples/signals) - Connect and emit signals.
117118
- [**resource**](https://github.com/godot-rust/godot-rust/tree/master/examples/resource) - Create and use custom resources.

examples/array-export/array_export_library.gdnlib

Lines changed: 0 additions & 17 deletions
This file was deleted.

examples/array-export/Cargo.toml renamed to examples/builder-export/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[package]
2-
name = "array-export"
2+
name = "builder-export"
33
version = "0.1.0"
44
authors = ["The godot-rust developers"]
55
edition = "2021"

examples/array-export/ExportsArrays.gdns renamed to examples/builder-export/ExportsArrays.gdns

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[gd_resource type="NativeScript" load_steps=2 format=2]
22

3-
[ext_resource path="res://array_export_library.gdnlib" type="GDNativeLibrary" id=1]
3+
[ext_resource path="res://builder_export_library.gdnlib" type="GDNativeLibrary" id=1]
44

55
[resource]
66
resource_name = "ExportsArrays"
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[entry]
2+
3+
X11.64="res://../../target/debug/libbuilder_export.so"
4+
OSX.64="res://../../target/debug/libbuilder_export.dylib"
5+
Windows.64="res://../../target/debug/builder_export.dll"
6+
7+
[dependencies]
8+
9+
X11.64=[ ]
10+
OSX.64=[ ]
11+
12+
[general]
13+
14+
singleton=false
15+
load_once=true
16+
symbol_prefix="godot_"
17+
reloadable=true
File renamed without changes.

examples/array-export/project.godot renamed to examples/builder-export/project.godot

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ _global_script_class_icons={
2020

2121
[application]
2222

23-
config/name="array_export"
23+
config/name="builder_export"
2424
run/main_scene="res://ExportsArrays.tscn"
2525
config/icon="res://icon.png"
2626

File renamed without changes.

examples/property-export/Cargo.toml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[package]
2+
name = "property-export"
3+
version = "0.1.0"
4+
authors = ["The godot-rust developers"]
5+
edition = "2021"
6+
rust-version = "1.56"
7+
license = "MIT"
8+
publish = false
9+
10+
[lib]
11+
crate-type = ["cdylib"]
12+
13+
[dependencies]
14+
gdnative = { path = "../../gdnative" }
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
extends Node
2+
3+
func _ready():
4+
var rust = get_node("../PropertyExport")
5+
6+
print("\n-----------------------------------------------------------------")
7+
print("Print from GDScript (note the lexicographically ordered map/set):")
8+
print(" Vec (name):");
9+
for name in rust.name_vec:
10+
print(" * %s" % name)
11+
12+
print("\n HashMap (string -> color):")
13+
for string in rust.color_map:
14+
var color = rust.color_map[string]
15+
print(" * %s -> #%s" % [string, color.to_html(false)]);
16+
17+
print("\n HashSet (ID):")
18+
for id in rust.id_set:
19+
print(" * %s" % id)
20+
21+
# The program has printed the contents and fulfilled its purpose, quit
22+
get_tree().quit()

examples/property-export/Main.tscn

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
[gd_scene load_steps=4 format=2]
2+
3+
[ext_resource path="res://property_export_library.gdnlib" type="GDNativeLibrary" id=1]
4+
[ext_resource path="res://GDScriptPrinter.gd" type="Script" id=2]
5+
6+
[sub_resource type="NativeScript" id=1]
7+
resource_name = "PropertyExport"
8+
class_name = "PropertyExport"
9+
library = ExtResource( 1 )
10+
11+
[node name="Node" type="Node"]
12+
13+
[node name="PropertyExport" type="Node" parent="."]
14+
script = SubResource( 1 )
15+
name_vec = [ "Godot", "Godette", "Go ." ]
16+
color_map = {
17+
"blue": Color( 0.184314, 0.160784, 0.8, 1 ),
18+
"green": Color( 0.0941176, 0.447059, 0.192157, 1 ),
19+
"teal": Color( 0.0941176, 0.423529, 0.564706, 1 )
20+
}
21+
id_set = [ 21, 77, 8, 90 ]
22+
23+
[node name="GDScriptPrinter" type="Node" parent="."]
24+
script = ExtResource( 2 )
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[gd_resource type="Environment" load_steps=2 format=2]
2+
3+
[sub_resource type="ProceduralSky" id=1]
4+
sky_top_color = Color( 0.0470588, 0.454902, 0.976471, 1 )
5+
sky_horizon_color = Color( 0.556863, 0.823529, 0.909804, 1 )
6+
sky_curve = 0.25
7+
ground_bottom_color = Color( 0.101961, 0.145098, 0.188235, 1 )
8+
ground_horizon_color = Color( 0.482353, 0.788235, 0.952941, 1 )
9+
ground_curve = 0.01
10+
sun_energy = 16.0
11+
12+
[resource]
13+
background_mode = 2
14+
background_sky = SubResource( 1 )
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
; Engine configuration file.
2+
; It's best edited using the editor UI and not directly,
3+
; since the parameters that go here are not all obvious.
4+
;
5+
; Format:
6+
; [section] ; section goes between []
7+
; param=value ; assign values to parameters
8+
9+
config_version=4
10+
11+
_global_script_classes=[ ]
12+
_global_script_class_icons={
13+
}
14+
15+
[application]
16+
17+
config/name="Godot Rust - Property Export"
18+
run/main_scene="res://Main.tscn"
19+
20+
[rendering]
21+
22+
environment/default_environment="res://default_env.tres"
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[entry]
2+
3+
X11.64="res://../../target/debug/libproperty_export.so"
4+
OSX.64="res://../../target/debug/libproperty_export.dylib"
5+
Windows.64="res://../../target/debug/property_export.dll"
6+
7+
[dependencies]
8+
9+
X11.64=[ ]
10+
OSX.64=[ ]
11+
12+
[general]
13+
14+
singleton=false
15+
load_once=true
16+
symbol_prefix="godot_"
17+
reloadable=true

examples/property-export/src/lib.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use gdnative::prelude::*;
2+
3+
use std::collections::{HashMap, HashSet};
4+
5+
#[derive(NativeClass, Default)]
6+
#[inherit(Node)]
7+
pub struct PropertyExport {
8+
#[property]
9+
name_vec: Vec<String>,
10+
11+
#[property]
12+
color_map: HashMap<GodotString, Color>,
13+
14+
#[property]
15+
id_set: HashSet<i64>,
16+
}
17+
18+
#[methods]
19+
impl PropertyExport {
20+
fn new(_base: &Node) -> Self {
21+
Self::default()
22+
}
23+
24+
#[export]
25+
fn _ready(&self, _base: &Node) {
26+
godot_print!("------------------------------------------------------------------");
27+
godot_print!("Print from Rust (note the unordered map/set):");
28+
godot_print!(" Vec (name):");
29+
for name in &self.name_vec {
30+
godot_print!(" * {}", name);
31+
}
32+
33+
godot_print!("\n HashMap (string -> color):");
34+
for (string, color) in &self.color_map {
35+
godot_print!(" * {} -> #{}", string, color.to_html(false));
36+
}
37+
38+
godot_print!("\n HashSet (ID):");
39+
for id in &self.id_set {
40+
godot_print!(" * {}", id);
41+
}
42+
}
43+
}
44+
45+
fn init(handle: InitHandle) {
46+
handle.add_class::<PropertyExport>();
47+
}
48+
49+
godot_init!(init);

0 commit comments

Comments
 (0)