forked from godot-rust/gdnative
/
lib.rs
78 lines (59 loc) · 1.63 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#[macro_use]
extern crate gdnative as godot;
use godot::*;
struct MyClass {
class_info: GodotClassInfo,
elapsed_time: f64,
}
impl MyClass {
fn new(info: GodotClassInfo) -> Self {
MyClass {
class_info: info,
elapsed_time: 0.0,
}
}
fn _ready(&mut self) {
godot_print!("Hello World!");
}
fn _process(&mut self, delta: f64) {
self.elapsed_time += delta;
}
fn _exit_tree(&mut self) {
godot_print!("MyClass node was running for {} seconds",
self.elapsed_time);
}
}
godot_gdnative_init!(godot_rust_gdnative_init);
godot_gdnative_terminate!(godot_rust_gdnative_terminate);
godot_nativescript_init! {
godot_rust_nativescript_init,
|handle| {
let constructor = godot_create_constructor!(MyClass, MyClass::new);
let destructor = godot_create_destructor!(MyClass);
let ready_method = godot_create_method!(MyClass,
fn _ready(&mut self) -> ());
let ready_method = GodotScriptMethod {
name: "_ready",
method_ptr: Some(ready_method),
attributes: GodotScriptMethodAttributes {
rpc_mode: GodotRpcMode::Disabled
},
method_data: std::ptr::null_mut(),
free_func: None
};
let process_method = godot_create_method!(MyClass,
fn _process(&mut self, delta: f64) -> ());
let exit_tree_method = godot_create_method!(MyClass,
fn _exit_tree(&mut self) -> ());
let builder = GodotScriptClassBuilder::new();
builder
.set_class_name("Test")
.set_base_class_name("Node")
.set_constructor(Some(constructor))
.set_destructor(Some(destructor))
.add_method_advanced(ready_method)
.add_method("_process", process_method)
.add_method("_exit_tree", exit_tree_method)
.build(handle);
}
}