rhaiscript / rhai

Rhai - An embedded scripting language for Rust.
https://crates.io/crates/rhai
Apache License 2.0
3.79k stars 177 forks source link

Is it possible to put variable into scope? #84

Closed stavinsky closed 4 years ago

stavinsky commented 5 years ago

Sorry if this question already asked, but I can't understand how can I put for example string to the scope? I'm trying to make something like string parser that will get strings as input and parse them by user script in rhai. Thanks.

UPDATE: So it is finally works. As I understood I can write a function in user script and eval this script by the engine. After that the function will be available to be called by the call_fn with my params.

timfish commented 5 years ago

Scope is currently defined like this:

pub type Scope = Vec<(String, Box<dyn Any>)>;

That means you can add to the scope by pushing into the vector:

    let mut scope: Scope = Scope::new();
    scope.push(("a".to_string(), Box::new(0i16)));