Skip to content

Commit e1982c8

Browse files
authored
Merge 36c426d into d38c3fd
2 parents d38c3fd + 36c426d commit e1982c8

File tree

50 files changed

+710
-349
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+710
-349
lines changed

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ bevy_mod_scripting_derive = { workspace = true }
112112
bevy_mod_scripting_asset = { workspace = true }
113113
bevy_mod_scripting_bindings = { workspace = true }
114114
bevy_mod_scripting_display = { workspace = true }
115+
bevy_mod_scripting_script = { workspace = true }
115116

116117
[workspace.dependencies]
117118
# local crates
@@ -127,6 +128,7 @@ bevy_mod_scripting_rhai = { path = "crates/languages/bevy_mod_scripting_rhai", v
127128
bevy_mod_scripting_asset = { path = "crates/bevy_mod_scripting_asset", version = "0.16.0", default-features = false }
128129
bevy_mod_scripting_bindings = { path = "crates/bevy_mod_scripting_bindings", version = "0.16.0", default-features = false }
129130
bevy_mod_scripting_display = { path = "crates/bevy_mod_scripting_display", version = "0.16.0", default-features = false }
131+
bevy_mod_scripting_script = { path = "crates/bevy_mod_scripting_script", version = "0.16.0", default-features = false }
130132
# bevy
131133

132134
bevy_mod_scripting_core = { path = "crates/bevy_mod_scripting_core", version = "0.16.0" }
@@ -261,6 +263,7 @@ members = [
261263
"crates/bevy_mod_scripting_asset",
262264
"crates/bevy_mod_scripting_bindings",
263265
"crates/bevy_mod_scripting_display",
266+
"crates/bevy_mod_scripting_script",
264267
]
265268
resolver = "2"
266269
exclude = ["codegen", "crates/macro_tests", "xtask"]
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function on_script_loaded()
2+
register_callback("on_test", dynamic_on_test)
3+
end
4+
5+
function dynamic_on_test()
6+
register_callback("on_test_last", dynamic_on_test_last)
7+
return "on test: I am dynamically registered from a normal callback!"
8+
end
9+
10+
function dynamic_on_test_last()
11+
return "on test last: I am dynamically registered from another dynamic callback!"
12+
end

assets/tests/register_callback/lua/empty.lua

Whitespace-only changes.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// #main_script dynamic_on_test.lua
2+
SetCurrentLanguage language="@this_script_language"
3+
InstallPlugin miliseconds_budget=999999
4+
SetupHandler OnTest=null, Update=null
5+
SetupHandler OnTestPostUpdate=null, PostUpdate=null
6+
SetupHandler Last=null, OnTestLast=null
7+
FinalizeApp
8+
9+
LoadScriptAs as_name="@this_script", path="@this_script"
10+
WaitForScriptLoaded name="@this_script"
11+
SpawnEntityWithScript name="test_entity", script="@this_script"
12+
RunUpdateOnce
13+
EmitScriptCallbackEvent emit_response=true, entity="test_entity", label="OnTest", language=null, recipients="EntityScript", script="@this_script"
14+
EmitScriptCallbackEvent emit_response=true, entity="test_entity", label="OnTestLast", language=null, recipients="EntityScript", script="@this_script"
15+
RunUpdateOnce
16+
AssertCallbackSuccess attachment="EntityScript", entity="test_entity", label="OnTest", script="@this_script", expect_string_value="on test: I am dynamically registered from a normal callback!"
17+
AssertCallbackSuccess attachment="EntityScript", entity="test_entity", label="OnTestLast", script="@this_script", expect_string_value="on test last: I am dynamically registered from another dynamic callback!"
18+
19+
// reload, deleting old callbacks, expect stored callbacks to still work
20+
ReloadScriptFrom script="@this_script", path="empty.lua"
21+
RunUpdateOnce
22+
EmitScriptCallbackEvent emit_response=true, entity="test_entity", label="OnTest", language=null, recipients="EntityScript", script="@this_script"
23+
EmitScriptCallbackEvent emit_response=true, entity="test_entity", label="OnTestLast", language=null, recipients="EntityScript", script="@this_script"
24+
RunUpdateOnce
25+
AssertCallbackSuccess attachment="EntityScript", entity="test_entity", label="OnTest", script="@this_script", expect_string_value="on test: I am dynamically registered from a normal callback!"
26+
AssertCallbackSuccess attachment="EntityScript", entity="test_entity", label="OnTestLast", script="@this_script", expect_string_value="on test last: I am dynamically registered from another dynamic callback!"
27+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
fn on_script_loaded(){
2+
register_callback("on_test", dynamic_on_test);
3+
}
4+
5+
fn dynamic_on_test(){
6+
register_callback("on_test_last", dynamic_on_test_last);
7+
return "on test: I am dynamically registered from a normal callback!";
8+
}
9+
10+
fn dynamic_on_test_last(){
11+
return "on test last: I am dynamically registered from another dynamic callback!";
12+
}

assets/tests/register_callback/rhai/empty.rhai

Whitespace-only changes.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// #main_script dynamic_on_test.rhai
2+
SetCurrentLanguage language="@this_script_language"
3+
InstallPlugin miliseconds_budget=999999
4+
SetupHandler OnTest=null, Update=null
5+
SetupHandler OnTestPostUpdate=null, PostUpdate=null
6+
SetupHandler Last=null, OnTestLast=null
7+
FinalizeApp
8+
9+
LoadScriptAs as_name="@this_script", path="@this_script"
10+
WaitForScriptLoaded name="@this_script"
11+
SpawnEntityWithScript name="test_entity", script="@this_script"
12+
RunUpdateOnce
13+
EmitScriptCallbackEvent emit_response=true, entity="test_entity", label="OnTest", language=null, recipients="EntityScript", script="@this_script"
14+
EmitScriptCallbackEvent emit_response=true, entity="test_entity", label="OnTestLast", language=null, recipients="EntityScript", script="@this_script"
15+
RunUpdateOnce
16+
AssertCallbackSuccess attachment="EntityScript", entity="test_entity", label="OnTest", script="@this_script", expect_string_value="on test: I am dynamically registered from a normal callback!"
17+
AssertCallbackSuccess attachment="EntityScript", entity="test_entity", label="OnTestLast", script="@this_script", expect_string_value="on test last: I am dynamically registered from another dynamic callback!"
18+
19+
// reload, deleting old callbacks, expect stored callbacks to still work
20+
ReloadScriptFrom script="@this_script", path="empty.rhai"
21+
RunUpdateOnce
22+
EmitScriptCallbackEvent emit_response=true, entity="test_entity", label="OnTest", language=null, recipients="EntityScript", script="@this_script"
23+
EmitScriptCallbackEvent emit_response=true, entity="test_entity", label="OnTestLast", language=null, recipients="EntityScript", script="@this_script"
24+
RunUpdateOnce
25+
AssertCallbackSuccess attachment="EntityScript", entity="test_entity", label="OnTest", script="@this_script", expect_string_value="on test: I am dynamically registered from a normal callback!"
26+
AssertCallbackSuccess attachment="EntityScript", entity="test_entity", label="OnTestLast", script="@this_script", expect_string_value="on test last: I am dynamically registered from another dynamic callback!"
27+

crates/bevy_mod_scripting_bindings/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ readme.workspace = true
1616
bevy_mod_scripting_asset = { workspace = true }
1717
bevy_mod_scripting_derive = { workspace = true }
1818
bevy_mod_scripting_display = { workspace = true }
19+
bevy_mod_scripting_script = { workspace = true }
1920
bevy_system_reflection = { workspace = true }
2021
bevy_diagnostic = { workspace = true }
2122
bevy_ecs = { workspace = true }

crates/bevy_mod_scripting_bindings/src/function/script_function.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use super::MagicFunctions;
44
use super::{from::FromScript, into::IntoScript, namespace::Namespace};
55
use crate::docgen::info::{FunctionInfo, GetFunctionInfo};
66
use crate::function::arg_meta::ArgMeta;
7-
use crate::{ScriptValue, ThreadWorldContainer, WorldContainer, WorldGuard, error::InteropError};
7+
use crate::{ScriptValue, ThreadWorldContainer, WorldGuard, error::InteropError};
88
use bevy_ecs::prelude::Resource;
99
use bevy_mod_scripting_asset::Language;
1010
use bevy_mod_scripting_derive::DebugWithTypeInfo;
@@ -55,7 +55,7 @@ impl FunctionCallContext {
5555
/// Tries to access the world, returning an error if the world is not available
5656
#[profiling::function]
5757
pub fn world<'l>(&self) -> Result<WorldGuard<'l>, InteropError> {
58-
ThreadWorldContainer.try_get_world()
58+
ThreadWorldContainer.try_get_context().map(|c| c.world)
5959
}
6060
/// Whether the caller uses 1-indexing on all indexes and expects 0-indexing conversions to be performed.
6161
#[profiling::function]
@@ -661,12 +661,19 @@ variadics_please::all_tuples!(impl_script_function, 0, 13, T);
661661
#[cfg(test)]
662662
mod test {
663663
use super::*;
664+
use bevy_asset::{AssetId, Handle};
664665
use bevy_ecs::{prelude::Component, world::World};
666+
use bevy_mod_scripting_script::ScriptAttachment;
665667

666668
fn with_local_world<F: Fn()>(f: F) {
667669
let mut world = World::default();
668670
WorldGuard::with_static_guard(&mut world, |world| {
669-
ThreadWorldContainer.set_world(world).unwrap();
671+
ThreadWorldContainer
672+
.set_context(crate::ThreadScriptContext {
673+
world,
674+
attachment: ScriptAttachment::StaticScript(Handle::Weak(AssetId::invalid())),
675+
})
676+
.unwrap();
670677
f()
671678
});
672679
}

crates/bevy_mod_scripting_bindings/src/reference.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66
//! we need wrapper types which have owned and ref variants.
77
use super::{WorldGuard, access_map::ReflectAccessId};
88
use crate::{
9-
ReflectAllocationId, ReflectAllocator, ThreadWorldContainer, WorldContainer,
10-
error::InteropError, reflection_extensions::PartialReflectExt, with_access_read,
11-
with_access_write,
9+
ReflectAllocationId, ReflectAllocator, ThreadWorldContainer, error::InteropError,
10+
reflection_extensions::PartialReflectExt, with_access_read, with_access_write,
1211
};
1312
use bevy_asset::{ReflectAsset, UntypedHandle};
1413
use bevy_ecs::{component::Component, ptr::Ptr, resource::Resource};
@@ -68,7 +67,7 @@ impl DisplayWithTypeInfo for ReflectReference {
6867

6968
let guard = any.downcast_ref::<WorldGuard>().cloned().or_else(|| {
7069
any.downcast_ref::<ThreadWorldContainer>()
71-
.and_then(|t| t.try_get_world().ok())
70+
.and_then(|t| t.try_get_context().ok().map(|c| c.world))
7271
});
7372

7473
if let Some(guard) = guard {
@@ -767,7 +766,7 @@ impl DisplayWithTypeInfo for ReflectBase {
767766

768767
let guard = any.downcast_ref::<WorldGuard>().cloned().or_else(|| {
769768
any.downcast_ref::<ThreadWorldContainer>()
770-
.and_then(|t| t.try_get_world().ok())
769+
.and_then(|t| t.try_get_context().ok().map(|c| c.world))
771770
});
772771

773772
if let Some(guard) = guard {

0 commit comments

Comments
 (0)