Scripting Autofill different when in ECS code

In Visual Studio when I use autofill for a loop, the code generated is different when in ECS code.

//non ECS code 
 for (int i = length - 1; i >= 0; i--)
 {
     
 }
//ECS code
 for (global::System.Int32 i = (length) - (1); i >= 0; i--)
 {
     
 }

How do I prevent this different autofill in Visual Studio?

Edit: there’s a tracked issue for this:

It’s some sort of bug in Visual Studio. It seems to trigger when in a nested loop context. For example:

In entities code:

foreach(LocalTransform localTransform in SystemAPI.Query<LocalTransform>())
{
    for (global::System.Int32 i = 0; i < length; i++)
    {
                    
    }
}

In a normal console app (also happens in Unity project):

for (int i = 0; i < length; i++)
{
    for (global::System.Int32 j = 0; j < length; j++)
    {
        
    }
}

foreach (var item in args)
{
    for (global::System.Int32 i = 0; i < length; i++)
    {
        
    }
}

while (true)
{
    for (global::System.Int32 i = 0; i < length; i++)
    {
        
    }
}

There could be some other root cause, and perhaps this isn’t just something that occurs with nested loops. If you see this happen outside an outer loop, please share that here.

I don’t know how to fix this at the moment. There’s no functional difference between both, but obviously readability suffers. You might want to raise this issue with Microsoft through Help > Send Feedback > Report a Problem...

For reference, I’ve checked this with 17.10.4 and 17.11.0.

1 Like