-
|
|
C#, Pointers, and References
|
I have a few questions about how memory allocation with C# works... Here is an example:
class Ship { Vector2 position;
float b;
float c;
}
class Game() { //update, init, ect...
Ship[] myShips = new Ships[50];
override Update()
{ drawRadar(myShips);
}
}
class DrawRadar (Ships[] shipsToDraw) { foreach (Ship ship in shipsToDraw)
//draw position of ship on radar
}
So, in my little example here an I copying all of the values in the myShips array to the DrawRadar, or should i be using 'ref' ?
|
|
-
-
- (9059)
-
premium membership
-
Posts
3,795
|
Re: C#, Pointers, and References
|
|
|
-
-
- (520)
-
premium membership
MVP
-
Posts
942
|
Re: C#, Pointers, and References
|
You won't need to use the ref keyword because Ship is defined as a class. in C#, all classes are Reference types, while all structs are Value types. Reference types will always be passed by reference, while value types will always be copied by value unless you use the ref keyword :-)
As a side note, all arrays (even if they're an array of value types) are reference types ... so be wary of declaring new arrays all the time.
Joel Martinez - XNA MVP Blog: http://codecube.netXNA Unit Testing: Scurvy Test
|
|
-
|
|
Re: C#, Pointers, and References
|
Ah, thanks guys.
I thought I had found something I could optimize, but I guess not!
|
|
-
-
- (8307)
-
premium membership
MVP
-
Posts
6,143
|
Re: C#, Pointers, and References
|
To find things to optimize, you should not be reading code, you should be measuring your code. Use a System.Diagnostics.Stopwatch, or PIX for Windows, or similar tools, to figure out what (if anything) you need to optimize.
If you optimize something so that it takes no time at all, but it only took 0.5% of the overall time before, then you have only improved your frame rate by 0.5%... Not the right thing to optimize.
Jon Watte, Direct3D MVP Tweets, occasionallykW X-port 3ds Max .X exporter kW Animation source code
|
|
-
|
|
Re: C#, Pointers, and References
|
The problem is it runs great on my PC, i can have over 1000 objects with no real issues.
The xBox, however, is a whole nother story.
When I profile it on the PC there are no real issues.
|
|
-
-
- (9059)
-
premium membership
-
Posts
3,795
|
Re: C#, Pointers, and References
|
|
|
-
-
- (520)
-
premium membership
MVP
-
Posts
942
|
Re: C#, Pointers, and References
|
|
|
-
|
|
Re: C#, Pointers, and References
|
This post is quite intresting as im currently in the proccess of designing my game layer methods (sorry about thread hijacking).
Does my logic below sound correct?
My logic is to create X number of objects that are used over and over.
e.g.
List of Object
ID = 1 Blue Box
ID 2 = Tree
ID 3 Red Box
Map matrix (int[,] map [X,Y])
1,1,2,3,1,1
1,1,2,1,1,1
1,1,2,3,1,1
I initilise each object with there given texture and attributes.
When drawing I just cycle through the map matrix and draw the relivent object in to correct co-ordinates.
That way there is only a limited number of objects in the memory rather than loads 100's
One part I am confused by is if I had an interactable object (treasure chest etc), I would need to "clone" the object so that each object could be opened and contain a different item?
Is there any advantage to cloning then creating a new object, other than have to set the inital settings?
Neil Working on a turn based battle game Blog
|
|
-
-
- (905)
-
premium membership
-
Posts
156
|
Re: C#, Pointers, and References
|
When you start cloning you need to be aware of the difference between deep and shallow copies. In your treasure chest example I would probably go with just creating a new treasure chest object each time (or if there are very many and they are short lived consider using a pool). That way you know exactly where you are, and you won't accidentally end up with treasure chests sharing some sub-object that you didn't intend.
Dev Blog | Released: Gerbil Physics - Gerbil buildings await destruction for your amusement! | Horn Swaggle Islands - Pirate-themed defence gaming | Twitter
|
|
-
-
- (15414)
-
premium membership
MVP
-
Posts
8,556
|
Re: C#, Pointers, and References
|
Beringela: In your treasure chest example I would probably go with just creating a new treasure chest object each time
Agreed. Even if there's a lot of them it's the right way to do things.
Jim Perry - Microsoft XNA MVP If people spent a minute searching the forums and reading the FAQs before posting I'd be out of a job. Got some XNA Game Studio/XNA Framework development info to share with the community? Put it on the XNA Wiki. Please mark posts as Answers or Good Feedback when appropriate.
|
|
-
|
|
Re: C#, Pointers, and References
|
DeadShawn:Ah, thanks guys.
I thought I had found something I could optimize, but I guess not!
Creating lots of objects isn't necessarily a bad thing. If you're going for speed, try increasing the effeciency of some of your algorithms. Use a profiler like jet.
|
|
-
-
- (122)
-
premium membership
-
Posts
63
|
Re: C#, Pointers, and References
|
|
|
|