Macro red4ext_rs::exports

source ·
macro_rules! exports {
    [$export:expr, $($tt:tt)*] => { ... };
    [$export:expr] => { ... };
    [] => { ... };
}
Expand description

Creates a list of exports to be registered within the game’s RTTI system.

§Example

use std::cell::Cell;

use red4ext_rs::{ClassExport, Exportable, GlobalExport, ScriptClass, class_kind, exports, methods, global};
use red4ext_rs::types::IScriptable;

fn exports() -> impl Exportable {
    exports![
        GlobalExport(global!(c"GlobalExample", global_example)),
        ClassExport::<MyClass>::builder()
           .base("IScriptable")
           .methods(methods![
               c"Value" => MyClass::value,
               c"SetValue" => MyClass::set_value,
           ])
           .build(),
    ]
}

fn global_example() -> String {
  "Hello, world!".to_string()
}

#[derive(Debug, Default, Clone)]
#[repr(C)]
struct MyClass {
    // You must include the base native class in your Rust struct.
    base: IScriptable,
    value: Cell<i32>,
}

impl MyClass {
   fn value(&self) -> i32 {
      self.value.get()
   }

   fn set_value(&self, value: i32) {
      self.value.set(value)
   }
}

unsafe impl ScriptClass for MyClass {
   const NAME: &'static str = "MyClass";
   type Kind = class_kind::Native;
}