Skip to content

Commit 8bfd501

Browse files
committed
DYNAMIC CALLS WOOOOOOO
1 parent 3c37ff2 commit 8bfd501

40 files changed

+3533
-34820
lines changed

Cargo.toml

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,13 @@ bevy_mod_scripting_core = { workspace = true }
5757
bevy_mod_scripting_lua = { path = "crates/languages/bevy_mod_scripting_lua", version = "0.8.0-alpha.2", optional = true }
5858
bevy_mod_scripting_rhai = { path = "crates/languages/bevy_mod_scripting_rhai", version = "0.8.0-alpha.2", optional = true }
5959
bevy_mod_scripting_rune = { path = "crates/languages/bevy_mod_scripting_rune", version = "0.8.0-alpha.2", optional = true }
60-
60+
bevy_mod_scripting_functions = { workspace = true }
6161

6262
[workspace.dependencies]
6363
bevy = { version = "0.15.0", default-features = false }
6464
bevy_mod_scripting_core = { path = "crates/bevy_mod_scripting_core", version = "0.8.0-alpha.2" }
6565
bevy_mod_scripting_common = { path = "crates/bevy_mod_scripting_common", version = "0.8.0-alpha.2" }
66+
bevy_mod_scripting_functions = { path = "crates/bevy_mod_scripting_functions", version = "0.8.0-alpha.2" }
6667
test_utils = { path = "crates/test_utils" }
6768

6869
[dev-dependencies]
@@ -84,6 +85,7 @@ members = [
8485
"crates/bevy_mod_scripting_common",
8586
"crates/bevy_mod_scripting_derive",
8687
"crates/test_utils",
88+
"crates/bevy_mod_scripting_functions",
8789
]
8890
resolver = "2"
8991
exclude = ["crates/bevy_api_gen", "crates/macro_tests"]
@@ -106,20 +108,12 @@ debug = false
106108
[[example]]
107109
name = "console_integration_lua"
108110
path = "examples/lua/console_integration.rs"
109-
required-features = [
110-
"lua54",
111-
"bevy/file_watcher",
112-
"bevy/multi_threaded",
113-
]
111+
required-features = ["lua54", "bevy/file_watcher", "bevy/multi_threaded"]
114112

115113
[[example]]
116114
name = "console_integration_rhai"
117115
path = "examples/rhai/console_integration.rs"
118-
required-features = [
119-
"rhai",
120-
"bevy/file_watcher",
121-
"bevy/multi_threaded",
122-
]
116+
required-features = ["rhai", "bevy/file_watcher", "bevy/multi_threaded"]
123117

124118
[[example]]
125119
name = "complex_game_loop_lua"

assets/scripts/bevy_api.lua

Lines changed: 105 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -9,141 +9,145 @@ end
99

1010
function on_event()
1111

12-
print(entity)
13-
print(script)
14-
print(world)
12+
world:spawn()
1513

14+
world:exit()
1615

17-
local my_component_type = world:get_type_by_name("MyComponent")
16+
-- print(entity)
17+
-- print(script)
18+
-- print(world)
1819

19-
local comp = world:get_component(entity, my_component_type)
20-
print("Before script: ", comp)
2120

22-
print("\noption")
23-
print(comp.option_usize)
24-
comp.option_usize = 69
25-
print(comp.option_usize)
26-
comp.option_usize = nil
27-
print(comp.option_usize)
21+
-- local my_component_type = world:get_type_by_name("MyComponent")
2822

29-
print("\nvec")
30-
print(table_to_string(comp.vec_of_usize))
31-
comp.vec_of_usize = {42,69,72}
32-
comp.vec_of_usize[1] = 0
33-
print(comp.vec_of_usize[2])
34-
print(table_to_string(comp.vec_of_usize))
35-
comp.vec_of_usize = {}
36-
print(table_to_string(comp.vec_of_usize))
37-
comp.vec_of_usize = comp.vec_of_usize2
38-
print(table_to_string(comp.vec_of_usize))
39-
comp.vec_of_usize = comp.vec_of_usize
40-
print(table_to_string(comp.vec_of_usize))
41-
comp.vec_of_usize:insert(1, 42)
42-
print(table_to_string(comp.vec_of_usize))
23+
-- local comp = world:get_component(entity, my_component_type)
24+
-- print("Before script: ", comp)
4325

44-
print("\nmap")
45-
-- print(comp.map_of_strings["key"])
46-
comp.map_of_strings:insert("key2", "value")
47-
-- print(comp.map_of_strings["key2"])
26+
-- print("\noption")
27+
-- print(comp.option_usize)
28+
-- comp.option_usize = 69
29+
-- print(comp.option_usize)
30+
-- comp.option_usize = nil
31+
-- print(comp.option_usize)
4832

33+
-- print("\nvec")
34+
-- print(table_to_string(comp.vec_of_usize))
35+
-- comp.vec_of_usize = {42,69,72}
36+
-- comp.vec_of_usize[1] = 0
37+
-- print(comp.vec_of_usize[2])
38+
-- print(table_to_string(comp.vec_of_usize))
39+
-- comp.vec_of_usize = {}
40+
-- print(table_to_string(comp.vec_of_usize))
41+
-- comp.vec_of_usize = comp.vec_of_usize2
42+
-- print(table_to_string(comp.vec_of_usize))
43+
-- comp.vec_of_usize = comp.vec_of_usize
44+
-- print(table_to_string(comp.vec_of_usize))
45+
-- comp.vec_of_usize:insert(1, 42)
46+
-- print(table_to_string(comp.vec_of_usize))
4947

48+
-- print("\nmap")
49+
-- -- print(comp.map_of_strings["key"])
50+
-- comp.map_of_strings:insert("key2", "value")
51+
-- -- print(comp.map_of_strings["key2"])
5052

51-
print("============")
5253

53-
-- vec's and matrices have custom __index and __newindex overrides
54-
print("comp.vec2 before: ", comp.vec2)
55-
comp.vec2[1] = 69
56-
print("comp.vec2 after: ", comp.vec2)
5754

58-
-- Option's get converted to nil or the value inside
59-
print("comp.option_vec3 before: ", comp.option_vec3)
60-
comp.option_vec3 = Vec3.new(2,1,3)
61-
print("comp.option_vec3 after: ", comp.option_vec3)
55+
-- print("============")
6256

63-
-- reflection via index is indexed starting at 1, unlike in Rust to match Lua's indexing
64-
print("comp.option_vec3[1] before: ", comp.option_vec3[1])
65-
comp.option_vec3[1] = 5
66-
print("comp.option_vec3[1] after: ", comp.option_vec3[1])
57+
-- -- vec's and matrices have custom __index and __newindex overrides
58+
-- print("comp.vec2 before: ", comp.vec2)
59+
-- comp.vec2[1] = 69
60+
-- print("comp.vec2 after: ", comp.vec2)
6761

68-
print("============")
62+
-- -- Option's get converted to nil or the value inside
63+
-- print("comp.option_vec3 before: ", comp.option_vec3)
64+
-- comp.option_vec3 = Vec3.new(2,1,3)
65+
-- print("comp.option_vec3 after: ", comp.option_vec3)
6966

70-
-- Vec<T> references get converted to a custom proxy `LuaVec<T>` which is
71-
-- also assignable via lua tables
67+
-- -- reflection via index is indexed starting at 1, unlike in Rust to match Lua's indexing
68+
-- print("comp.option_vec3[1] before: ", comp.option_vec3[1])
69+
-- comp.option_vec3[1] = 5
70+
-- print("comp.option_vec3[1] after: ", comp.option_vec3[1])
7271

73-
print("comp.vec_of_option_bools before: ", table_to_string(comp.vec_of_option_bools))
74-
comp.vec_of_option_bools = {true,false,true}
75-
print("comp.vec_of_option_bools after assignment: ", table_to_string(comp.vec_of_option_bools))
72+
-- print("============")
7673

77-
print("comp.vec_of_option_bools[1] before: ", comp.vec_of_option_bools[1])
78-
comp.vec_of_option_bools[1] = false
79-
print("comp.vec_of_option_bools[1] after: ", comp.vec_of_option_bools[1])
74+
-- -- Vec<T> references get converted to a custom proxy `LuaVec<T>` which is
75+
-- -- also assignable via lua tables
8076

81-
-- there are some additional methods available on LuaVec proxies imitating the Vec<T> api
82-
print("comp.vec_of_option_bools before insert: ", table_to_string(comp.vec_of_option_bools))
83-
comp.vec_of_option_bools:insert(1,nil)
84-
print("comp.vec_of_option_bools after insert: ", table_to_string(comp.vec_of_option_bools))
77+
-- print("comp.vec_of_option_bools before: ", table_to_string(comp.vec_of_option_bools))
78+
-- comp.vec_of_option_bools = {true,false,true}
79+
-- print("comp.vec_of_option_bools after assignment: ", table_to_string(comp.vec_of_option_bools))
8580

81+
-- print("comp.vec_of_option_bools[1] before: ", comp.vec_of_option_bools[1])
82+
-- comp.vec_of_option_bools[1] = false
83+
-- print("comp.vec_of_option_bools[1] after: ", comp.vec_of_option_bools[1])
8684

85+
-- -- there are some additional methods available on LuaVec proxies imitating the Vec<T> api
86+
-- print("comp.vec_of_option_bools before insert: ", table_to_string(comp.vec_of_option_bools))
87+
-- comp.vec_of_option_bools:insert(1,nil)
88+
-- print("comp.vec_of_option_bools after insert: ", table_to_string(comp.vec_of_option_bools))
8789

88-
print("comp.vec_of_option_bools before push: ", table_to_string(comp.vec_of_option_bools))
89-
comp.vec_of_option_bools:push(false)
90-
print("comp.vec_of_option_bools after push: ", table_to_string(comp.vec_of_option_bools))
9190

92-
print("comp.vec_of_option_bools len after push: ", #comp.vec_of_option_bools)
9391

94-
print("comp.vec_of_option_bools before pop: ", table_to_string(comp.vec_of_option_bools))
95-
print(comp.vec_of_option_bools:pop():print_value())
96-
print("comp.vec_of_option_bools after pop: ", table_to_string(comp.vec_of_option_bools))
92+
-- print("comp.vec_of_option_bools before push: ", table_to_string(comp.vec_of_option_bools))
93+
-- comp.vec_of_option_bools:push(false)
94+
-- print("comp.vec_of_option_bools after push: ", table_to_string(comp.vec_of_option_bools))
9795

98-
print("the pairs inside comp.vec_of_option_bools: ")
99-
for k,v in pairs(comp.vec_of_option_bools) do
100-
print(string.format(" - %s:%s",k,v))
101-
end
96+
-- print("comp.vec_of_option_bools len after push: ", #comp.vec_of_option_bools)
97+
98+
-- print("comp.vec_of_option_bools before pop: ", table_to_string(comp.vec_of_option_bools))
99+
-- print(comp.vec_of_option_bools:pop():print_value())
100+
-- print("comp.vec_of_option_bools after pop: ", table_to_string(comp.vec_of_option_bools))
102101

102+
-- print("the pairs inside comp.vec_of_option_bools: ")
103+
-- for k,v in pairs(comp.vec_of_option_bools) do
104+
-- print(string.format(" - %s:%s",k,v))
105+
-- end
103106

104-
comp.vec_of_option_bools:clear()
105-
print("comp.vec_of_option_bools after clear: ", table_to_string(comp.vec_of_option_bools))
106-
print("comp.vec_of_option_bools len after clear: ", #comp.vec_of_option_bools)
107107

108-
print("============")
108+
-- comp.vec_of_option_bools:clear()
109+
-- print("comp.vec_of_option_bools after clear: ", table_to_string(comp.vec_of_option_bools))
110+
-- print("comp.vec_of_option_bools len after clear: ", #comp.vec_of_option_bools)
109111

110-
print(Vec3.new(0,1,0) + Vec3.new(1,0,0))
111-
print(Vec3.new(0,1,0):any_orthonormal_vector())
112-
print(comp.mat3[1])
113-
print(Vec3.new(0,1,0):any_orthonormal_vector() + comp.mat3[1])
114-
local complex_vec_op = Vec3.new(0,1,0):any_orthonormal_vector() + comp.mat3[1]
115-
print("(0,1,0).any_orthonormal_vector() + mat3.x_axis is: ", complex_vec_op)
112+
-- print("============")
116113

117-
local new_mat3 = Mat3.from_cols(Vec3.new(1,0,0),Vec3.new(0,1,0),Vec3.new(0,0,-1))
118-
print("new_mat3 is:", new_mat3)
114+
-- print(Vec3.new(0,1,0) + Vec3.new(1,0,0))
115+
-- print(Vec3.new(0,1,0):any_orthonormal_vector())
116+
-- print(comp.mat3[1])
117+
-- print(Vec3.new(0,1,0):any_orthonormal_vector() + comp.mat3[1])
118+
-- local complex_vec_op = Vec3.new(0,1,0):any_orthonormal_vector() + comp.mat3[1]
119+
-- print("(0,1,0).any_orthonormal_vector() + mat3.x_axis is: ", complex_vec_op)
120+
121+
-- local new_mat3 = Mat3.from_cols(Vec3.new(1,0,0),Vec3.new(0,1,0),Vec3.new(0,0,-1))
122+
-- print("new_mat3 is:", new_mat3)
119123

120-
comp.vec2 = comp.vec2 + comp.vec2
121-
print("A")
122-
comp.usize = comp.vec2:min_element()
124+
-- comp.vec2 = comp.vec2 + comp.vec2
125+
-- print("A")
126+
-- comp.usize = comp.vec2:min_element()
123127

124-
print("B")
125-
comp.f32 = comp.f32 + comp.f32 + comp.vec2:min_element()
126-
print("C")
127-
comp.vec2 = Vec2.new(2,1)
128-
print("D")
129-
comp.quat = Quat.from_xyzw(3,2,1,4)
130-
print("E")
131-
comp.mat3[1] = Vec3.new(69,69,69)
132-
print("F")
128+
-- print("B")
129+
-- comp.f32 = comp.f32 + comp.f32 + comp.vec2:min_element()
130+
-- print("C")
131+
-- comp.vec2 = Vec2.new(2,1)
132+
-- print("D")
133+
-- comp.quat = Quat.from_xyzw(3,2,1,4)
134+
-- print("E")
135+
-- comp.mat3[1] = Vec3.new(69,69,69)
136+
-- print("F")
133137

134138

135-
world:exit()
136-
do return end
137-
print("============")
139+
-- world:exit()
140+
-- do return end
141+
-- print("============")
138142

139-
-- this is an example of something impossible to achieve with plain bevy reflection under the hood
140-
comp.mat3[1][1] = 42
143+
-- -- this is an example of something impossible to achieve with plain bevy reflection under the hood
144+
-- comp.mat3[1][1] = 42
141145

142-
-- now let's retrieve these again to see if we actually changed their values permanently
143-
comp = world:get_component(entity,my_component_type)
146+
-- -- now let's retrieve these again to see if we actually changed their values permanently
147+
-- comp = world:get_component(entity,my_component_type)
144148

145-
print("After script:")
146-
print(comp)
149+
-- print("After script:")
150+
-- print(comp)
147151

148-
world:exit()
152+
-- world:exit()
149153
end

crates/bevy_mod_scripting_core/src/bindings/access_map.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ impl ReflectAccessId {
137137
ReflectBase::Resource(id) => Some(Self::for_component_id(id)),
138138
ReflectBase::Component(_, id) => Some(Self::for_component_id(id)),
139139
ReflectBase::Owned(id) => Some(Self::for_allocation(id)),
140+
ReflectBase::World => None,
140141
}
141142
}
142143
}

crates/bevy_mod_scripting_core/src/bindings/allocator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::collections::HashMap;
77
use std::fmt::{Display, Formatter};
88
use std::sync::Arc;
99

10-
#[derive(Clone, PartialEq, Eq, Hash, Debug)]
10+
#[derive(Clone, PartialEq, Eq, Hash, Debug, PartialOrd, Ord)]
1111
pub struct ReflectAllocationId(pub(crate) Arc<usize>);
1212
impl ReflectAllocationId {
1313
pub fn id(&self) -> usize {

0 commit comments

Comments
 (0)