I’m currently learning C++ and as an exercise I tried to duplicate the Vector3 structure. However, when changing the x, y, or z variables, I was unable to have other variables such as magnitude and normalized change. This is because, to my knowledge, C++ does not support properties.
I thought Unity3D had its API classes and structures built using C++, so how do they support changing the magnitude and normalized variables when a component variable changes?
I know that understanding how the API works isn’t vital for using Unity3D, but I’m curious and since this directly relates to the Unity API, I thought I would ask it here.
> I thought Unity3D had its API classes and structures built using C++ A lot of it is built on Mono. This question would be better posted on the forums though.
Just to clarify a little bit. The Vector3 struct only contains 3 data members: x, y, z and nothing else. The Vector3 struct itself is a Mono type, however if native code function get passed a Vector3, C++ will receive a pointer to 3 floats. The memory foodprint of the struct looks exactly the same in C++.
If you want to dig a bit deeper to understand how Unity’s scripting environment works, just download ILSpy which is an open source C# / CIL reflector. Open UnityEngine.dll and / or UnityEditor.dll to see how the classes are implemented. A lot functions / properties map directly to native code so you can’t go deeper than that, but like Eric mentioned a lot stuff is done directly in managed code.
Only a few methods of Vector3 are native code functions (Slerp, OrthoNormalize, RotateTowards). Everything else is pure managed code.
On the other hand most stuff in the Transform-class is native code, even the .position property:
public Vector3 position
{
get
{
Vector3 result;
this.INTERNAL_get_position(out result);
return result;
}
set
{
this.INTERNAL_set_position(ref value);
}
}
This is the magnitude property:
public float magnitude
{
get
{
return Mathf.Sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
}
}
Values such as magnitude ultimately depend on the individual components of the vector. Someone building a vector library has two main choices:
Each time someone asks for the magnitude, re-calculate it based on the current components
Each time someone changes a component, cache the current magnitude
The first case would be mostly driven by accessor method which calculate values on “get”.
The second case would be mostly driven by mutator methods which calculate values on “set”.
If you add caching or other “lazy” operations, it can get quite complicated.
A lot of people overlook one of the most powerful elements in C# properties: they are implicitly functions. Supposing we wanted to implement the second method I described above, we could do something like this: private float _x = 0f; public float x { get { return _x; } set { _x = value; Recalc(); //rebuild other values } } To someone using your interface, x looks just like a float which you can get/set normally, but setting it can also cause other operations to fire.
You can easily create the equivalent of "properties" in c++, it's just a little less convenient/simplistic. private float x; public float set_x(float x_input){x = x_input; Recalc();}
> I thought Unity3D had its API classes and structures built using C++ A lot of it is built on Mono. This question would be better posted on the forums though.
– Eric5h5