-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Ability to Create and Insert Components #26
Add Ability to Create and Insert Components #26
Conversation
Bevy 0.9 had a PR merged which significantly reduced the serialized output: |
I have also been thinking about how creating values should work. world.insertResource(Vec3, { x: 0, y: 0, z: 0 }); work (assuming There is nothing preventing us from doing so, because we know the type when inserting we should be able to construct the actual value from the There are however, some operations where we need to know the type before handing the value to the world, like calling methods. This is where something like your let value = as(Vec3, { x: 0, y: 1, z: 2 });
value.normalize(); This should work recursively, e.g. for creating a Alternatively a good stopgap solution is just being able to construct a default value, which you can use like let translation = default(Vec3); // or Vec3.default()?
translation.x = 5;
let transform = default(Transform);
transform.translation = translation; Typing this, I realized that we can just make |
That said I think this PR is a good start and I will merge it to have a good base and improve on that later. |
To me I think I'd rather have to create values before inserting them as resources. I like being explicit about the creation step instead of adding an overload to the Supporting
Yeah, that should be possible now with this PR, and doesn't feel too bad. I think using |
And I pushed an update fixing your review points. 👍 |
Just pushed an update that renames let vel = Value.create(Velocity, [
{
x: -200,
y: 200,
},
]); And you can still just leave out the second argument to create a default value: let ball = Value.create(Ball); I think that works pretty well. |
Pushed another update that adds Value.patch(vel, [
{
x: 0,
y: 200,
},
]); BTW, I liked the idea of a free function called |
Looks good, thank you. I haven't decided yet how exactly I would ideally want to have the API for dealing with values look like, but when I find something I'll just open a PR or an issue for discussion. |
Now we can create an insert components!
I kind of hacked the example real quick to demonstrate. Not sure if we want to make that nicer or we just leave it for now.
Right now the only way to create components is with
Value.default(MyComponent)
, and then modifying the returned value ref.I tried to make a version called
Value.create(MyComponent, { my: 'components', example: [1, 2, 3] })
, but it turns out that Bevy's ReflectDeserializer seems quite verbose as far as I can tell:It seems that the Bevy scene loader somehow gets away without as much verbosity, but it's also RON, so I'm not sure if it's able to trim out unnecessary annotation of types.
Either way, I figure using a default implementation to create the component, and then patching the component manually ( especially if we implement a
value.patch()
method later ), then we're already in pretty good shape and it isn't worth the effort yet.This opens up a lot more awesome use-cases for scripting!