AmbientRun/Ambient

Add function to load a `Prefab` programmatically

philpax opened this issue · 6 comments

let world = World::load_from_url("assets/Cube.glb/objects/main.json").await?;
// world.objects contains a dict of entityids to components
assert_eq!(world.objects.iter().next(), Some((EntityId(...), Components::new()...)));
/// can spawn individual objects, or spawn the entire world
let id = world.objects.get(&EntityId(...)).unwrap().spawn();
let id = world.spawn();

entity::spawn_template has been removed

Just realised this needs infrastructure on the guest side for the host to notify it of async events (unless we reuse event::ENTITY_SPAWN) for this. Maybe define another event like event::WORLD_SPAWN?

(What does object_from_url do for multiple entities in an object, anyway?)

@philpax So there are two different things here; loading and spawning worlds has more to do with maps and other stuff. I think that probably has it's own flow, maybe something like let map = World::load("url").await; map.spawn(); or something.

Then on the other hand we have object_from_url; this is for hot-reloading assets etc. In that case, the "base entity" is already created by the user, so to be notified when the rest of the entity is loaded we'd have to wait for some object_loaded component to be added to that entity (doesn't exist yet).

object_from_url doesn't handle multiple-entity objects right now, but it should handle it the same way as model_from_url does in the future; if it's one object it should attach the components to the entity; if it's multiple they should be created as children to it.

Makes sense; I was using World to mean "a collection of entities that could be spawned in" (e.g. what happens when you currently request a multiple-entity object to be spawned). I think there's a good chance people will want to spawn in multiple-entity objects and directly manipulate them outside of map-loading, so I'm thinking we offer all three:

  1. Spawning in an entire World : for map loading
  2. Loading a World from somewhere, tweaking its constituent Components, and then spawning it
  3. Just using object_from_url and having it do all the work for you

Perhaps we should call it something other than World on the guest side?

Prefab is the term we use for a world being spawned in and updating/creating an entity or multiple entities. In line with that, we'd call this Prefab::load_from_url("url") and offer both the ability to spawn it immediately and the ability to poke around in its hierarchy before spawning it.

A future extension of this could also be used to save entities for later loading. I'm leaving that as a future development as it'll have to tie in to our story around persistence and general I/O access.