-
Notifications
You must be signed in to change notification settings - Fork 72
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add entity and component lifecycle events #98
Add entity and component lifecycle events #98
Conversation
Gonna look at this later! :) |
Havent had time yet, last few days were hell. But tomorrow :) |
Sorry for the late reply, im extremely happy when that damn thesis does not consume 99% of my time anymore.
That can be done later ^^ Probably its even "clean" to separate them or probably its my tired brain forcing me into that thinking.
I think it makes the most sense to fire a add event only or? Well actually a create operation is a add and a set operation, but for the user it only notable as one single add operation i guess.
I think there's a way around this. Its not that well documented since its dark magic (actually i just have no clue how i should explain it).
Thats a potential improvement, but is that really necessary? ^^ Theres only one Events class, correct?
Thats actually very useful. I could also imagine that this approach is "better" with e.g. queryable events and stuff.
It should (to enforce a common codestyle here) i guess ^^ Im gonna look at that add/set problematic to remove the need of array renting :) |
Keeping them separate does mean that no array space is wasted accounting for components that don't have events subscribed to them.
It now raises both events with this commit, since it doesn't check if the component passed is of default value before raising the set event. Should it? That would be the only way I can think of fixing it.
There's one events class and its generic counterpart that inherits it, with an array that holds those event classes, which are casted using Unsafe.As
Checked this and it isn't possible to do while targeting netstandard2, since DangerousGetReference can't be called on a list and no span can be obtained from it without a call to CollectionsMarshal, which was introduced in .NET 5. A preprocessor directive could be used for #if NET5_0_OR_GREATER, if that's considered worth it.
World has been removed from the archetype in this commit and the event-raising methods are public now with this commit |
So i think this should work as a replacement for the renting arrays: // Update the entityInfo of all copied entities.
for (var chunkIndex = archetypeSlot.ChunkIndex; chunkIndex >= newArchetypeSlot.ChunkIndex; --chunkIndex)
{
// Get data
ref var chunk = ref archetype.GetChunk(chunkIndex);
ref var entityFirstElement = ref chunk.Entity(0);
// Only move within the range, depening on which chunk we are at.
var isStart = chunkIndex == archetypeSlot.ChunkIndex;
var isEnd = chunkIndex == newArchetypeSlot.ChunkIndex;
var upper = isStart ? archetypeSlot.Index : chunk.Size-1;
var lower = isEnd ? newArchetypeSlot.Index : 0;
for (var index = upper; index >= lower; --index)
{
ref readonly var entity = ref Unsafe.Add(ref entityFirstElement, index);
// Fire event for entity
}
} This is done exactly like this during the shift operation and since no EntityInfo is accessed, it will also work after the shift since all the data still exists. I'll try to incorporate this next :) probably even with a small enumerator to simplify code since no one likes to write this shit everytime.
I think that's fine :) Default value is default value and its still being set, so it makes sense to still fire it.
Then we will just leave it like this for the first. Putting more directives into this would just reduce the readability. |
See #25
This doesn't (currently) support custom user-defined events.
Adds a way to handle entity created/destroyed events and component add/set/remove events.
It only uses arrays for data structures unless a component of type object is passed, at which point it needs to use a dictionary to find its id.
Few things:
_world.Create<EventTestComponentOne>();
fires a set event but not an add event, should that be changed?event Action
field so that this can more easily support ordering later if necessary.