Prefabs with special cases at runtime

(Azriel Hoh) #1

People who have used prefabs, how would you do this:

  • There are generic CharacterPrefabs, which impl PrefabData.
  • Most character entities should just have Prefab<CharacterPrefab> with just the main entity
  • Some characters should have an additional HpBarPrefab (also impl PrefabData), whose Parent is the character entity

Since Prefab<_>s are Assets, and we attach Handle<Prefab<_>> to entities, instead of the Prefab<_> directly, it’s ergonomic to be able to load them early.
However, this means specifying all the entity objects up front – you have to specify the parent links when you define Prefab<_>.

As far as I can tell, this means one of:

  • Prefab<CharacterPrefab> with HP bars cannot be loaded early on – we instantiate and load one on the fly when we know which “some characters” are. Or,
  • We don’t use HpBarPrefab inside a Prefab, and instead manually run add_to_entity on the “some characters”. Or,
  • Don’t use a prefab for HpBar.

Is there another better way?

0 Likes

(Sergey Veselkov) #2

If I understand the problem correctly, then you can declare a structure like this
pub struct HpBarSet(pub Vec<HpBarHandle>)
and push Handle to the Write<'a, HpBarSet> when calling the HpBarPrefab::load_sub_assets.

Now add a id field to the CharacterPrefabs and get HpBar from Write<'a, HpBarSet> by id when calling the CharacterPrefabs::load_sub_assets.

0 Likes

(Azriel Hoh) #3

ah :bulb:! That’s clever (add_to_entity fits a bit better in this case) – write to a resource, which the CharacterPrefab: PrefabData#add_to_entity implementation will read, and create entities for HpBar

Thanks :bowing_man:!

0 Likes

(Sergey Veselkov) #4

All thanks to @Rhuagh . I learned this tip by working on his pull request.

0 Likes