Davaris Posted July 15, 2010 Share Posted July 15, 2010 I just wanted to ask why Vector3 a class instead of a struct? The other C# engines I have worked with (DirectX, Unity) make them structs. Quote Win 7 Pro 64 bit AMD Phenom II X3 720 2.8GHz GeForce 9800 GTX/9800 GTX+ 4 GB RAM Link to comment Share on other sites More sharing options...
Canardia Posted July 15, 2010 Share Posted July 15, 2010 Structs are quite useless, because Classes can do exactly the same plus more. Quote ■ Ryzen 9 ■ RX 6800M ■ 16GB ■ XF8 ■ Windows 11 ■ ■ Ultra ■ LE 2.5 ■ 3DWS 5.6 ■ Reaper ■ C/C++ ■ C# ■ Fortran 2008 ■ Story ■ ■ Homepage: https://canardia.com ■ Link to comment Share on other sites More sharing options...
TylerH Posted July 15, 2010 Share Posted July 15, 2010 There is no difference between a class and a struct, other than whether the members are private or public by default, respectively. Quote nVidia 530M Intel Core i7 - 2.3Ghz 8GB DDR3 RAM Windows 7 Ultimate (64x)----- Visual Studio 2010 Ultimate Google Chrome Creative Suite 5 FL Studio 10 Office 15 ----- Expert Professional Expert BMX Programmer ----- Link to comment Share on other sites More sharing options...
ZioRed Posted July 15, 2010 Share Posted July 15, 2010 Well, in a struct you cannot declare default values for members which must be fully assigned in the constructor (but the constructor parameters can have a default value with NET 4.0), so I see it mostly unuseful too, except very rare cases in which you have only few variables and fewer methods (well a vector class has very few variables so it could be used). Quote ?? FRANCESCO CROCETTI ?? http://skaredcreations.com Link to comment Share on other sites More sharing options...
Davaris Posted July 15, 2010 Author Share Posted July 15, 2010 There must be a reason why Unity and Microsoft chose structs over classes. I just checked C# 3.0 Pocket Reference: StructsA struct is similar to a class, with the following key differences: • A struct is a value type, whereas a class is a reference type. • A struct does not support inheritance (other than implicitly deriving from object). A struct can have all the members a class can, except: • A parameterless constructor • A finalizer • Virtual members A struct is used instead of a class when value type semantics are desirable. Good examples of structs are numeric types, where it is more natural for assignment to copy a value rather than a reference. Because a struct is a value type, each instance does not require instantiation of an object on the heap. This can be important when creating many instances of a type, for example, with an array. At any rate you may want to investigate why they chose structs over classes, because MS and Unity hire the best of the best, so I assume there is a very good reason for why they both made the same decision. Anyway its up to you guys. I just thought I'd point it out. Quote Win 7 Pro 64 bit AMD Phenom II X3 720 2.8GHz GeForce 9800 GTX/9800 GTX+ 4 GB RAM Link to comment Share on other sites More sharing options...
Canardia Posted July 15, 2010 Share Posted July 15, 2010 How can they be the best of the best if they choose C#? With C++ you can have value type classes too, so there is no such limitation that classes can be only references. Quote ■ Ryzen 9 ■ RX 6800M ■ 16GB ■ XF8 ■ Windows 11 ■ ■ Ultra ■ LE 2.5 ■ 3DWS 5.6 ■ Reaper ■ C/C++ ■ C# ■ Fortran 2008 ■ Story ■ ■ Homepage: https://canardia.com ■ Link to comment Share on other sites More sharing options...
Davaris Posted July 15, 2010 Author Share Posted July 15, 2010 How can they be the best of the best if they choose C#? With C++ you can have value type classes too, so there is no such limitation that classes can be only references. But this is C# not C++. Also who are you referring to MS, or Unity? DirectX can be used in C++ or C#. Unity can be used in C++ and C#. Quote Win 7 Pro 64 bit AMD Phenom II X3 720 2.8GHz GeForce 9800 GTX/9800 GTX+ 4 GB RAM Link to comment Share on other sites More sharing options...
Canardia Posted July 15, 2010 Share Posted July 15, 2010 It doesn't matter what language it is, but class must be value type also. Quote ■ Ryzen 9 ■ RX 6800M ■ 16GB ■ XF8 ■ Windows 11 ■ ■ Ultra ■ LE 2.5 ■ 3DWS 5.6 ■ Reaper ■ C/C++ ■ C# ■ Fortran 2008 ■ Story ■ ■ Homepage: https://canardia.com ■ Link to comment Share on other sites More sharing options...
Davaris Posted July 15, 2010 Author Share Posted July 15, 2010 It doesn't matter what language it is, but class must be value type also. Not in C#. See my quote about the difference between class and struct. Quote Win 7 Pro 64 bit AMD Phenom II X3 720 2.8GHz GeForce 9800 GTX/9800 GTX+ 4 GB RAM Link to comment Share on other sites More sharing options...
TylerH Posted July 15, 2010 Share Posted July 15, 2010 We have a very good reason for the vectors being classes. I can assure you our code is extremely efficient, and for the implementation that Lazlo and I have worked on, the results are extremely user-friendly and quick. We have yet to notice any speed issues or bottlenecks due to the nature of our math classes; however, if any such decreases or bottlenecks are discovered in the future, we will research the necessary optimizations. Quote nVidia 530M Intel Core i7 - 2.3Ghz 8GB DDR3 RAM Windows 7 Ultimate (64x)----- Visual Studio 2010 Ultimate Google Chrome Creative Suite 5 FL Studio 10 Office 15 ----- Expert Professional Expert BMX Programmer ----- Link to comment Share on other sites More sharing options...
Fester Posted July 15, 2010 Share Posted July 15, 2010 My understanding of C# structs is that they will be allocated on the stack rather than the heap which in some situations may offer some speed increases. I think the recommendation is for types that should be treat like value types to be defined as structs (Usually mathematical types like points, vectors etc) and to be kept under 16 bytes (don't quote me on this). I think the main speed increase will be seen in tight loops where you need to allocate a lot of these types at once as there will be no need to allocate the memory on the heap and resolve the references. Other than that the actual code for the type will not change so unless you are seeing any performance problems using these types, changing them from classes to structs now is just premature optimisation. Quote Link to comment Share on other sites More sharing options...
Rick Posted July 15, 2010 Share Posted July 15, 2010 Unity can be used in C++ and C#. Not from my experience. Unity seems to only be C# or Javascript (their own version of it) Quote Link to comment Share on other sites More sharing options...
Davaris Posted July 15, 2010 Author Share Posted July 15, 2010 Not from my experience. Unity seems to only be C# or Javascript (their own version of it) http://unity3d.com/support/documentation/Manual/Plugins.html Plugins - Pro only feature Unity has extensive support for C, C++ or Objective-C based Plugins. Plugins will work in standalones only. They are disabled when building a Web Player for security reasons. This is supposed to be pro only, but I have been told there is a way to do it with the free version. Quote Win 7 Pro 64 bit AMD Phenom II X3 720 2.8GHz GeForce 9800 GTX/9800 GTX+ 4 GB RAM Link to comment Share on other sites More sharing options...
Davaris Posted July 15, 2010 Author Share Posted July 15, 2010 Other than that the actual code for the type will not change so unless you are seeing any performance problems using these types, changing them from classes to structs now is just premature optimisation. I'm thinking more in terms of assumptions people make, when they're building their games. With pass by value, or pass by reference, you can make changes to pass by value, without altering the original value. But if its pass by reference, then you alter the source. So if there is one standard used by other engines and we are the only ones doing something different, then it can isolate us - if you get my drift. Anyway I don't know how much of a big deal it is, when it comes to doing conversions. I guess it depends on how complex the game part of your engine is. Quote Win 7 Pro 64 bit AMD Phenom II X3 720 2.8GHz GeForce 9800 GTX/9800 GTX+ 4 GB RAM Link to comment Share on other sites More sharing options...
Canardia Posted July 15, 2010 Share Posted July 15, 2010 Unity C plugins can't call engine functions, only the engine can call the plugin functions, which is kinda useless since usually your own plugins and functions are supposed to call engine functions. Quote ■ Ryzen 9 ■ RX 6800M ■ 16GB ■ XF8 ■ Windows 11 ■ ■ Ultra ■ LE 2.5 ■ 3DWS 5.6 ■ Reaper ■ C/C++ ■ C# ■ Fortran 2008 ■ Story ■ ■ Homepage: https://canardia.com ■ Link to comment Share on other sites More sharing options...
L B Posted July 15, 2010 Share Posted July 15, 2010 Back on topic: Our vectors inherit from an abstract class implement property accessors have instanced utility math functions define custom operators have custom constructors All of which a struct is not allowed to do in C#/.NET. Quote Link to comment Share on other sites More sharing options...
Fester Posted July 15, 2010 Share Posted July 15, 2010 I'm pretty sure Structs in C# / .Net CAN... Implement Property accessors, define custom operators, and have custom constructors In fact, I have the Visual Studio object browser open now and I can see a number of Structs that implement at least one or more of these features: E.G. System.Drawing.Point, System.Drawing.Color and System.Drawing.Rectangle. Quote Link to comment Share on other sites More sharing options...
Davaris Posted July 15, 2010 Author Share Posted July 15, 2010 I just looked up XNA and Vector3 is struct there as well. Anyway as I said above, for fundamentals like Vector3, Point, string, etc, it might be an idea to see what the consensus is on the popular 3D engines using C#. As most programmers have built up libraries and game code, they won't want to maintain two versions if LWs C# is the odd man out. Not that I am saying LW C# is the odd man out. The only ones I have checked is DirectX, Unity and XNA. EDIT: I just found this. Perhaps it is an argument for keeping Vector3 as a class? http://dotnetperls.com/static-constructor You want to determine the performance hit for static constructors in the C# programming language. Microsoft and many developers warn that static constructors on a type impose a substantial overhead. Static constructors are also called type initializers, because they refer to types, not instances. Here we test static constructors in the C# language and determine if they are useful. === Static constructor performance test in C# === Class 1: Has static constructor Time: 3208 ms Class 2: No static constructor Time: 319 ms Quote Win 7 Pro 64 bit AMD Phenom II X3 720 2.8GHz GeForce 9800 GTX/9800 GTX+ 4 GB RAM Link to comment Share on other sites More sharing options...
L B Posted July 16, 2010 Share Posted July 16, 2010 With the new dynamic vector design, such things are not possible with structs: Inheriting from the FloatArrayManaged class. Defining internal constructors in which not all variables are assigned before control is returned to the caller. I do feel the importance of efficiency, but the design advantage gives development speed. In other words: If you're worried so much about minor performance issues such as this one over development speed, have a look into C++ or C, which run faster but develop slower. Quote Link to comment Share on other sites More sharing options...
Davaris Posted July 16, 2010 Author Share Posted July 16, 2010 In other words: If you're worried so much about minor performance issues such as this one over development speed, have a look into C++ or C, which run faster but develop slower. My chief concern is standards, so people can use libraries they have built up without having to maintain different versions for different engines. However if the speed difference is as great as claimed, then I'd think again. As for C++, I've been there and done that and now I prefer languages that are easier and more enjoyable to use. Case in point, if Python wasn't so slow, I'd be using that instead of C#. Quote Win 7 Pro 64 bit AMD Phenom II X3 720 2.8GHz GeForce 9800 GTX/9800 GTX+ 4 GB RAM Link to comment Share on other sites More sharing options...
Rick Posted July 16, 2010 Share Posted July 16, 2010 if Python wasn't so slow, I'd be using that instead of C#. I thought you said easier and more enjoyable Quote Link to comment Share on other sites More sharing options...
Davaris Posted July 16, 2010 Author Share Posted July 16, 2010 Python really is enjoyable to use. So much so, that it got me over a long episode where I was burned out from programming (C++) for a couple of years. I haven't used Python for game programming though, because I keep reading comments that it is too slow. However people are working on speeding it up: http://psyco.sourceforge.net/ Quote Win 7 Pro 64 bit AMD Phenom II X3 720 2.8GHz GeForce 9800 GTX/9800 GTX+ 4 GB RAM Link to comment Share on other sites More sharing options...
Rick Posted July 16, 2010 Share Posted July 16, 2010 Seems it could be a substitute for something like LUA, for higher level scripting. Quote Link to comment Share on other sites More sharing options...
Davaris Posted July 16, 2010 Author Share Posted July 16, 2010 Python would be fine as a scripting language for programmers, as long as you never intend to give modders access to it because with it, they could do terrible things to a user's machine. Quote Win 7 Pro 64 bit AMD Phenom II X3 720 2.8GHz GeForce 9800 GTX/9800 GTX+ 4 GB RAM Link to comment Share on other sites More sharing options...
Rick Posted July 16, 2010 Share Posted July 16, 2010 You can basically do the same with LUA. Most game companies that expose LUA for gamers just strip it down so you can't do that, but out of the box you can do pretty much anything with it. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.