Skip to content

Commit 56fa757

Browse files
committed
change core functions to script functions
1 parent 4af4331 commit 56fa757

File tree

4 files changed

+36
-18
lines changed

4 files changed

+36
-18
lines changed

assets/scripts/bevy_api.lua

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ function on_event()
1515
print(script)
1616
print(world)
1717

18-
print(world:hello(entity, entity))
19-
print(world:test_vec({entity, entity})[1])
18+
-- print(world:hello(entity, entity))
19+
-- print(world:test_vec({entity, entity})[1])
2020

2121

2222
local my_component_type = world:get_type_by_name("MyComponent")
@@ -34,6 +34,7 @@ function on_event()
3434
print(comp.option_usize)
3535
print("\nvec")
3636
-- print(table_to_string(comp.vec_of_usize))
37+
print(comp.vec_of_usize)
3738
comp.vec_of_usize = {42,69,72}
3839
comp.vec_of_usize[1] = 612312312
3940
print(table_to_string(comp.vec_of_usize))

crates/bevy_mod_scripting_core/src/bindings/function/from.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ impl FromScript for ScriptValue {
3232
}
3333
}
3434

35+
impl FromScript for () {
36+
type This<'w> = Self;
37+
fn from_script(value: ScriptValue, _world: WorldGuard) -> Result<Self, InteropError> {
38+
Ok(())
39+
}
40+
}
41+
3542
macro_rules! impl_from_with_downcast {
3643
($($ty:ty),*) => {
3744
$(

crates/bevy_mod_scripting_core/src/bindings/function/into.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ impl IntoScript for ScriptValue {
2323
}
2424
}
2525

26+
impl IntoScript for () {
27+
fn into_script(self, _world: WorldGuard) -> Result<ScriptValue, InteropError> {
28+
Ok(ScriptValue::Unit)
29+
}
30+
}
31+
2632
macro_rules! impl_into_with_downcast {
2733
($variant:tt as $cast:ty [$($ty:ty),*]) => {
2834
$(

crates/bevy_mod_scripting_functions/src/core.rs

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -66,30 +66,34 @@ impl Plugin for CoreFunctionsPlugin {
6666

6767
fn register_world_functions(reg: &mut FunctionRegistry) -> Result<(), FunctionRegistrationError> {
6868
NamespaceBuilder::<WorldCallbackAccess>::new(reg)
69-
.overwrite_script_function("hello", |b: Ref<Entity>, c: Mut<Entity>| None::<usize>)
70-
.overwrite(
71-
"test_vec",
72-
|s: WorldCallbackAccess, entities: Vec<Entity>| entities,
73-
)
74-
.overwrite("spawn", |s: WorldCallbackAccess| s.spawn())
75-
.overwrite(
69+
// .overwrite_script_function("hello", |b: Ref<Entity>, c: Mut<Entity>| None::<usize>)
70+
// .overwrite(
71+
// "test_vec",
72+
// |s: WorldCallbackAccess, entities: Vec<Entity>| entities,
73+
// )
74+
.overwrite_script_function("spawn", |s: WorldCallbackAccess| Val(s.spawn()))
75+
.overwrite_script_function(
7676
"get_type_by_name",
77-
|world: WorldCallbackAccess, type_name: String| world.get_type_by_name(type_name),
77+
|world: WorldCallbackAccess, type_name: String| {
78+
world.get_type_by_name(type_name).map(Val)
79+
},
7880
)
79-
.overwrite(
81+
.overwrite_script_function(
8082
"get_component",
81-
|world: WorldCallbackAccess, entity: Entity, registration: ScriptTypeRegistration| {
83+
|world: WorldCallbackAccess,
84+
entity: Val<Entity>,
85+
registration: Val<ScriptTypeRegistration>| {
8286
let s: ScriptValue = registration
8387
.component_id()
84-
.and_then(|id| world.get_component(entity, id).transpose())
88+
.and_then(|id| world.get_component(*entity, id).transpose())
8589
.into();
8690
s
8791
},
8892
)
89-
.overwrite("exit", |s: WorldCallbackAccess| s.exit());
93+
.overwrite_script_function("exit", |s: WorldCallbackAccess| s.exit());
9094

9195
NamespaceBuilder::<ReflectReference>::new(reg)
92-
.overwrite(
96+
.overwrite_script_function(
9397
"get",
9498
|world: WorldCallbackAccess, self_: ScriptValue, key: ScriptValue| {
9599
if let ScriptValue::Reference(mut r) = self_ {
@@ -103,7 +107,7 @@ fn register_world_functions(reg: &mut FunctionRegistry) -> Result<(), FunctionRe
103107
}
104108
},
105109
)
106-
.overwrite(
110+
.overwrite_script_function(
107111
"get_1_indexed",
108112
|world: WorldCallbackAccess, self_: ScriptValue, key: ScriptValue| {
109113
if let ScriptValue::Reference(mut r) = self_ {
@@ -118,7 +122,7 @@ fn register_world_functions(reg: &mut FunctionRegistry) -> Result<(), FunctionRe
118122
}
119123
},
120124
)
121-
.overwrite(
125+
.overwrite_script_function(
122126
"set",
123127
|world: WorldCallbackAccess,
124128
self_: ScriptValue,
@@ -151,7 +155,7 @@ fn register_world_functions(reg: &mut FunctionRegistry) -> Result<(), FunctionRe
151155
ScriptValue::Unit
152156
},
153157
)
154-
.overwrite(
158+
.overwrite_script_function(
155159
"set_1_indexed",
156160
|world: WorldCallbackAccess,
157161
self_: ScriptValue,

0 commit comments

Comments
 (0)