File tree 5 files changed +42
-0
lines changed
5 files changed +42
-0
lines changed Original file line number Diff line number Diff line change @@ -34,9 +34,19 @@ function run() {
34
34
world . insert ( item . entity , Value . create ( Ball ) ) ;
35
35
36
36
// Create a velocity component
37
+ //
38
+ // We can optionally include a patch to the default value of velocity as the second argument
39
+ // to create().
37
40
let vel = Value . create ( Velocity , [
38
41
{
39
42
x : - 200 ,
43
+ } ,
44
+ ] ) ;
45
+
46
+ // For demonstration purposes, we can also patch values after they have been created. This
47
+ // works on any ECS component, not just ones created with Value.create().
48
+ Value . patch ( vel , [
49
+ {
40
50
y : 200 ,
41
51
} ,
42
52
] ) ;
Original file line number Diff line number Diff line change @@ -40,6 +40,7 @@ pub fn insert_ecs_ops(ops: &mut OpMap) {
40
40
"ecs_value_ref_default" ,
41
41
Box :: new ( value:: ecs_value_ref_default) ,
42
42
) ;
43
+ ops. insert ( "ecs_value_ref_patch" , Box :: new ( value:: ecs_value_ref_patch) ) ;
43
44
ops. insert (
44
45
"ecs_component_insert" ,
45
46
Box :: new ( component:: ecs_component_insert) ,
Original file line number Diff line number Diff line change 187
187
create ( type , patch ) {
188
188
return Value . wrapValueRef ( bevyModJsScriptingOpSync ( "ecs_value_ref_default" , type . typeName , patch ) ) ;
189
189
} ,
190
+
191
+ patch ( value , patch ) {
192
+ Value . wrapValueRef ( bevyModJsScriptingOpSync ( "ecs_value_ref_patch" , Value . unwrapValueRef ( value ) , patch ) ) ;
193
+ }
190
194
}
191
195
192
196
const world = new World ( ) ;
Original file line number Diff line number Diff line change @@ -364,6 +364,32 @@ pub fn ecs_value_ref_default(
364
364
Ok ( serde_json:: to_value ( value_ref) ?)
365
365
}
366
366
367
+ pub fn ecs_value_ref_patch (
368
+ context : OpContext ,
369
+ world : & mut bevy:: prelude:: World ,
370
+ args : serde_json:: Value ,
371
+ ) -> anyhow:: Result < serde_json:: Value > {
372
+ // Parse args
373
+ let ( value_ref, patch) : ( JsValueRef , serde_json:: Value ) =
374
+ serde_json:: from_value ( args) . context ( "parse args" ) ?;
375
+
376
+ let value_refs = context
377
+ . op_state
378
+ . entry :: < JsValueRefs > ( )
379
+ . or_insert_with ( default) ;
380
+
381
+ let value_ref = value_refs
382
+ . get_mut ( value_ref. key )
383
+ . ok_or_else ( || format_err ! ( "Value ref does not exist" ) ) ?;
384
+
385
+ let mut value = value_ref. get_mut ( world) ?;
386
+
387
+ // Patch the default value if a patch is provided
388
+ patch_reflect_with_json ( value. as_reflect_mut ( ) , patch) ?;
389
+
390
+ Ok ( serde_json:: Value :: Null )
391
+ }
392
+
367
393
pub fn ecs_value_ref_call (
368
394
context : OpContext ,
369
395
world : & mut bevy:: prelude:: World ,
Original file line number Diff line number Diff line change @@ -24,6 +24,7 @@ declare type RawValueRef = unknown;
24
24
25
25
declare interface ValueGlobal {
26
26
create < T > ( t : BevyType < T > , patch ?: any ) : T ;
27
+ patch < T > ( value : any , patch : any ) : T ;
27
28
}
28
29
29
30
declare let Value : ValueGlobal ;
You can’t perform that action at this time.
0 commit comments