1+ function table_to_string (t )
2+ local result = " ["
3+ for k ,v in pairs (t ) do
4+ result = result .. string.format (" %s:%s," ,k ,v )
5+ end
6+ return result .. " ]"
7+ end
8+
9+
10+ function on_event ()
11+
12+ print (entity )
13+ print (script )
14+ print (world )
15+
16+
17+ local my_component_type = world :get_type_by_name (" MyComponent" )
18+
19+ local comp = world :get_component (entity , my_component_type )
20+ print (" Before script: " , comp )
21+
22+ print (" \n option" )
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+
29+ print (" \n vec" )
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+
42+
43+ print (" ============" )
44+
45+ -- vec's and matrices have custom __index and __newindex overrides
46+ print (" comp.vec2 before: " , comp .vec2 )
47+ comp .vec2 [1 ] = 69
48+ print (" comp.vec2 after: " , comp .vec2 )
49+
50+ -- Option's get converted to nil or the value inside
51+ print (" comp.option_vec3 before: " , comp .option_vec3 )
52+ comp .option_vec3 = Vec3 .new (2 ,1 ,3 )
53+ print (" comp.option_vec3 after: " , comp .option_vec3 )
54+
55+ -- reflection via index is indexed starting at 1, unlike in Rust to match Lua's indexing
56+ print (" comp.option_vec3[1] before: " , comp .option_vec3 [1 ])
57+ comp .option_vec3 [1 ] = 5
58+ print (" comp.option_vec3[1] after: " , comp .option_vec3 [1 ])
59+
60+ print (" ============" )
61+
62+ -- Vec<T> references get converted to a custom proxy `LuaVec<T>` which is
63+ -- also assignable via lua tables
64+
65+ print (" comp.vec_of_option_bools before: " , table_to_string (comp .vec_of_option_bools ))
66+ comp .vec_of_option_bools = {true ,false ,true }
67+ print (" comp.vec_of_option_bools after assignment: " , table_to_string (comp .vec_of_option_bools ))
68+
69+ print (" comp.vec_of_option_bools[1] before: " , comp .vec_of_option_bools [1 ])
70+ comp .vec_of_option_bools [1 ] = false
71+ print (" comp.vec_of_option_bools[1] after: " , comp .vec_of_option_bools [1 ])
72+
73+ -- there are some additional methods available on LuaVec proxies imitating the Vec<T> api
74+ print (" comp.vec_of_option_bools before insert: " , table_to_string (comp .vec_of_option_bools ))
75+ comp .vec_of_option_bools :insert (1 ,nil )
76+ print (" comp.vec_of_option_bools after insert: " , table_to_string (comp .vec_of_option_bools ))
77+
78+ print (" comp.vec_of_option_bools before push: " , table_to_string (comp .vec_of_option_bools ))
79+ comp .vec_of_option_bools :push (false )
80+ print (" comp.vec_of_option_bools after push: " , table_to_string (comp .vec_of_option_bools ))
81+
82+ print (" comp.vec_of_option_bools len after push: " , # comp .vec_of_option_bools )
83+
84+ print (" comp.vec_of_option_bools before pop: " , table_to_string (comp .vec_of_option_bools ))
85+ print (comp .vec_of_option_bools :pop ())
86+ print (" comp.vec_of_option_bools after pop: " , table_to_string (comp .vec_of_option_bools ))
87+
88+ print (" the pairs inside comp.vec_of_option_bools: " )
89+ for k ,v in pairs (comp .vec_of_option_bools ) do
90+ print (string.format (" - %s:%s" ,k ,v ))
91+ end
92+
93+ comp .vec_of_option_bools :clear ()
94+ print (" comp.vec_of_option_bools after clear: " , table_to_string (comp .vec_of_option_bools ))
95+
96+ print (" comp.vec_of_option_bools len after clear: " , # comp .vec_of_option_bools )
97+ print (" ============" )
98+
99+ print (" comp.option_vec_of_bools before: " , table_to_string (comp .option_vec_of_bools ))
100+ print (comp .option_vec_of_bools :pop ())
101+ print (" comp.option_vec_of_bools after pop: " , table_to_string (comp .option_vec_of_bools ))
102+
103+
104+ print (" comp.option_vec_of_bools len after pop: " , # comp .option_vec_of_bools )
105+
106+ print (" the pairs inside comp.option_vec_of_bools: " )
107+ for k ,v in pairs (comp .option_vec_of_bools ) do
108+ print (string.format (" - %s:%s" ,k ,v ))
109+ end
110+
111+ print (" ============" )
112+
113+ local complex_vec_op = Vec3 .new (0 ,1 ,0 ):any_orthonormal_vector () + comp .mat3 .x_axis
114+ print (" (0,1,0).any_orthonormal_vector() + mat3.x_axis is: " , complex_vec_op )
115+
116+ local new_mat3 = Mat3 .from_cols (Vec3 .new (1 ,0 ,0 ),Vec3 .new (0 ,1 ,0 ),Vec3 .new (0 ,0 ,- 1 ))
117+ print (" new_mat3 is:" , new_mat3 )
118+
119+ comp .vec2 = comp .vec2 + comp .vec2
120+ comp .usize = comp .vec2 :min_element ()
121+ comp .f32 = comp .f32 + comp .f32 + comp .vec2 :min_element ()
122+ comp .vec2 = Vec2 .new (2 ,1 )
123+ comp .quat = Quat .from_xyzw (3 ,2 ,1 ,4 )
124+ comp .mat3 .x_axis = Vec3 .new (69 ,69 ,69 )
125+
126+ print (" ============" )
127+
128+ -- this is an example of something impossible to achieve with plain bevy reflection under the hood
129+ comp .mat3 [1 ][1 ] = 42
130+
131+ -- now let's retrieve these again to see if we actually changed their values permanently
132+ comp = world :get_component (entity ,my_component_type )
133+
134+ print (" After script:" )
135+ print (comp )
136+
137+ world :exit ()
138+ end
0 commit comments