Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,17 @@ rhai = ["bevy_mod_scripting_rhai"]
rhai_script_api=["bevy_script_api/rhai"]

[dependencies]
bevy = { version = "0.9", default-features = false}
bevy = { version = "0.10.1", default-features = false}
bevy_mod_scripting_core = { path = "bevy_mod_scripting_core", version = "0.2.2" }
bevy_mod_scripting_lua = { path = "languages/bevy_mod_scripting_lua", version = "0.2.2", optional = true }
bevy_mod_scripting_rhai = { path = "languages/bevy_mod_scripting_rhai", version = "0.2.2", optional = true}
bevy_script_api = { path = "bevy_script_api", version = "0.2.2", optional = true }

[dev-dependencies]
bevy = { version = "0.9"}
bevy = { version = "0.10.1"}
clap = { version = "4.1", features = ["derive"]}
rand = "0.8.5"
bevy_console = "0.5.0"
bevy_console = "0.7.0"
rhai-rand = "0.1"

[workspace]
Expand Down
22 changes: 0 additions & 22 deletions api_gen_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,6 @@ source="bevy_ui"
type="Button"
source="bevy_ui"

[[types]]
type="ImageMode"
source="bevy_ui"

[[types]]
type="Display"
source="bevy_ui"
Expand Down Expand Up @@ -211,10 +207,6 @@ source="bevy_hierarchy"
type="Text2dBounds"
source="bevy_text"

[[types]]
type="Text2dSize"
source="bevy_text"

[[types]]
type="Text"
source="bevy_text"
Expand All @@ -231,16 +223,6 @@ source="bevy_text"
type="TextStyle"
source="bevy_text"

[[types]]
type="HorizontalAlign"
source="bevy_text"

[[types]]
type="VerticalAlign"
source="bevy_text"



## BEVY_TIME

[[types]]
Expand Down Expand Up @@ -423,10 +405,6 @@ source="bevy_render"
type="ScalingMode"
source="bevy_render"

[[types]]
type="WindowOrigin"
source="bevy_render"

[[types]]
type="Color"
source="bevy_render"
Expand Down
4 changes: 2 additions & 2 deletions assets/scripts/game_of_life.tl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ math.randomseed(os.time())

global function init()
local LifeState = world:get_type_by_name("LifeState")
local life_state = world:get_component(entity,LifeState) as types.LuaLifeState
local life_state = world:get_component(entity,LifeState) as BevyAPI.LuaLifeState
local cells = life_state.cells as {integer}

-- set some cells alive
Expand All @@ -16,7 +16,7 @@ global function on_update()
local LifeState = world:get_type_by_name("LifeState")
local Settings = world:get_type_by_name("Settings")

local life_state = world:get_component(entity,LifeState) as types.LuaLifeState
local life_state = world:get_component(entity,LifeState) as BevyAPI.LuaLifeState
local cells = life_state.cells as {integer}

-- note that here we do not make use of LuaProxyable and just go off pure reflection
Expand Down
4 changes: 2 additions & 2 deletions bevy_api_gen/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::collections::HashSet;

use clap::Parser;
use indexmap::IndexMap;
use indexmap::{IndexMap, IndexSet};
use rustdoc_types::{Crate, Item, ItemEnum, Visibility};
use serde::Deserialize;

Expand Down Expand Up @@ -36,7 +36,7 @@ pub struct Config {

/// Describes the set of non generic things which are representible
/// as simple lua types and don't need UserData proxies
pub primitives: HashSet<String>,
pub primitives: IndexSet<String>,

pub manual_lua_types: Vec<ManualLuaType>,
}
Expand Down
2 changes: 1 addition & 1 deletion bevy_api_gen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ pub(crate) fn generate_macros(
// get_doc_fragment
writer.write_no_newline("fn get_doc_fragment(&self) -> Option<Self::DocTarget>");
writer.open_brace();
writer.write_no_newline("Some(\"BevyAPI\",LuaDocFragment::new(|tw|");
writer.write_no_newline("Some(LuaDocFragment::new(\"BevyAPI\", |tw|");
writer.open_brace();
writer.write_line("tw");
writer.write_line(".document_global_instance::<BevyAPIGlobals>().expect(\"Something went wrong documenting globals\")");
Expand Down
4 changes: 1 addition & 3 deletions bevy_api_gen/src/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,7 @@ impl PrettyWriter {

/// Writes indentation and prefix without a newline
fn write_indented_prefix(&mut self) {
(0..self.state.indentation_level)
.into_iter()
.for_each(|_| self.output.push('\t'));
(0..self.state.indentation_level).for_each(|_| self.output.push('\t'));

if let Some(prefix) = &self.state.prefix {
self.output.push_str(prefix);
Expand Down
2 changes: 1 addition & 1 deletion bevy_event_priority/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ name = "bevy_event_priority"
path = "src/lib.rs"

[dependencies]
bevy = { version = "0.9", default-features = false}
bevy = { version = "0.10.1", default-features = false}

2 changes: 1 addition & 1 deletion bevy_mod_scripting_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ doc_always = []


[dependencies]
bevy = { version = "0.9", default-features = false, features=["bevy_asset","bevy_gltf","bevy_animation","bevy_core_pipeline","bevy_ui","bevy_pbr","bevy_render","bevy_text","bevy_sprite","filesystem_watcher"]}
bevy = { version = "0.10.1", default-features = false, features=["bevy_asset","bevy_gltf","bevy_animation","bevy_core_pipeline","bevy_ui","bevy_pbr","bevy_render","bevy_text","bevy_sprite","filesystem_watcher"]}
bevy_event_priority = {path = "../bevy_event_priority", version = "0.2.2" }
thiserror = "1.0.31"
paste = "1.0.7"
Expand Down
18 changes: 14 additions & 4 deletions bevy_mod_scripting_core/src/hosts.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
//! All script host related stuff
use bevy::{asset::Asset, prelude::*, reflect::FromReflect};
use bevy::{
asset::Asset,
ecs::schedule::{BaseSystemSet, FreeSystemSet},
prelude::*,
reflect::FromReflect,
};
use std::{
collections::HashMap,
iter::once,
Expand Down Expand Up @@ -124,10 +129,15 @@ pub trait ScriptHost: Send + Sync + 'static + Default + Resource {
Ok(())
}

/// Registers the script host with the given app, and attaches handlers to deal with spawning/removing scripts at the given stage.
/// Registers the script host with the given app, and attaches handlers to deal with spawning/removing scripts in the given System Set.
///
/// Ideally place after any game logic which can spawn/remove/modify scripts to avoid frame lag. (typically `CoreSet::Post_Update`)
fn register_with_app_in_set(app: &mut App, set: impl FreeSystemSet);

/// Registers the script host with the given app, and attaches handlers to deal with spawning/removing scripts in the given Base System Set.
///
/// Ideally place after any game logic which can spawn/remove/modify scripts to avoid frame lag. (typically `CoreStage::Post_Update`)
fn register_with_app(app: &mut App, stage: impl StageLabel);
/// Ideally place after any game logic which can spawn/remove/modify scripts to avoid frame lag. (typically `CoreSet::Post_Update`)
fn register_with_app_in_base_set(app: &mut App, set: impl BaseSystemSet);
}

/// Implementors can modify a script context in order to enable
Expand Down
109 changes: 63 additions & 46 deletions bevy_mod_scripting_core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ use crate::{
event::ScriptErrorEvent,
hosts::{APIProvider, APIProviders, ScriptHost},
};
use bevy::{ecs::schedule::IntoRunCriteria, prelude::*};
use bevy::{
ecs::schedule::{BaseSystemSet, FreeSystemSet},
prelude::*,
};
use event::ScriptLoaded;
use systems::{script_event_handler, ScriptSystemLabel};
use systems::script_event_handler;

pub mod asset;
pub mod docs;
Expand All @@ -24,6 +27,7 @@ pub mod prelude {
APIProvider, APIProviders, Recipients, Script, ScriptCollection, ScriptContexts,
ScriptData, ScriptHost,
},
crate::systems::script_event_handler,
crate::{
AddScriptApiProvider, AddScriptHost, AddScriptHostHandler, GenDocumentation,
ScriptingPlugin,
Expand Down Expand Up @@ -71,19 +75,41 @@ impl GenDocumentation for App {
/// Trait for app builder notation
pub trait AddScriptHost {
/// registers the given script host with your app,
/// the given stage will contain systems handling script loading,re-loading, removal etc.
/// This stage will also send events related to the script lifecycle.
/// Any systems which need to run the same frame a script is loaded must run after this stage.
fn add_script_host<T: ScriptHost, S: StageLabel>(&mut self, stage: S) -> &mut Self;
/// the given system set will contain systems handling script loading, re-loading, removal etc.
/// This system set will also send events related to the script lifecycle.
/// Any systems which need to run the same frame a script is loaded must run after this set.
fn add_script_host_to_set<T: ScriptHost, S: FreeSystemSet + Clone>(
&mut self,
set: S,
) -> &mut Self;

/// registers the given script host with your app,
/// the given base set will contain systems handling script loading, re-loading, removal etc.
/// This set will also send events related to the script lifecycle.
/// Any systems which need to run the same frame a script is loaded must run after this set.
fn add_script_host_to_base_set<T: ScriptHost, S: BaseSystemSet + Clone>(
&mut self,
set: S,
) -> &mut Self;
}

impl AddScriptHost for App {
fn add_script_host<T, S>(&mut self, stage: S) -> &mut Self
fn add_script_host_to_set<T, S>(&mut self, set: S) -> &mut Self
where
T: ScriptHost,
S: FreeSystemSet + Clone,
{
T::register_with_app_in_set(self, set);
self.init_resource::<T>();
self.add_event::<ScriptLoaded>();
self
}
fn add_script_host_to_base_set<T, S>(&mut self, set: S) -> &mut Self
where
T: ScriptHost,
S: StageLabel,
S: BaseSystemSet + Clone,
{
T::register_with_app(self, stage);
T::register_with_app_in_base_set(self, set);
self.init_resource::<T>();
self.add_event::<ScriptLoaded>();
self
Expand Down Expand Up @@ -124,14 +150,20 @@ impl AddScriptApiProvider for App {

pub trait AddScriptHostHandler {
/// Enables this script host to handle events with priorities in the range [0,min_prio] (inclusive),
/// during the runtime of the given stage.
/// during from within the given set.
///
/// Think of handler stages as a way to run certain types of events at various points in your engine.
/// Note: this is identical to adding the script_event_handler system manually, so if you require setting schedules etc, you should use that directly.
/// ```rust,ignore
/// self.add_system(
/// script_event_handler::<T, MAX, MIN>.in_set(set).in_schedule(MySchedule::SomeSchedule)
/// );
/// ```
/// Think of handler system sets as a way to run certain types of events at specific points in your engine.
/// A good example of this is Unity [game loop's](https://docs.unity3d.com/Manual/ExecutionOrder.html) `onUpdate` and `onFixedUpdate`.
/// FixedUpdate runs *before* any physics while Update runs after physics and input events.
///
/// A similar setup can be achieved by using a separate stage before and after your physics,
/// then assigning event priorities such that your events are forced to run at a particular stage, for example:
/// A similar setup can be achieved by using a separate system set before and after your physics,
/// then assigning event priorities such that your events are forced to run at a particular system set, for example:
///
/// PrePhysics: min_prio = 1
/// PostPhysics: min_prio = 4
Expand All @@ -144,61 +176,46 @@ pub trait AddScriptHostHandler {
/// | 3 | PostPhysics | OnMouse |
/// | 4 | PostPhysics | Update |
///
/// The *frequency* of running these events, is controlled by your systems, if the event is not emitted, it cannot not handled.
/// The *frequency* of running these events, is controlled by your systems, if the event is not emitted, it cannot be handled.
/// Of course there is nothing stopping your from emitting a single event type at varying priorities.
fn add_script_handler_stage<T: ScriptHost, S: StageLabel, const MAX: u32, const MIN: u32>(
fn add_script_handler_to_set<T: ScriptHost, S: FreeSystemSet, const MAX: u32, const MIN: u32>(
&mut self,
stage: S,
set: S,
) -> &mut Self;

/// Like `add_script_handler_stage` but with additional run criteria
fn add_script_handler_stage_with_criteria<
fn add_script_handler_to_base_set<
T: ScriptHost,
S: StageLabel,
M,
C: IntoRunCriteria<M>,
S: BaseSystemSet,
const MAX: u32,
const MIN: u32,
>(
&mut self,
stage: S,
criteria: C,
set: S,
) -> &mut Self;
}

impl AddScriptHostHandler for App {
fn add_script_handler_stage<T: ScriptHost, S: StageLabel, const MAX: u32, const MIN: u32>(
fn add_script_handler_to_set<
T: ScriptHost,
S: FreeSystemSet,
const MAX: u32,
const MIN: u32,
>(
&mut self,
stage: S,
set: S,
) -> &mut Self {
self.add_system_to_stage(
stage,
script_event_handler::<T, MAX, MIN>
.label(ScriptSystemLabel::EventHandling)
.at_end(),
);
self.add_system(script_event_handler::<T, MAX, MIN>.in_set(set));
self
}

fn add_script_handler_stage_with_criteria<
fn add_script_handler_to_base_set<
T: ScriptHost,
S: StageLabel,
M,
C: IntoRunCriteria<M>,
S: BaseSystemSet,
const MAX: u32,
const MIN: u32,
>(
&mut self,
stage: S,
criteria: C,
set: S,
) -> &mut Self {
self.add_system_to_stage(
stage,
script_event_handler::<T, MAX, MIN>
.label(ScriptSystemLabel::EventHandling)
.at_end()
.with_run_criteria(criteria),
);
self.add_system(script_event_handler::<T, MAX, MIN>.in_base_set(set));
self
}
}
Loading