sebas77/Svelto.ECS

EGID with explicit groupId works incorrectly in case if negative entityId was used

Closed this issue · 2 comments

uds commented

As was shown in your example, I'm using GO.GetInstanceID() to obtain IDs for my entities.
I find it's logical and convenient.

Unity often will return a negative GO ID's, which will result in a problem in cases when the non-default groupId was used to create a new EGID instance.

The problem is in the following code:

        static long MAKE_GLOBAL_ID(int entityId, int groupId)
        {
            return entityId | (long)groupId << 32;
        }

Here , if entityId is negative, it's sign bit will be extended during explicit cast of entityId to long, effectively masking group ID completely. As a result, trying to get back group ID from such EGID instance will always return -1.

Note that C# compiler issues warning about sign bit extension on that line as well:
warning CS0675: Bitwise-or operator used on a sign-extended operand; consider casting to a smaller unsigned type first

Here is a fix which works for me:

        static long MAKE_GLOBAL_ID(int entityId, int groupId)
        {
            return entityId & 0xFFFFFFFF | (long)groupId << 32;
        }

hey thanks a lot for this! I will keep it in mind for the next release

Hey I just checked the code and in my code the entityID was casted to uint, I added the masking for safety though (shouldn't be necessary really)