randomPoison / gunship-rs

An experimental game engine written in Rust.
MIT License
27 stars 3 forks source link

Component Manager Improvements #52

Closed randomPoison closed 8 years ago

randomPoison commented 8 years ago

Currently the most annoying part of writing gameplay code with Gunship are the limitations surrounding borrowing component managers and component data. Currently it is possible to borrow multiple components at once because doing so only requires an immutable borrow of the manager, however adding and removing components require a mutable borrow of the manager and so all borrows on component data must end before doing either. This makes for annoying gameplay code because the programmer must manually ensure that they do not have any component data borrowed before adding or destroying a component lest they face the wrath of RefCell panicking.

Where ever possible component managers should support safely adding and removing components with only immutably borrowing the manager. This would allow for components to be added and removed at will without the programmer having to manually track lifetimes. Note that this should't be required of all component managers, as that would defeat the purpose of having component-specific managers, however it should be implemented where possible as it is more ergonomic than the alternative.

randomPoison commented 8 years ago

It looks like making these changes for TransformManager are going to be more involved, so I'm going to push those changes off to a different issue so they can be tracked separately.

randomPoison commented 8 years ago

The recent merge pulled in a bunch of the work that I've done on this issue. At this point we've implemented a messaging system and it's no longer possible to get arbitrary mutable references to components. The work isn't finished though, and there are a lot of ergonomic changes that need to be made. I think the next one is to make it so that you can default assign components so that you can assign components through the scene, e.g. scene.assign::<Mesh>(entity). That would make it possible to create entities without getting the managers which makes things easier. This only works if the component doesn't require different arguments when constructed. For example assigning the mesh currently requires the URI to be specified when the mesh is created, which means it can't be default assigned through the scene. To fix this mesh and similar components should be changed to have any custom configuration done through the component itself. So in the case of mesh you would have something like:

let mesh = scene.assign::<Mesh>();
mesh.set_mesh("cube");

In these cases the component/manager would have to be smart enough to handle the cases where the player doesn't fully initialize the type (e.g. if the player doesn't assign the actual mesh to the mesh component).

randomPoison commented 8 years ago

The current plan is to throw away the entire ECS in favor of a more low-level design for the core engine, so this issue is no longer valid.