# Builtin types

bevy_scriptum provides the following types that can be used in JavaScript:

- `Vec3`
- `Entity`

## Vec3

A `Vec3` is represented as a plain object with `x`, `y` and `z` number
properties. A global `Vec3(x, y, z)` constructor function is available to all
scripts.

### Example JavaScript usage

```js
var my_vec = Vec3(1, 2, 3);
print(my_vec.x, my_vec.y, my_vec.z);
set_translation(entity, my_vec);
```

### Example Rust usage

```rust,no_run
# extern crate bevy;
# extern crate bevy_scriptum;

use bevy::prelude::*;
use bevy_scriptum::prelude::*;
use bevy_scriptum::runtimes::javascript::prelude::*;

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_scripting::<JsRuntime>(|runtime| {
             runtime.add_function(String::from("set_translation"), set_translation);
        })
        .run();
}

fn set_translation(
    In((entity, translation)): In<(BevyEntity, BevyVec3)>,
    mut entities: Query<&mut Transform>,
) {
    let mut transform = entities.get_mut(entity.0).unwrap();
    transform.translation = translation.0;
}
```

## Entity

The currently processed entity is available through the global `entity`
variable. It exposes an `index` property and can be passed back to Rust
functions that accept a `BevyEntity` argument.

`entity` is currently not available within promise callbacks.

### Example JavaScript usage

```js
print(entity.index);
pass_to_rust(entity);
```

### Example Rust usage

```rust,no_run
# extern crate bevy;
# extern crate bevy_scriptum;

use bevy::prelude::*;
use bevy_scriptum::prelude::*;
use bevy_scriptum::runtimes::javascript::prelude::*;

fn main() {
    App::new()
        .add_plugins(DefaultPlugins)
        .add_scripting::<JsRuntime>(|runtime| {
             runtime.add_function(String::from("pass_to_rust"), |In((entity,)): In<(BevyEntity,)>| {
               println!("pass_to_rust called with entity: {:?}", entity);
             });
        })
        .run();
}
```
