- Home /
C# Point structure
Is there a way to store xy cooridnates in a point structure instead of a Vector2 (I dont want to have to convert the floats to int every time I use them)
I tried System.Collections.Generic;, but this still does not give the Point structure for C#. Is there another collections I need to use?
Thanks!
Answer by agamedesigner · Aug 04, 2012 at 03:50 PM
struct Point
{
public int x, y;
}
then use it like:
Point myPoint;
myPoint.x = 5;
myPoint.y = 3;
you can also use it with a constructor:
struct Point
{
public int x, y;
public Point(int px, int py)
{
x = px;
y = py;
}
}
in which case you could do this:
Point myPoint = new Point(5, 7);
Thanks! That really helped a lot!! =]!
If you read this, and have a second, I would like to know what exactly is going on. I have not really run into this type of set-up yet in my basic code skills.
Thanks again!!!
Hi, a lot can be said about them so I've provided some links for you. Basically they are very similar to classes, but should generally be used for lightweight things only (unless you want to kill performance). They are a value type like int or char, as opposed to a reference type, which a class is.
$$anonymous$$ore on value vs reference types:
http://msdn.microsoft.com/en-us/library/t63sy5hs(v=vs.80).aspx
http://www.c-sharpcorner.com/uploadfile/e7ad16/reference-type-and-value-type-in-C-Sharp/
Here are the links on structs, in order:
http://msdn.microsoft.com/en-us/library/ah19swz4(v=vs.71).aspx
http://www.csharp-station.com/Tutorial/CSharp/lesson12
http://www.dotnetperls.com/struct
http://stackoverflow.com/questions/7484735/c-struct-vs-class-faster
http://stackoverflow.com/questions/521298/when-to-use-struct-in-c
Hope this helps!
@agamedesigner: You should implement IEquatable, GetHashCode and all that crap if you're going to implement your own Point class. Otherwise, Unity's old implementation of the .NET framework will create garbage in weird places. See: http://stackoverflow.com/questions/1307493/is-there-a-complete-iequatable-implementation-reference https://andrewfray.wordpress.com/2013/02/04/reducing-memory-usage-in-unity-c-and-netmono/
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
how do I move array objects to world coordinates? 1 Answer
Simple C# Vector2D Moving Question 1 Answer