Doraku / DefaultEcs

Entity Component System framework aiming for syntax and usage simplicity with maximum performance for game development.
MIT No Attribution
658 stars 62 forks source link

WorldIds are assigned starting from 1 not 0 #111

Closed raineszm closed 3 years ago

raineszm commented 3 years ago

The static constructor for World is

  static World()
        {
            _lockObject = new object();
            _worldIdDispenser = new IntDispenser(0);

            Worlds = new World[2];

            IsAliveFlag = ComponentFlag.GetNextFlag();
            IsEnabledFlag = ComponentFlag.GetNextFlag();
        }

This leads to the lowest world Id being 1 and the first element of the Worlds array being empty.

Doraku commented 3 years ago

Hello, this is actually by design :) Since Entity is a struct, its default value would have the internal WorldId 0 so this means you could access a real entity data with the default value if 0 were a valid WorldId, which would be weird. Also doing so leave the id 0 in the Publisher open to be used for static subscription that is used internally, ex: https://github.com/Doraku/DefaultEcs/blob/a86000d2ab0588e0da81a0ea38d8aa5b949b5dc0/source/DefaultEcs/Technical/ComponentManager.cs#L33

I could just do a WorldId - 1 when accessing the arrays but I think it would incur a small performance loss, so all in all I think the tradeoff of losing (n + 1) * IntPtr amount of memory (n: number of components) for all this is acceptable.

raineszm commented 3 years ago

Ah. That's clever. I see. Never mind. Thanks for a great library!

Doraku commented 3 years ago

Thanks, don't hesitate if you have questions.