Transfer entity between two worlds #11101
-
TL;DR I want to be able to losslessly transfer items from the main world to another world (stored as a resource) and back again I'm implementing an inventory system and the method I've settled on involves moving an entity from the game world to another world and storing some additional metadata in a small database. With this approach I can essentially "freeze" an entity (it doesn't render, get physics updates, collide with anything, etc) while persisting its component data in a form that I can still query using bevy ecs. At the moment, I have an exclusive system which takes in a fn handle_inventory_event (world: &mut World){
world.resource_scope(|world, mut cached_state: Mut<CachedSystemState>| {
world.resource_scope(|world, inventory_data: Mut<InventoryData>| {
for event in cached_state.event_state.get(world).read() {
match event {
InventoryEvent::InsertItems(insert_data) => {
for insert_item in insert_data {
// transfer insert_item.item_id from main world to inventory_data.world
}
},
InventoryEvent::RetrieveItems(retrieve_data) => {},
InventoryEvent::DisposeItems(dispose_data) => {},
InventoryEvent::TransferItems(transfer_data) => {},
}
}
});
}); I've seen some references to MultiWorld support but I don't know how far along that is. What would be a good next step? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
To my understanding, the multi world stuff is currently special-cased for rendering and not yet available for general use. I also found an RFC on subworlds and a discussion on multi-world support. So I think this is not easily possible yet. |
Beta Was this translation helpful? Give feedback.
-
This is definitively possible, but not the recommended way of approaching the problem you are describing. I would typically add an If you want to "move" entities between |
Beta Was this translation helpful? Give feedback.
This is definitively possible, but not the recommended way of approaching the problem you are describing. I would typically add an
Inventory
component, and remove physics and rendering components to "convert" a physical entity into an inventory item. I've done this many times. I think you would benefit from not looking at differentWorld
s as different "container" where to put entities. But rather entirely ignore the concept ofWorld
(unless you care about serialization and scene loading, or other esoteric stuff). It's an implementation details, and shouldn't enter in game logic. Use components to "tag" entities with various behaviors.If you want to "move" entities between
World
s, it is p…