bevy_script_api 0.8.0

Bevy API for multiple script languages, part of bevy_mod_scripting.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
// @generated by cargo bevy-api-gen generate, modify the templates not this file
#![allow(clippy::all)]
#![allow(unused, deprecated, dead_code)]
#![cfg_attr(rustfmt, rustfmt_skip)]
use super::bevy_reflect::*;
extern crate self as bevy_script_api;
use bevy_script_api::{
    lua::RegisterForeignLuaType, ReflectedValue, common::bevy::GetWorld,
};
#[derive(bevy_mod_scripting_lua_derive::LuaProxy)]
#[proxy(
    derive(clone),
    remote = "bevy::ecs::entity::Entity",
    functions[r#"

    #[lua(
        as_trait = "std::cmp::PartialEq",
        kind = "MetaFunction",
        composite = "eq",
        metamethod = "Eq",
    )]
    fn eq(&self, #[proxy] other: &entity::Entity) -> bool;

"#,
    r#"
/// Creates a new entity ID with the specified `index` and a generation of 1.
/// # Note
/// Spawning a specific `entity` value is __rarely the right choice__. Most apps should favor
/// [`Commands::spawn`](crate::system::Commands::spawn). This method should generally
/// only be used for sharing entities across apps, and only when they have a scheme
/// worked out to share an index space (which doesn't happen by default).
/// In general, one should not try to synchronize the ECS by attempting to ensure that
/// `Entity` lines up between instances, but instead insert a secondary identifier as
/// a component.

    #[lua(kind = "Function", output(proxy))]
    fn from_raw(index: u32) -> bevy::ecs::entity::Entity;

"#,
    r#"
/// Convert to a form convenient for passing outside of rust.
/// Only useful for identifying entities within the same instance of an application. Do not use
/// for serialization between runs.
/// No particular structure is guaranteed for the returned bits.

    #[lua(kind = "Method")]
    fn to_bits(self) -> u64;

"#,
    r#"
/// Reconstruct an `Entity` previously destructured with [`Entity::to_bits`].
/// Only useful when applied to results from `to_bits` in the same instance of an application.
/// # Panics
/// This method will likely panic if given `u64` values that did not come from [`Entity::to_bits`].

    #[lua(kind = "Function", output(proxy))]
    fn from_bits(bits: u64) -> bevy::ecs::entity::Entity;

"#,
    r#"
/// Return a transiently unique identifier.
/// No two simultaneously-live entities share the same index, but dead entities' indices may collide
/// with both live and dead entities. Useful for compactly representing entities within a
/// specific snapshot of the world, such as when serializing.

    #[lua(kind = "Method")]
    fn index(self) -> u32;

"#,
    r#"
/// Returns the generation of this Entity's index. The generation is incremented each time an
/// entity with a given index is despawned. This serves as a "count" of the number of times a
/// given index has been reused (index, generation) pairs uniquely identify a given Entity.

    #[lua(kind = "Method")]
    fn generation(self) -> u32;

"#,
    r#"

    #[lua(as_trait = "std::clone::Clone", kind = "Method", output(proxy))]
    fn clone(&self) -> bevy::ecs::entity::Entity;

"#,
    r#"
#[lua(kind="MetaMethod", metamethod="ToString")]
fn index(&self) -> String {
    format!("{:?}", _self)
}
"#]
)]
struct Entity {}
#[derive(bevy_mod_scripting_lua_derive::LuaProxy)]
#[proxy(
    derive(),
    remote = "bevy::ecs::world::OnAdd",
    functions[r#"
#[lua(kind="MetaMethod", metamethod="ToString")]
fn index(&self) -> String {
    format!("{:?}", _self)
}
"#]
)]
struct OnAdd {}
#[derive(bevy_mod_scripting_lua_derive::LuaProxy)]
#[proxy(
    derive(),
    remote = "bevy::ecs::world::OnInsert",
    functions[r#"
#[lua(kind="MetaMethod", metamethod="ToString")]
fn index(&self) -> String {
    format!("{:?}", _self)
}
"#]
)]
struct OnInsert {}
#[derive(bevy_mod_scripting_lua_derive::LuaProxy)]
#[proxy(
    derive(),
    remote = "bevy::ecs::world::OnRemove",
    functions[r#"
#[lua(kind="MetaMethod", metamethod="ToString")]
fn index(&self) -> String {
    format!("{:?}", _self)
}
"#]
)]
struct OnRemove {}
#[derive(bevy_mod_scripting_lua_derive::LuaProxy)]
#[proxy(
    derive(),
    remote = "bevy::ecs::world::OnReplace",
    functions[r#"
#[lua(kind="MetaMethod", metamethod="ToString")]
fn index(&self) -> String {
    format!("{:?}", _self)
}
"#]
)]
struct OnReplace {}
#[derive(bevy_mod_scripting_lua_derive::LuaProxy)]
#[proxy(
    derive(clone),
    remote = "bevy::ecs::component::ComponentId",
    functions[r#"
/// Creates a new [`ComponentId`].
/// The `index` is a unique value associated with each type of component in a given world.
/// Usually, this value is taken from a counter incremented for each type of component registered with the world.

    #[lua(kind = "Function", output(proxy))]
    fn new(index: usize) -> bevy::ecs::component::ComponentId;

"#,
    r#"
/// Returns the index of the current component.

    #[lua(kind = "Method")]
    fn index(self) -> usize;

"#,
    r#"

    #[lua(as_trait = "std::clone::Clone", kind = "Method", output(proxy))]
    fn clone(&self) -> bevy::ecs::component::ComponentId;

"#,
    r#"

    #[lua(
        as_trait = "std::cmp::PartialEq",
        kind = "MetaFunction",
        composite = "eq",
        metamethod = "Eq",
    )]
    fn eq(&self, #[proxy] other: &component::ComponentId) -> bool;

"#,
    r#"

    #[lua(as_trait = "std::cmp::Eq", kind = "Method")]
    fn assert_receiver_is_total_eq(&self) -> ();

"#,
    r#"
#[lua(kind="MetaMethod", metamethod="ToString")]
fn index(&self) -> String {
    format!("{:?}", _self)
}
"#]
)]
struct ComponentId();
#[derive(bevy_mod_scripting_lua_derive::LuaProxy)]
#[proxy(
    derive(clone),
    remote = "bevy::ecs::component::Tick",
    functions[r#"

    #[lua(as_trait = "std::clone::Clone", kind = "Method", output(proxy))]
    fn clone(&self) -> bevy::ecs::component::Tick;

"#,
    r#"

    #[lua(
        as_trait = "std::cmp::PartialEq",
        kind = "MetaFunction",
        composite = "eq",
        metamethod = "Eq",
    )]
    fn eq(&self, #[proxy] other: &component::Tick) -> bool;

"#,
    r#"

    #[lua(as_trait = "std::cmp::Eq", kind = "Method")]
    fn assert_receiver_is_total_eq(&self) -> ();

"#,
    r#"
/// Creates a new [`Tick`] wrapping the given value.

    #[lua(kind = "Function", output(proxy))]
    fn new(tick: u32) -> bevy::ecs::component::Tick;

"#,
    r#"
/// Gets the value of this change tick.

    #[lua(kind = "Method")]
    fn get(self) -> u32;

"#,
    r#"
/// Sets the value of this change tick.

    #[lua(kind = "MutatingMethod")]
    fn set(&mut self, tick: u32) -> ();

"#,
    r#"
/// Returns `true` if this `Tick` occurred since the system's `last_run`.
/// `this_run` is the current tick of the system, used as a reference to help deal with wraparound.

    #[lua(kind = "Method")]
    fn is_newer_than(
        self,
        #[proxy]
        last_run: bevy::ecs::component::Tick,
        #[proxy]
        this_run: bevy::ecs::component::Tick,
    ) -> bool;

"#,
    r#"
#[lua(kind="MetaMethod", metamethod="ToString")]
fn index(&self) -> String {
    format!("{:?}", _self)
}
"#]
)]
struct Tick {}
#[derive(bevy_mod_scripting_lua_derive::LuaProxy)]
#[proxy(
    derive(clone),
    remote = "bevy::ecs::component::ComponentTicks",
    functions[r#"
/// Returns `true` if the component or resource was added after the system last ran
/// (or the system is running for the first time).

    #[lua(kind = "Method")]
    fn is_added(
        &self,
        #[proxy]
        last_run: bevy::ecs::component::Tick,
        #[proxy]
        this_run: bevy::ecs::component::Tick,
    ) -> bool;

"#,
    r#"
/// Returns `true` if the component or resource was added or mutably dereferenced after the system last ran
/// (or the system is running for the first time).

    #[lua(kind = "Method")]
    fn is_changed(
        &self,
        #[proxy]
        last_run: bevy::ecs::component::Tick,
        #[proxy]
        this_run: bevy::ecs::component::Tick,
    ) -> bool;

"#,
    r#"
/// Creates a new instance with the same change tick for `added` and `changed`.

    #[lua(kind = "Function", output(proxy))]
    fn new(
        #[proxy]
        change_tick: bevy::ecs::component::Tick,
    ) -> bevy::ecs::component::ComponentTicks;

"#,
    r#"
/// Manually sets the change tick.
/// This is normally done automatically via the [`DerefMut`](std::ops::DerefMut) implementation
/// on [`Mut<T>`](crate::change_detection::Mut), [`ResMut<T>`](crate::change_detection::ResMut), etc.
/// However, components and resources that make use of interior mutability might require manual updates.
/// # Example
/// ```no_run
/// # use bevy_ecs::{world::World, component::ComponentTicks};
/// let world: World = unimplemented!();
/// let component_ticks: ComponentTicks = unimplemented!();
/// component_ticks.set_changed(world.read_change_tick());
/// ```

    #[lua(kind = "MutatingMethod")]
    fn set_changed(&mut self, #[proxy] change_tick: bevy::ecs::component::Tick) -> ();

"#,
    r#"

    #[lua(as_trait = "std::clone::Clone", kind = "Method", output(proxy))]
    fn clone(&self) -> bevy::ecs::component::ComponentTicks;

"#,
    r#"
#[lua(kind="MetaMethod", metamethod="ToString")]
fn index(&self) -> String {
    format!("{:?}", _self)
}
"#]
)]
struct ComponentTicks {
    #[lua(output(proxy))]
    added: bevy::ecs::component::Tick,
    #[lua(output(proxy))]
    changed: bevy::ecs::component::Tick,
}
#[derive(bevy_mod_scripting_lua_derive::LuaProxy)]
#[proxy(
    derive(clone),
    remote = "bevy::ecs::identifier::Identifier",
    functions[r#"

    #[lua(
        as_trait = "std::cmp::PartialEq",
        kind = "MetaFunction",
        composite = "eq",
        metamethod = "Eq",
    )]
    fn eq(&self, #[proxy] other: &identifier::Identifier) -> bool;

"#,
    r#"
/// Returns the value of the low segment of the [`Identifier`].

    #[lua(kind = "Method")]
    fn low(self) -> u32;

"#,
    r#"
/// Returns the masked value of the high segment of the [`Identifier`].
/// Does not include the flag bits.

    #[lua(kind = "Method")]
    fn masked_high(self) -> u32;

"#,
    r#"
/// Convert the [`Identifier`] into a `u64`.

    #[lua(kind = "Method")]
    fn to_bits(self) -> u64;

"#,
    r#"
/// Convert a `u64` into an [`Identifier`].
/// # Panics
/// This method will likely panic if given `u64` values that did not come from [`Identifier::to_bits`].

    #[lua(kind = "Function", output(proxy))]
    fn from_bits(value: u64) -> bevy::ecs::identifier::Identifier;

"#,
    r#"

    #[lua(as_trait = "std::clone::Clone", kind = "Method", output(proxy))]
    fn clone(&self) -> bevy::ecs::identifier::Identifier;

"#,
    r#"
#[lua(kind="MetaMethod", metamethod="ToString")]
fn index(&self) -> String {
    format!("{:?}", _self)
}
"#]
)]
struct Identifier {}
#[derive(bevy_mod_scripting_lua_derive::LuaProxy)]
#[proxy(
    derive(clone),
    remote = "bevy::ecs::entity::EntityHash",
    functions[r#"

    #[lua(as_trait = "std::clone::Clone", kind = "Method", output(proxy))]
    fn clone(&self) -> bevy::ecs::entity::EntityHash;

"#]
)]
struct EntityHash {}
#[derive(bevy_mod_scripting_lua_derive::LuaProxy)]
#[proxy(
    derive(clone),
    remote = "bevy::ecs::removal_detection::RemovedComponentEntity",
    functions[r#"

    #[lua(as_trait = "std::clone::Clone", kind = "Method", output(proxy))]
    fn clone(&self) -> bevy::ecs::removal_detection::RemovedComponentEntity;

"#,
    r#"
#[lua(kind="MetaMethod", metamethod="ToString")]
fn index(&self) -> String {
    format!("{:?}", _self)
}
"#]
)]
struct RemovedComponentEntity();
#[derive(bevy_mod_scripting_lua_derive::LuaProxy)]
#[proxy(derive(), remote = "bevy::ecs::system::SystemIdMarker", functions[])]
struct SystemIdMarker {}
#[derive(Default)]
pub(crate) struct Globals;
impl bevy_mod_scripting_lua::tealr::mlu::ExportInstances for Globals {
    fn add_instances<
        'lua,
        T: bevy_mod_scripting_lua::tealr::mlu::InstanceCollector<'lua>,
    >(self, instances: &mut T) -> bevy_mod_scripting_lua::tealr::mlu::mlua::Result<()> {
        instances
            .add_instance(
                "Entity",
                bevy_mod_scripting_lua::tealr::mlu::UserDataProxy::<LuaEntity>::new,
            )?;
        instances
            .add_instance(
                "ComponentId",
                bevy_mod_scripting_lua::tealr::mlu::UserDataProxy::<LuaComponentId>::new,
            )?;
        instances
            .add_instance(
                "Tick",
                bevy_mod_scripting_lua::tealr::mlu::UserDataProxy::<LuaTick>::new,
            )?;
        instances
            .add_instance(
                "ComponentTicks",
                bevy_mod_scripting_lua::tealr::mlu::UserDataProxy::<
                    LuaComponentTicks,
                >::new,
            )?;
        instances
            .add_instance(
                "Identifier",
                bevy_mod_scripting_lua::tealr::mlu::UserDataProxy::<LuaIdentifier>::new,
            )?;
        Ok(())
    }
}
pub struct BevyEcsAPIProvider;
impl bevy_mod_scripting_core::hosts::APIProvider for BevyEcsAPIProvider {
    type APITarget = std::sync::Mutex<bevy_mod_scripting_lua::tealr::mlu::mlua::Lua>;
    type ScriptContext = std::sync::Mutex<bevy_mod_scripting_lua::tealr::mlu::mlua::Lua>;
    type DocTarget = bevy_mod_scripting_lua::docs::LuaDocFragment;
    fn attach_api(
        &mut self,
        ctx: &mut Self::APITarget,
    ) -> Result<(), bevy_mod_scripting_core::error::ScriptError> {
        let ctx = ctx.get_mut().expect("Unable to acquire lock on Lua context");
        bevy_mod_scripting_lua::tealr::mlu::set_global_env(Globals, ctx)
            .map_err(|e| bevy_mod_scripting_core::error::ScriptError::Other(
                e.to_string(),
            ))
    }
    fn get_doc_fragment(&self) -> Option<Self::DocTarget> {
        Some(
            bevy_mod_scripting_lua::docs::LuaDocFragment::new(
                "BevyEcsAPI",
                |tw| {
                    tw.document_global_instance::<Globals>()
                        .expect("Something went wrong documenting globals")
                        .process_type::<LuaEntity>()
                        .process_type::<
                            bevy_mod_scripting_lua::tealr::mlu::UserDataProxy<LuaEntity>,
                        >()
                        .process_type::<LuaOnAdd>()
                        .process_type::<LuaOnInsert>()
                        .process_type::<LuaOnRemove>()
                        .process_type::<LuaOnReplace>()
                        .process_type::<LuaComponentId>()
                        .process_type::<
                            bevy_mod_scripting_lua::tealr::mlu::UserDataProxy<
                                LuaComponentId,
                            >,
                        >()
                        .process_type::<LuaTick>()
                        .process_type::<
                            bevy_mod_scripting_lua::tealr::mlu::UserDataProxy<LuaTick>,
                        >()
                        .process_type::<LuaComponentTicks>()
                        .process_type::<
                            bevy_mod_scripting_lua::tealr::mlu::UserDataProxy<
                                LuaComponentTicks,
                            >,
                        >()
                        .process_type::<LuaIdentifier>()
                        .process_type::<
                            bevy_mod_scripting_lua::tealr::mlu::UserDataProxy<
                                LuaIdentifier,
                            >,
                        >()
                        .process_type::<LuaEntityHash>()
                        .process_type::<LuaRemovedComponentEntity>()
                        .process_type::<LuaSystemIdMarker>()
                },
            ),
        )
    }
    fn setup_script(
        &mut self,
        script_data: &bevy_mod_scripting_core::hosts::ScriptData,
        ctx: &mut Self::ScriptContext,
    ) -> Result<(), bevy_mod_scripting_core::error::ScriptError> {
        Ok(())
    }
    fn setup_script_runtime(
        &mut self,
        world_ptr: bevy_mod_scripting_core::world::WorldPointer,
        _script_data: &bevy_mod_scripting_core::hosts::ScriptData,
        ctx: &mut Self::ScriptContext,
    ) -> Result<(), bevy_mod_scripting_core::error::ScriptError> {
        Ok(())
    }
    fn register_with_app(&self, app: &mut bevy::app::App) {
        app.register_foreign_lua_type::<bevy::ecs::entity::Entity>();
        app.register_foreign_lua_type::<bevy::ecs::world::OnAdd>();
        app.register_foreign_lua_type::<bevy::ecs::world::OnInsert>();
        app.register_foreign_lua_type::<bevy::ecs::world::OnRemove>();
        app.register_foreign_lua_type::<bevy::ecs::world::OnReplace>();
        app.register_foreign_lua_type::<bevy::ecs::component::ComponentId>();
        app.register_foreign_lua_type::<bevy::ecs::component::Tick>();
        app.register_foreign_lua_type::<bevy::ecs::component::ComponentTicks>();
        app.register_foreign_lua_type::<bevy::ecs::identifier::Identifier>();
        app.register_foreign_lua_type::<bevy::ecs::entity::EntityHash>();
        app.register_foreign_lua_type::<
                bevy::ecs::removal_detection::RemovedComponentEntity,
            >();
        app.register_foreign_lua_type::<bevy::ecs::system::SystemIdMarker>();
    }
}