XNA Creators Club Online
Page 1 of 1 (13 items)
Sort Posts: Previous Next

C#, Pointers, and References

Last post 7/29/2009 4:38 AM by DeadTyler. 12 replies.
  • 7/26/2009 12:09 AM

    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' ?

  • 7/26/2009 12:18 AM In reply to

    Re: C#, Pointers, and References

    See this and this for the answer.
    "Software is never finished, it is in varying states of 'less broken'" because "If it ain't broke, it doesn't have enough features yet"

    In Playtest: Avatar Land | The MANLY Game for MANLY Men

    The signature that was too big for the 512 char limit
  • 7/26/2009 12:19 AM In reply to

    Re: C#, Pointers, and References

    Answer
    Reply Quote
    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.net
    XNA Unit Testing: Scurvy Test
  • 7/26/2009 12:26 AM In reply to

    Re: C#, Pointers, and References

    Ah, thanks guys.

    I thought I had found something I could optimize, but I guess not!
  • 7/26/2009 11:10 AM In reply to

    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, occasionally
    kW X-port 3ds Max .X exporter
    kW Animation source code
  • 7/27/2009 4:49 AM In reply to

    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.
  • 7/27/2009 10:59 AM In reply to

    Re: C#, Pointers, and References

    Then it could be the GPU is being used inefficiently (since those are very hard to diagnose). Have you gone through these blog posts (under Performance and GPU Performance)? They were extremely helpful when I had performance problems with my game.
    "Software is never finished, it is in varying states of 'less broken'" because "If it ain't broke, it doesn't have enough features yet"

    In Playtest: Avatar Land | The MANLY Game for MANLY Men

    The signature that was too big for the 512 char limit
  • 7/27/2009 1:34 PM In reply to

    Re: C#, Pointers, and References

    Joel Martinez - XNA MVP
    Blog: http://codecube.net
    XNA Unit Testing: Scurvy Test
  • 7/27/2009 2:35 PM In reply to

    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
  • 7/27/2009 4:16 PM In reply to

    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
  • 7/27/2009 4:24 PM In reply to

    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.
  • 7/27/2009 6:39 PM In reply to

    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.
  • 7/29/2009 4:38 AM In reply to

    Re: C#, Pointers, and References

    removed
    -Tyler
Page 1 of 1 (13 items) Previous Next