squirrel_example/
squirrel_example.rs

1use rrplug::{
2    bindings::squirreldatatypes::SQClosure,
3    high::squirrel::{call_sq_object_function, SQHandle},
4    prelude::*,
5};
6
7pub struct ExamplePlugin;
8
9impl Plugin for ExamplePlugin {
10    const PLUGIN_INFO: PluginInfo =
11        PluginInfo::new(c"example", c"EXAMPLLEE", c"EXAMPLE", PluginContext::all());
12
13    fn new(_reloaded: bool) -> Self {
14        register_sq_functions(great_person);
15        register_sq_functions(call_with_random_number);
16        register_sq_functions(sum);
17
18        Self {}
19    }
20}
21
22entry!(ExamplePlugin);
23
24// if it returns an error the function will throw a error in the sqvm which can be caught at the call site
25#[rrplug::sqfunction(VM = "SERVER", ExportName = "GreatPerson")]
26fn great_person(function: SQHandle<SQClosure>) -> Result<String, rrplug::errors::CallError> {
27    // non type safe way of getting a return from a function
28    // this could be changed if the crate gets some attention
29    let name = call_sq_object_function(sqvm, sq_functions, function, ())?;
30
31    log::info!("hello, {}", name);
32
33    Ok(name)
34}
35
36#[rrplug::sqfunction(VM = "SERVER", ExportName = "CallWithRandomNumber")]
37fn call_with_random_number(mut function: SquirrelFn<(i32, String)>) {
38    const TOTALY_RANDOM_NUMBER: i32 = 37;
39
40    _ = function.call(
41        sqvm,
42        sq_functions,
43        (TOTALY_RANDOM_NUMBER, TOTALY_RANDOM_NUMBER.to_string()),
44    );
45}
46
47#[rrplug::sqfunction(VM = "SERVER", ExportName = "Sum")]
48fn sum(vec: Vector3, numbers: Vec<f32>) -> f32 {
49    vec.x + vec.y + vec.z + numbers.into_iter().sum::<f32>()
50}