@@ -169,6 +169,10 @@ impl ScriptWorld {
169
169
) -> Result < ScriptRef , ScriptError > {
170
170
let mut w = self . write ( ) ;
171
171
172
+ let mut entity_ref = w
173
+ . get_entity_mut ( entity)
174
+ . ok_or_else ( || ScriptError :: Other ( format ! ( "Entity is not valid {:#?}" , entity) ) ) ?;
175
+
172
176
let component_data = comp_type. data :: < ReflectComponent > ( ) . ok_or_else ( || {
173
177
ScriptError :: Other ( format ! ( "Not a component {}" , comp_type. short_name( ) ) )
174
178
} ) ?;
@@ -177,18 +181,18 @@ impl ScriptWorld {
177
181
// TODO: maybe get an add_default impl added to ReflectComponent
178
182
// this means that we don't require ReflectDefault for adding components!
179
183
match comp_type. 0 . type_info ( ) {
180
- bevy:: reflect:: TypeInfo :: Struct ( _) => component_data. insert ( & mut w . entity_mut ( entity ) , & DynamicStruct :: default ( ) ) ,
181
- bevy:: reflect:: TypeInfo :: TupleStruct ( _) => component_data. insert ( & mut w . entity_mut ( entity ) , & DynamicTupleStruct :: default ( ) ) ,
182
- bevy:: reflect:: TypeInfo :: Tuple ( _) => component_data. insert ( & mut w . entity_mut ( entity ) , & DynamicTuple :: default ( ) ) ,
183
- bevy:: reflect:: TypeInfo :: List ( _) => component_data. insert ( & mut w . entity_mut ( entity ) , & DynamicList :: default ( ) ) ,
184
- bevy:: reflect:: TypeInfo :: Array ( _) => component_data. insert ( & mut w . entity_mut ( entity ) , & DynamicArray :: new ( Box :: new ( [ ] ) ) ) ,
185
- bevy:: reflect:: TypeInfo :: Map ( _) => component_data. insert ( & mut w . entity_mut ( entity ) , & DynamicMap :: default ( ) ) ,
186
- bevy:: reflect:: TypeInfo :: Value ( _) => component_data. insert ( & mut w . entity_mut ( entity ) ,
184
+ bevy:: reflect:: TypeInfo :: Struct ( _) => component_data. insert ( & mut entity_ref , & DynamicStruct :: default ( ) ) ,
185
+ bevy:: reflect:: TypeInfo :: TupleStruct ( _) => component_data. insert ( & mut entity_ref , & DynamicTupleStruct :: default ( ) ) ,
186
+ bevy:: reflect:: TypeInfo :: Tuple ( _) => component_data. insert ( & mut entity_ref , & DynamicTuple :: default ( ) ) ,
187
+ bevy:: reflect:: TypeInfo :: List ( _) => component_data. insert ( & mut entity_ref , & DynamicList :: default ( ) ) ,
188
+ bevy:: reflect:: TypeInfo :: Array ( _) => component_data. insert ( & mut entity_ref , & DynamicArray :: new ( Box :: new ( [ ] ) ) ) ,
189
+ bevy:: reflect:: TypeInfo :: Map ( _) => component_data. insert ( & mut entity_ref , & DynamicMap :: default ( ) ) ,
190
+ bevy:: reflect:: TypeInfo :: Value ( _) => component_data. insert ( & mut entity_ref ,
187
191
comp_type. data :: < ReflectDefault > ( ) . ok_or_else ( ||
188
192
ScriptError :: Other ( format ! ( "Component {} is a value or dynamic type with no `ReflectDefault` type_data, cannot instantiate sensible value" , comp_type. short_name( ) ) ) ) ?
189
193
. default ( )
190
194
. as_ref ( ) ) ,
191
- bevy:: reflect:: TypeInfo :: Enum ( _) => component_data. insert ( & mut w . entity_mut ( entity ) , & DynamicEnum :: default ( ) )
195
+ bevy:: reflect:: TypeInfo :: Enum ( _) => component_data. insert ( & mut entity_ref , & DynamicEnum :: default ( ) )
192
196
} ;
193
197
194
198
Ok ( ScriptRef :: new_component_ref (
@@ -205,11 +209,15 @@ impl ScriptWorld {
205
209
) -> Result < Option < ScriptRef > , ScriptError > {
206
210
let w = self . read ( ) ;
207
211
212
+ let entity_ref = w
213
+ . get_entity ( entity)
214
+ . ok_or_else ( || ScriptError :: Other ( format ! ( "Entity is not valid {:#?}" , entity) ) ) ?;
215
+
208
216
let component_data = comp_type. data :: < ReflectComponent > ( ) . ok_or_else ( || {
209
217
ScriptError :: Other ( format ! ( "Not a component {}" , comp_type. short_name( ) ) )
210
218
} ) ?;
211
219
212
- Ok ( component_data. reflect ( w . entity ( entity ) ) . map ( |_component| {
220
+ Ok ( component_data. reflect ( entity_ref ) . map ( |_component| {
213
221
ScriptRef :: new_component_ref ( component_data. clone ( ) , entity, self . clone ( ) . into ( ) )
214
222
} ) )
215
223
}
@@ -224,7 +232,11 @@ impl ScriptWorld {
224
232
ScriptError :: Other ( format ! ( "Not a component {}" , comp_type. short_name( ) ) )
225
233
} ) ?;
226
234
227
- Ok ( component_data. reflect ( w. entity ( entity) ) . is_some ( ) )
235
+ let entity_ref = w
236
+ . get_entity ( entity)
237
+ . ok_or_else ( || ScriptError :: Other ( format ! ( "Entity is not valid {:#?}" , entity) ) ) ?;
238
+
239
+ Ok ( component_data. reflect ( entity_ref) . is_some ( ) )
228
240
}
229
241
230
242
pub fn remove_component (
@@ -233,10 +245,15 @@ impl ScriptWorld {
233
245
comp_type : ScriptTypeRegistration ,
234
246
) -> Result < ( ) , ScriptError > {
235
247
let mut w = self . write ( ) ;
248
+
249
+ let mut entity_ref = w
250
+ . get_entity_mut ( entity)
251
+ . ok_or_else ( || ScriptError :: Other ( format ! ( "Entity is not valid {:#?}" , entity) ) ) ?;
252
+
236
253
let component_data = comp_type. data :: < ReflectComponent > ( ) . ok_or_else ( || {
237
254
ScriptError :: Other ( format ! ( "Not a component {}" , comp_type. short_name( ) ) )
238
255
} ) ?;
239
- component_data. remove ( & mut w . entity_mut ( entity ) ) ;
256
+ component_data. remove ( & mut entity_ref ) ;
240
257
Ok ( ( ) )
241
258
}
242
259
0 commit comments