Jump to content

  • Log In with Google      Sign In   
  • Create Account


Oh Unity, what a strange decision you have made.

  • You cannot reply to this topic
12 replies to this topic

#1 ferrous   Members   -  Reputation: 1544

Like
2Likes
Like

Posted 17 March 2014 - 04:51 PM

So, in Unity, this is perfectly legal code, no warnings or compiler errors will show up.

 

(Though this is pseudo as I don't have the code in front of me)


void SomeFunction(Vector3 value0, Vector3 value1, float deltaTime)
{
   
    Vector2 interpValue = Vector3.Lerp(value0, value1, deltaTime);
}

You see, Unity, in it's infinite wisdom, has made it's vector classes auto-convert from Vector2s to Vector3s and back again.  It just sticks a zero in or drops the last value.  Who cares about catching compile time error checking, right?

 

Do people like that feature?  It drives me batty, a simple mistype can become a mysterious bug, that would normally be caught at compile time.



Sponsor:

#2 Ravyne   Crossbones+   -  Reputation: 6765

Like
1Likes
Like

Posted 17 March 2014 - 05:02 PM

Its probably a bit of a misstep to allow implicit conversion like that, but I don't know that its terribly egregious either. Particularly in the other direction its common and convenient to convert implicitly, and its possible they viewed consistency of requiring the explicit conversion as more important than preventing this particular pothole -- although I would personally disagree with that since round-tripping down and back again effectively zero's the trailing dimensions. I would probably be tempted to make upwards conversions implicit, but downwards conversions explicit.



#3 ferrous   Members   -  Reputation: 1544

Like
0Likes
Like

Posted 17 March 2014 - 07:07 PM

It's certainly not the worst Coding Horror I've ever seen, but it's definitely the first Vector math class/structs that I've seen that allow implicit conversion both ways.  It just doesn't seem like much of an advantage is gained over simple type safety.  



#4 TheChubu   Crossbones+   -  Reputation: 3704

Like
2Likes
Like

Posted 17 March 2014 - 07:18 PM

Ehh, I'm on a fence on this.

 

On one side its very convenient, but explicit conversions are kinda annoying to write.

 

Now, if you could do it in a GLSL-like way, ie:

 

vec2 interpValue = vec2(lerp(val0,val1,delta).xy);

 

That would be both convenient and explicit enough for me.


"I AM ZE EMPRAH OPENGL 3.3 THE CORE, I DEMAND FROM THEE ZE SHADERZ AND MATRIXEZ"

 

My journals: dustArtemis ECS framework and Making a Terrain Generator


#5 Nypyren   Crossbones+   -  Reputation: 3688

Like
0Likes
Like

Posted 17 March 2014 - 07:37 PM

I only use implicit conversion operators if I'm the only person who will EVER use the type. Otherwise it's explicit operators or ToX() methods.

Unity could have easily used 'explicit' instead of 'implicit' when defining those operators.

Edited by Nypyren, 17 March 2014 - 07:37 PM.


#6 Ravyne   Crossbones+   -  Reputation: 6765

Like
0Likes
Like

Posted 17 March 2014 - 08:47 PM

It kinda depends on legacy of the code -- explicit conversion constructors have been around since C++98, but explicit conversion operators are new in C++11. Depending on how they chose to implement them, and their other considerations in doing so, explicit conversions might not have been an option at the time (outside of named conversion functions, like ToX member functions).

 

Its also one of those things where changing it after the fact would yield a more-restrictive API surface than before. You can make things less-restrictive over time without breaking source compatibility, but you can't be more-restrictive without breaking source all over the place. Yes, it's better to be explicit, but try telling that to the 100s of millions of lines of unity code that exists, and those who have to maintain it.



#7 Hodgman   Moderators   -  Reputation: 27638

Like
1Likes
Like

Posted 17 March 2014 - 08:51 PM


It kinda depends on legacy of the code -- explicit conversion constructors have been around since C++98, but explicit conversion operators are new in C++11

Unity uses an old version of C#/Mono wink.png

 

At the least, the code should probably look something like:

Vector2 interpValue = Vector3.Lerp(value0, value1, deltaTime).ToVec2();

or

Vector2 interpValue = Vector3.Lerp(value0.ToVec2(), value1.ToVec2(), deltaTime);

or

Vector2 interpValue = Vector2.Lerp(value0, value1, deltaTime); //overload that takes vec3 arguments


Edited by Hodgman, 17 March 2014 - 08:52 PM.


#8 frob   Moderators   -  Reputation: 18857

Like
0Likes
Like

Posted 17 March 2014 - 11:47 PM

IIRC it was a change they introduced mid 2013, along with other preparatory changes for the eventual Unity 2D environment.

I'm not seeing it specifically in the change logs, but there is a definite cutoff point where in the unity3d forums people stopped complaining about the difficulty of converting the Vector2 mouse coordinates into vector3 objects.
Check out my personal indie blog at bryanwagstaff.com.

#9 Barbossa   Members   -  Reputation: 769

Like
0Likes
Like

Posted 18 March 2014 - 08:51 AM

As nice as it may be it introduces bugs that are horrible to track down as well, for example dropping a script containing this on a 2D-only object (e.g. a sprite) does not produce an error but crashes the editor instantly:

void Update() {    rigidbody.AddForce(Vector3.up * 10);}
Took me while to figure out that the reason for the editor to crash as soon as I hit play was that I wrote "rigidbody" instead of "rigidbody2d"...

Edit: Not even a simple warning is emitted.

#10 ferrous   Members   -  Reputation: 1544

Like
0Likes
Like

Posted 18 March 2014 - 01:55 PM

As nice as it may be it introduces bugs that are horrible to track down as well, for example dropping a script containing this on a 2D-only object (e.g. a sprite) does not produce an error but crashes the editor instantly:
 

void Update() {    rigidbody.AddForce(Vector3.up * 10);}
Took me while to figure out that the reason for the editor to crash as soon as I hit play was that I wrote "rigidbody" instead of "rigidbody2d"...

 

 

And even then it cringe, as it should be:

void Update() {    rigidbody2D.AddForce(Vector2.up * 10);}

Though you can get away with it anyway, since the up vector just happens to be 0,1,0, it still irks.



#11 ferrous   Members   -  Reputation: 1544

Like
0Likes
Like

Posted 18 March 2014 - 01:57 PM

IIRC it was a change they introduced mid 2013, along with other preparatory changes for the eventual Unity 2D environment.

I'm not seeing it specifically in the change logs, but there is a definite cutoff point where in the unity3d forums people stopped complaining about the difficulty of converting the Vector2 mouse coordinates into vector3 objects.

 

Waaa?  Is it really that hard to do mouse3 = new Vector3(mouse.x,0.0, mouse.y) or however you want to divvy up the values?

 

 Also, I am in complete agreement with Hodgman, I really wouldn't have minded if they had a .ToVector2() or .ToVector3() function, but doing it behind my back is what drives me crazy.



#12 MJP   Moderators   -  Reputation: 10231

Like
3Likes
Like

Posted 18 March 2014 - 02:06 PM

This happens all of the time in shader code, and I firmly believe it to be Evil. It has been the source of many bugs that were difficult to track down. Fortunately the HLSL compiler will emit a warning when it truncates vector types, so if you enable treating warnings as errors then you can catch it.



#13 ferrous   Members   -  Reputation: 1544

Like
1Likes
Like

Posted 18 March 2014 - 03:30 PM

This happens all of the time in shader code, and I firmly believe it to be Evil. It has been the source of many bugs that were difficult to track down. Fortunately the HLSL compiler will emit a warning when it truncates vector types, so if you enable treating warnings as errors then you can catch it.

 

Yeah, I'm a firm believer in catching as many errors as possible at compile time, which is why I like languages that are strongly typed. 







PARTNERS