Pointers/references in XNA

Started by
3 comments, last by TheSpotlessPane 12 years, 7 months ago
I was wondering if you could pass by reference in XNA. The reason this question arose for me is because I wanted to create a sprite class. In the constructor, I want to be able to pass in the name of the file, and load the texture from the constructor using that string. To do this, I'd like to be able to pass a pointer to the graphicsDeviceManager (or pass by reference). As I understand it, if I just pass the GraphicsDeviceManager without specifying that it references an already existing object, the compiler (or what have you, not sure if my terminology is correct here) will create a NEW GraphicsDeviceManager object that is local to the function. This would both waste memory, and it might have bad consequences (since the permanent GDM wouldn't be doing the loading). So, is there a way to pass by reference in XNA? Or does this kind of thing require a work around...like having all of the actual loading take place in the main game loop class.

Also, I was playing around, and declared a Point (that would store the location of my sprite object) as a pointer like so:

Point* pos;

This didn't produce an error which seems to suggest that pointers are allowed in C#. BUT, when I tried to use the pointer in my constructor to initialize the values, it gave me an error and said something to the effect of "pointers can only be used in an unsafe context." What constitutes an unsafe context? Thanks for any help.
Advertisement
AFAIK, instances of classes (objects) are considered reference types in C#. Which means when you pass those as arguments, you're already passing them by reference.

http://msdn.microsof...2(v=VS.71).aspx


EDIT: the wording might be weird. I meant: classes are reference types. So instances of classes are passed by reference.
You can also use the ref keyword for pass by reference.

To do this, I'd like to be able to pass a pointer to the graphicsDeviceManager (or pass by reference). As I understand it, if I just pass the GraphicsDeviceManager without specifying that it references an already existing object, the compiler (or what have you, not sure if my terminology is correct here) will create a NEW GraphicsDeviceManager object that is local to the function.

This would both waste memory, and it might have bad consequences (since the permanent GDM wouldn't be doing the loading). So, is there a way to pass by reference in XNA? Or does this kind of thing require a work around...like having all of the actual loading take place in the main game loop class.


If you pass "null" to a method, it means a null reference (as in, not refering to an existing object). This does not mean an object will be created for you automatically, though. Sometimes passing null can be valid input (e.g. if something is null, do this or don't do this). Most cases, it requires you to do error checking (e.g. GraphicsDeviceManager probably should not be null, so throw an exception).

What you might have this mixed up with are passing structs around, which are value-types (allocated/stored on the stack, doesn't create garbage since not allocated on the heap, etc - that's the neat thing about them since they can help improve performance). Say you're passing a Vector3 or a Matrix to a function, these values are copied and passed to the function, so if you made changes to a Vector3's value in the method you called, that change will not be reflected in the Vector3 that you passed in because it's not pass by reference. When dealing with class objects, it's always pass by reference.

However, you can pass value types by reference, which requires special keywords (ref and out). But this is a bit different from what you were originally thinking, since GraphicsDeviceManager is a class and not a struct. So like I said earlier, if you have a Sprite class that needs access to the graphics device, and the GDM is null - that's high time to throw an error because it'll be unable to initialize itself or do any meaningful work. Anyways, I recommend looking up these topics on the MSDN docs:

Passing Parameters

Classes vs Structs

There's also a plethora of information out there on the net too.


Also, I was playing around, and declared a Point (that would store the location of my sprite object) as a pointer like so:

Point* pos;

This didn't produce an error which seems to suggest that pointers are allowed in C#. BUT, when I tried to use the pointer in my constructor to initialize the values, it gave me an error and said something to the effect of "pointers can only be used in an unsafe context." What constitutes an unsafe context? Thanks for any help.


Pointers can be used in C# but only when in an unsafe context, e.g. the method has to be declared with the unsafe keyword, or your pointer be inside an unsafe code block. You have to also allow your code to be compiled with the /unsafe compiler option (can be easily set in Visual Studio under the project's properties).
Thanks a bunch, guys, very helpful info. I added the msdn link to my favorites, and have already used it for several other questions. I think that site is gonna' be very, very handy.

This topic is closed to new replies.

Advertisement