@@ -7,122 +7,135 @@ function table_to_string(t)
77end
88
99
10- -- the api provides us with 3 globals
11- print (entity )
12- print (script )
13- print (world )
14-
15- -- we first retrieve ID's for our component and resource by their short name (long name/full path also work)
16- local my_component_type = world :get_type_by_name (" MyComponent" )
17-
18- -- then ask the world to give us a reference to `MyComponent` on the entity we just spawned
19- -- resources work the same way, but we use `get_resource` instead of `get_component`
20- -- the comp object is resolved to a `bevy_script_api::script_ref::ReflectValue` which implements UserData.
21- -- we can use a custom proxy instead (by implementing LuaProxyable), but this is the simplest way to get started.
22- local comp = world :get_component (entity , my_component_type )
23- print (" Before script: " , comp )
24-
25- print (" ============" )
26-
27- -- the index metamethod on ReflectValue's uses bevy's reflection mechanism on top of some custom sub-reflection logic to
28- -- allow reflecting inside Options, Vectors etc.
29- -- when we index into ReflectValue's we either get back a custom proxy or another ReflectValue
30-
31- -- the LuaBevyAPIProvider provides us custom proxies for many bevy types as well as std types.
32- -- all of these implementations can be overridden via the bevy TypeRegistry
33- comp .usize = 2
34- print (" comp.usize after assigning to 2: " , comp .usize )
35-
36- -- vec's and matrices have custom __index and __newindex overrides
37- print (" comp.vec2 before: " , comp .vec2 )
38- comp .vec2 [1 ] = 69
39- print (" comp.vec2 after: " , comp .vec2 )
40-
41- -- Option's get converted to nil or the value inside
42- print (" comp.option_vec3 before: " , comp .option_vec3 )
43- comp .option_vec3 = Vec3 .new (2 ,1 ,3 )
44- print (" comp.option_vec3 after: " , comp .option_vec3 )
45-
46- -- reflection via index is indexed starting at 1, unlike in Rust to match Lua's indexing
47- print (" comp.option_vec3[1] before: " , comp .option_vec3 [1 ])
48- comp .option_vec3 [1 ] = 5
49- print (" comp.option_vec3[1] after: " , comp .option_vec3 [1 ])
50-
51- print (" ============" )
52-
53- -- Vec<T> references get converted to a custom proxy `LuaVec<T>` which is
54- -- also assignable via lua tables
55-
56- print (" comp.vec_of_option_bools before: " , table_to_string (comp .vec_of_option_bools ))
57- comp .vec_of_option_bools = {true ,false ,true }
58- print (" comp.vec_of_option_bools after assignment: " , table_to_string (comp .vec_of_option_bools ))
59-
60- print (" comp.vec_of_option_bools[1] before: " , comp .vec_of_option_bools [1 ])
61- comp .vec_of_option_bools [1 ] = false
62- print (" comp.vec_of_option_bools[1] after: " , comp .vec_of_option_bools [1 ])
63-
64- -- there are some additional methods available on LuaVec proxies imitating the Vec<T> api
65- print (" comp.vec_of_option_bools before insert: " , table_to_string (comp .vec_of_option_bools ))
66- comp .vec_of_option_bools :insert (1 ,nil )
67- print (" comp.vec_of_option_bools after insert: " , table_to_string (comp .vec_of_option_bools ))
68-
69- print (" comp.vec_of_option_bools before push: " , table_to_string (comp .vec_of_option_bools ))
70- comp .vec_of_option_bools :push (false )
71- print (" comp.vec_of_option_bools after push: " , table_to_string (comp .vec_of_option_bools ))
72-
73- print (" comp.vec_of_option_bools len after push: " , # comp .vec_of_option_bools )
74-
75- print (" comp.vec_of_option_bools before pop: " , table_to_string (comp .vec_of_option_bools ))
76- print (comp .vec_of_option_bools :pop ())
77- print (" comp.vec_of_option_bools after pop: " , table_to_string (comp .vec_of_option_bools ))
78-
79- print (" the pairs inside comp.vec_of_option_bools: " )
80- for k ,v in pairs (comp .vec_of_option_bools ) do
81- print (string.format (" - %s:%s" ,k ,v ))
82- end
10+ function on_event ()
8311
84- comp .vec_of_option_bools :clear ()
85- print (" comp.vec_of_option_bools after clear: " , table_to_string (comp .vec_of_option_bools ))
12+ print (entity )
13+ print (script )
14+ print (world )
8615
87- print (" comp.vec_of_option_bools len after clear: " , # comp .vec_of_option_bools )
88- print (" ============" )
8916
90- print (" comp.option_vec_of_bools before: " , table_to_string (comp .option_vec_of_bools ))
91- print (comp .option_vec_of_bools :pop ())
92- print (" comp.option_vec_of_bools after pop: " , table_to_string (comp .option_vec_of_bools ))
17+ local my_component_type = world :get_type_by_name (" MyComponent" )
9318
19+ local comp = world :get_component (entity , my_component_type )
20+ print (" Before script: " , comp )
9421
95- print (" comp.option_vec_of_bools len after pop: " , # comp .option_vec_of_bools )
9622
97- print (" the pairs inside comp.option_vec_of_bools: " )
98- for k ,v in pairs (comp .option_vec_of_bools ) do
99- print (string.format (" - %s:%s" ,k ,v ))
100- end
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 )
28+ world :exit ()
29+
30+ print (comp .vec_of_usize )
31+ print (comp .vec_of_usize [2 ])
32+ comp .vec_of_usize [2 ] = 69
33+ print (comp .vec_of_usize [2 ])
34+ world :exit ()
35+
36+ print (" ============" )
37+
38+ -- the index metamethod on ReflectValue's uses bevy's reflection mechanism on top of some custom sub-reflection logic to
39+ -- allow reflecting inside Options, Vectors etc.
40+ -- when we index into ReflectValue's we either get back a custom proxy or another ReflectValue
41+
42+ -- the LuaBevyAPIProvider provides us custom proxies for many bevy types as well as std types.
43+ -- all of these implementations can be overridden via the bevy TypeRegistry
44+ print (" Hello:" , comp .usize ._1 )
45+ comp .usize [1 ] = 2
46+ print (" comp.usize after assigning to 2: " , comp .usize ._1 )
47+
48+ -- vec's and matrices have custom __index and __newindex overrides
49+ print (" comp.vec2 before: " , comp .vec2 )
50+ comp .vec2 [1 ] = 69
51+ print (" comp.vec2 after: " , comp .vec2 )
52+
53+ -- Option's get converted to nil or the value inside
54+ print (" comp.option_vec3 before: " , comp .option_vec3 )
55+ comp .option_vec3 = Vec3 .new (2 ,1 ,3 )
56+ print (" comp.option_vec3 after: " , comp .option_vec3 )
57+
58+ -- reflection via index is indexed starting at 1, unlike in Rust to match Lua's indexing
59+ print (" comp.option_vec3[1] before: " , comp .option_vec3 [1 ])
60+ comp .option_vec3 [1 ] = 5
61+ print (" comp.option_vec3[1] after: " , comp .option_vec3 [1 ])
62+
63+ print (" ============" )
64+
65+ -- Vec<T> references get converted to a custom proxy `LuaVec<T>` which is
66+ -- also assignable via lua tables
67+
68+ print (" comp.vec_of_option_bools before: " , table_to_string (comp .vec_of_option_bools ))
69+ comp .vec_of_option_bools = {true ,false ,true }
70+ print (" comp.vec_of_option_bools after assignment: " , table_to_string (comp .vec_of_option_bools ))
71+
72+ print (" comp.vec_of_option_bools[1] before: " , comp .vec_of_option_bools [1 ])
73+ comp .vec_of_option_bools [1 ] = false
74+ print (" comp.vec_of_option_bools[1] after: " , comp .vec_of_option_bools [1 ])
75+
76+ -- there are some additional methods available on LuaVec proxies imitating the Vec<T> api
77+ print (" comp.vec_of_option_bools before insert: " , table_to_string (comp .vec_of_option_bools ))
78+ comp .vec_of_option_bools :insert (1 ,nil )
79+ print (" comp.vec_of_option_bools after insert: " , table_to_string (comp .vec_of_option_bools ))
80+
81+ print (" comp.vec_of_option_bools before push: " , table_to_string (comp .vec_of_option_bools ))
82+ comp .vec_of_option_bools :push (false )
83+ print (" comp.vec_of_option_bools after push: " , table_to_string (comp .vec_of_option_bools ))
84+
85+ print (" comp.vec_of_option_bools len after push: " , # comp .vec_of_option_bools )
86+
87+ print (" comp.vec_of_option_bools before pop: " , table_to_string (comp .vec_of_option_bools ))
88+ print (comp .vec_of_option_bools :pop ())
89+ print (" comp.vec_of_option_bools after pop: " , table_to_string (comp .vec_of_option_bools ))
90+
91+ print (" the pairs inside comp.vec_of_option_bools: " )
92+ for k ,v in pairs (comp .vec_of_option_bools ) do
93+ print (string.format (" - %s:%s" ,k ,v ))
94+ end
95+
96+ comp .vec_of_option_bools :clear ()
97+ print (" comp.vec_of_option_bools after clear: " , table_to_string (comp .vec_of_option_bools ))
98+
99+ print (" comp.vec_of_option_bools len after clear: " , # comp .vec_of_option_bools )
100+ print (" ============" )
101+
102+ print (" comp.option_vec_of_bools before: " , table_to_string (comp .option_vec_of_bools ))
103+ print (comp .option_vec_of_bools :pop ())
104+ print (" comp.option_vec_of_bools after pop: " , table_to_string (comp .option_vec_of_bools ))
105+
106+
107+ print (" comp.option_vec_of_bools len after pop: " , # comp .option_vec_of_bools )
108+
109+ print (" the pairs inside comp.option_vec_of_bools: " )
110+ for k ,v in pairs (comp .option_vec_of_bools ) do
111+ print (string.format (" - %s:%s" ,k ,v ))
112+ end
101113
102- print (" ============" )
114+ print (" ============" )
103115
104- local complex_vec_op = Vec3 .new (0 ,1 ,0 ):any_orthonormal_vector () + comp .mat3 .x_axis
105- print (" (0,1,0).any_orthonormal_vector() + mat3.x_axis is: " , complex_vec_op )
116+ local complex_vec_op = Vec3 .new (0 ,1 ,0 ):any_orthonormal_vector () + comp .mat3 .x_axis
117+ print (" (0,1,0).any_orthonormal_vector() + mat3.x_axis is: " , complex_vec_op )
106118
107- local new_mat3 = Mat3 .from_cols (Vec3 .new (1 ,0 ,0 ),Vec3 .new (0 ,1 ,0 ),Vec3 .new (0 ,0 ,- 1 ))
108- print (" new_mat3 is:" , new_mat3 )
119+ local new_mat3 = Mat3 .from_cols (Vec3 .new (1 ,0 ,0 ),Vec3 .new (0 ,1 ,0 ),Vec3 .new (0 ,0 ,- 1 ))
120+ print (" new_mat3 is:" , new_mat3 )
109121
110- comp .vec2 = comp .vec2 + comp .vec2
111- comp .usize = comp .vec2 :min_element ()
112- comp .f32 = comp .f32 + comp .f32 + comp .vec2 :min_element ()
113- comp .vec2 = Vec2 .new (2 ,1 )
114- comp .quat = Quat .from_xyzw (3 ,2 ,1 ,4 )
115- comp .mat3 .x_axis = Vec3 .new (69 ,69 ,69 )
122+ comp .vec2 = comp .vec2 + comp .vec2
123+ comp .usize = comp .vec2 :min_element ()
124+ comp .f32 = comp .f32 + comp .f32 + comp .vec2 :min_element ()
125+ comp .vec2 = Vec2 .new (2 ,1 )
126+ comp .quat = Quat .from_xyzw (3 ,2 ,1 ,4 )
127+ comp .mat3 .x_axis = Vec3 .new (69 ,69 ,69 )
116128
117- print (" ============" )
129+ print (" ============" )
118130
119- -- this is an example of something impossible to achieve with plain bevy reflection under the hood
120- comp .mat3 [1 ][1 ] = 42
131+ -- this is an example of something impossible to achieve with plain bevy reflection under the hood
132+ comp .mat3 [1 ][1 ] = 42
121133
122- -- now let's retrieve these again to see if we actually changed their values permanently
123- comp = world :get_component (entity ,my_component_type )
134+ -- now let's retrieve these again to see if we actually changed their values permanently
135+ comp = world :get_component (entity ,my_component_type )
124136
125- print (" After script:" )
126- print (comp )
137+ print (" After script:" )
138+ print (comp )
127139
128- world :exit ()
140+ world :exit ()
141+ end
0 commit comments