Vector3D variables exposed as Properties (bug?)

Irrlicht.Net is no longer developed or supported, Irrlicht.Net Cross Platform is a much more complete wrapper. Please ask your C# related questions on their forums first.
Locked
ghost42
Posts: 1
Joined: Thu Dec 15, 2005 6:10 am

Vector3D variables exposed as Properties (bug?)

Post by ghost42 »

I am new to Irrlicht and have been trying to dig into the code for the last several days. So my point is I am not an expert on Irrlicht, but I have noticed something that seems like a bug to me, or at least an oversight. In the .NET API accessor methods (the getPosition() and setPosition(), etc.) have often been replaced with Properties. This is fine in a lot of cases, and is generally considered a .NET best practice, but for cases where the Property is being used to expose one of the engine's value types (such as Vector3D) this is probably not appropriate unless the property is intended to be readonly. If I were to try to modify a node's position with the following C# code, the compiler will produce an error (comparable code in VB.NET will fail as well)

Code: Select all

ISceneNode node = null;
// node = Some Valid Node
node.Position.X = 5.0f;
When I was first trying to search on this topic I came across a couple posts in unrelated topics that used code like the following:

Code: Select all

ISceneNode node = null;
// node = Some Valid Node
Vector3D vec = node.Position;
vec.X = 5.0f;

// or this, which will also fail quietly but miserably
node.Position.Set(5.0f, 0.0f, 0.0f);
This will compile and run, but it will not change the position of the node. Assigning a value type simply makes a copy of the item being assigned. As soon as the get accessor in the Position property returns the underlying Vector3D, it is a copy that is being dealt with. I am not sure if the version of Irrlicht used when those older posts were made exposed Vector3D as a reference type (I am using the new 0.14), but I am worried there are some people out there wondering why things don't seem to be working the way they expect them to. Anyhow this is probably the only code that will work:

Code: Select all

node.Position = new Vector3D(5.0f, 0.0f, 0.0f);
This is tedious and almost counterintuitive for most usage cases. Also, you could spend a whole lot of time screwing around with the stack unnecessarily.

...So my question is: is this intentional design? If so, fine, though I find it to be a little dangerous for the novice .NET programmer (or any programmer not paying that close attention to whether the Irrlicht data types are value or reference types). Regardless, there should still probably be traditional SetPosition functions at the least, or even SetPositionX, SetPositionY, etc. I noticed that there is a Set(float, float, float) function, but as demonstrated above, this will not work because a get on node.Position means you are already dealing with a copy. The Set... methods would have to be on the class with ownership (the node in this case)

As a further note, the same holds true of the properties exposing transformation matrices as those are value types as well (might be more appropriate for matrices to be a reference type anyhow, not sure, but general .NET best practices call for types > 16 bytes to generally be reference types. For an integral math class this a tough call though.)

Anyhow, has anyone dealt with this/looked in to it? Anybody have any thoughts? Am I catching something that has been flying under the radar or am I missing something obvious myself?

Thanks.
Locked