Skip to content

Commit 8b596c8

Browse files
authored
feat: registered callbacks via register_callback, and bevy_mod_scripting_script crate. (#490)
1 parent d38c3fd commit 8b596c8

File tree

62 files changed

+864
-396
lines changed

Some content is hidden

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

62 files changed

+864
-396
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/allocator.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,13 +195,13 @@ pub struct ReflectAllocator {
195195

196196
#[profiling::all_functions]
197197
impl ReflectAllocator {
198-
/// Allocates a new [`Reflect`] value and returns an [`AllocationId`] which can be used to access it later.
198+
/// Allocates a new `Reflect` value and returns an [`ReflectAllocationId`] which can be used to access it later.
199199
/// Use [`Self::allocate_boxed`] if you already have an allocated boxed value.
200200
pub fn allocate<T: PartialReflect>(&mut self, value: T) -> ReflectAllocationId {
201201
self.allocate_boxed(Box::new(value))
202202
}
203203

204-
/// Allocates a new boxed [`PartialReflect`] value and returns an [`AllocationId`] which can be used to access it later.
204+
/// Allocates a new boxed `PartialReflect` value and returns an [`ReflectAllocationId`] which can be used to access it later.
205205
pub fn allocate_boxed(&mut self, value: Box<dyn PartialReflect>) -> ReflectAllocationId {
206206
static COUNTER: AtomicU64 = AtomicU64::new(0);
207207

@@ -236,7 +236,7 @@ impl ReflectAllocator {
236236
self.allocations.get(id)
237237
}
238238

239-
/// Deallocates the [`PartialReflect`] value with the given [`AllocationId`]
239+
/// Deallocates the `PartialReflect` value with the given [`ReflectAllocationId`]
240240
pub fn deallocate(&mut self, id: &ReflectAllocationId) {
241241
self.allocations.remove(id);
242242
}

crates/bevy_mod_scripting_bindings/src/docgen/typed_through.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
//! Defines a set of traits which destruture [`bevy::reflect::TypeInfo`] and implement a light weight wrapper around it, to allow types
2-
//! which normally can't implement [`bevy::reflect::Typed`] to be used in a reflection context.
1+
//! Defines a set of traits which destruture [`bevy_reflect::TypeInfo`] and implement a light weight wrapper around it, to allow types
2+
//! which normally can't implement [`bevy_reflect::Typed`] to be used in a reflection context.
33
44
use std::{ffi::OsString, path::PathBuf};
55

0 commit comments

Comments
 (0)