Skip to content

Commit 4ac541e

Browse files
zicklagjakobhellermann
authored andcommitted
Add Value.patch()
1 parent 01de445 commit 4ac541e

File tree

5 files changed

+42
-0
lines changed

5 files changed

+42
-0
lines changed

assets/scripts/breakout.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,19 @@ function run() {
3434
world.insert(item.entity, Value.create(Ball));
3535

3636
// 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().
3740
let vel = Value.create(Velocity, [
3841
{
3942
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+
{
4050
y: 200,
4151
},
4252
]);

src/runtime/ops/ecs.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ pub fn insert_ecs_ops(ops: &mut OpMap) {
4040
"ecs_value_ref_default",
4141
Box::new(value::ecs_value_ref_default),
4242
);
43+
ops.insert("ecs_value_ref_patch", Box::new(value::ecs_value_ref_patch));
4344
ops.insert(
4445
"ecs_component_insert",
4546
Box::new(component::ecs_component_insert),

src/runtime/ops/ecs/ecs.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,10 @@
187187
create(type, patch) {
188188
return Value.wrapValueRef(bevyModJsScriptingOpSync("ecs_value_ref_default", type.typeName, patch));
189189
},
190+
191+
patch(value, patch) {
192+
Value.wrapValueRef(bevyModJsScriptingOpSync("ecs_value_ref_patch", Value.unwrapValueRef(value), patch));
193+
}
190194
}
191195

192196
const world = new World();

src/runtime/ops/ecs/value.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,32 @@ pub fn ecs_value_ref_default(
364364
Ok(serde_json::to_value(value_ref)?)
365365
}
366366

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+
367393
pub fn ecs_value_ref_call(
368394
context: OpContext,
369395
world: &mut bevy::prelude::World,

types/lib.bevy.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ declare type RawValueRef = unknown;
2424

2525
declare interface ValueGlobal {
2626
create<T>(t: BevyType<T>, patch?: any): T;
27+
patch<T>(value: any, patch: any): T;
2728
}
2829

2930
declare let Value: ValueGlobal;

0 commit comments

Comments
 (0)