rhai/examples/README.md

37 lines
3.9 KiB
Markdown
Raw Permalink Normal View History

2022-08-09 10:36:33 +02:00
# Sample Applications
2020-10-07 16:56:21 +02:00
2022-08-09 10:36:33 +02:00
## Standard Examples
2020-10-07 16:56:21 +02:00
| Example | Description |
| --------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- |
| [`arrays_and_structs`](arrays_and_structs.rs) | shows how to register a Rust type and using it with arrays |
| [`callback`](callback.rs) | shows how to store a Rhai closure and call it later within Rust |
| [`custom_types_and_methods`](custom_types_and_methods.rs) | shows how to register a Rust type and methods/getters/setters for it |
2022-08-09 10:36:33 +02:00
| [`custom_types`](custom_types.rs) | shows how to register a Rust type and methods/getters/setters using the `CustomType` trait. |
| [`definitions`](./definitions) | shows how to generate definition files for use with the [Rhai Language Server](https://github.com/rhaiscript/lsp) (requires the `metadata` feature) |
| [`hello`](hello.rs) | simple example that evaluates an expression and prints the result |
| [`reuse_scope`](reuse_scope.rs) | evaluates two pieces of code in separate runs, but using a common `Scope` |
| [`serde`](serde.rs) | example to serialize and deserialize Rust types with [`serde`](https://crates.io/crates/serde) (requires the `serde` feature) |
| [`simple_fn`](simple_fn.rs) | shows how to register a simple Rust function |
| [`strings`](strings.rs) | shows different ways to register Rust functions taking string arguments |
| [`threading`](threading.rs) | shows how to communicate with an `Engine` running in a separate thread via an MPSC channel |
2020-10-07 16:56:21 +02:00
2022-08-09 10:36:33 +02:00
## Scriptable Event Handler With State Examples
2022-02-12 04:48:16 +01:00
Because of its popularity, included are sample implementations for the pattern
2022-02-12 04:50:53 +01:00
[_Scriptable Event Handler With State_](https://rhai.rs/book/patterns/events.html) in different styles.
2022-02-12 04:48:16 +01:00
2022-02-12 04:50:53 +01:00
| Example | Handler Script | Description |
| ------------------------------------------ | ------------------------------------------------------------------ | :---------------------------------------------------------: |
| [`event_handler_main`](event_handler_main) | [`event_handler_main/script.rhai`](event_handler_main/script.rhai) | [_Main Style_](https://rhai.rs/book/patterns/events-1.html) |
| [`event_handler_js`](event_handler_js) | [`event_handler_js/script.rhai`](event_handler_js/script.rhai) | [_JS Style_](https://rhai.rs/book/patterns/events-2.html) |
| [`event_handler_map`](event_handler_map) | [`event_handler_map/script.rhai`](event_handler_map/script.rhai) | [_Map Style_](https://rhai.rs/book/patterns/events-3.html) |
2022-02-12 04:48:16 +01:00
2022-08-09 10:36:33 +02:00
## Running Examples
2022-02-12 04:48:16 +01:00
Examples can be run with the following command:
```sh
cargo run --example {example_name}
2020-10-07 16:56:21 +02:00
```