Megafunk / MassSample

My understanding of Unreal Engine 5's experimental ECS plugin with a small sample project.
MIT License
681 stars 112 forks source link

Shared Fragments are only shared across entities of the same archetype #52

Open manuelott85 opened 1 year ago

manuelott85 commented 1 year ago

Hey there,

in the section about "Shared Fragments," you explain the following:

In the example above, all the entities containing the FClockSharedFragment will see the same Clock value. If an entity modifies the Clock value, the rest of the entities with this fragment will see the change, as this fragment is shared accross them.

Due to my own research, I think that is not correct. I created two different traits, and both have only the shared fragment in common. When one entity changes a property of the shared fragment, only entities of the same archetype will have access to the same information. It looks like each archetype gets its own instance of the shared fragment.

To further narrow down this fact, I extracted the shared fragment from both traits (a, b) into a third trait (c). This one has ONLY the shared fragment. And added this new third trait to each of the Data Assets. So that in the end, I have one archetype containing the traits a and c, and another one containing b and c. However, when editing the property of the shared fragment, only the entities of the same archetype get the information.

This proves to me that a shared fragment is shared per archetype, not per trait, and not as globally as one might interpret your explanation. I expected it to be similar to a property of a static class.

Which is a pain, because I still don't know how I am expected to share information on a global level.

Megafunk commented 1 year ago

Traits are prefabs for entities that define what their starting values and fragments are, not the true memory of the archetype

Megafunk commented 1 year ago

When a trait is used to create a template and spawn it the shared fragment is either retrieved from existing ones in SharedFragmentsMap or created if the hash isn't in there.. That said, I think that explanation isn't really a good example of how they work so I'm definitely rewriting it. Thanks!!

You don't need to use GetOrCreateSharedFragmentByHash but it's the main entry point for the default shared fragment setup.

Megafunk commented 1 year ago

I'm not even really sure how one is supposed to mess with shared fragments after the fact outside of foreachsharedfragment and processors. I'm going to assume you would store the FSharedStruct or hash of the one you want. That example with just a float would definitely not hash in the intended way